正如 Sertac Akyuz 所提到的,只需将另一个事件分配给您的按钮。
一个透支的例子可能是:
unit DynamicEvents;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TNotifyClass=Class
Constructor Create(const msg:String);overload ;
Constructor Create(EV:TNotifyEvent);overload;
Procedure NotifyEvent(Sender:TObject);
private
FMessage:String;
FNotifyEvent:TNotifyEvent;
End;
TNotifyList=Class
Constructor Create(arr:Array of String);
Destructor Destroy;override;
private
FList:TList;
FLastAccessedIndex:Integer;
function GetEvent(Index: Integer): TNotifyEvent;
public
Property Events[Index:Integer]:TNotifyEvent read GetEvent; default;
Procedure Add(NC:TNotifyClass);
published
Property LastAccessedIndex:Integer read FLastAccessedIndex;
End;
TForm6 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private-Deklarationen }
FNotifyList:TNotifyList;
procedure FormCloseEV(Sender: TObject);
public
{ Public-Deklarationen }
end;
var
Form6: TForm6;
implementation
{$R *.dfm}
{ TNotifyList }
procedure TNotifyList.Add(NC: TNotifyClass);
begin
Flist.Add(NC);
end;
constructor TNotifyList.Create(arr: array of String);
var
i:Integer;
begin
FList := TList.Create;
FLastAccessedIndex := -1;
for I := Low(arr) to High(arr) do
FList.Add(TNotifyClass.Create(arr[i]));
end;
destructor TNotifyList.Destroy;
var
i:Integer;
begin
for I := 0 to Flist.Count -1 do
TNotifyClass(FList[i]).Free;
Flist.Free;
inherited;
end;
function TNotifyList.GetEvent(Index: Integer): TNotifyEvent;
begin
if (Index>=0) and (Index<Flist.Count) then
begin
FLastAccessedIndex := Index;
end
else
begin
if Flist.Count>0 then
begin
FLastAccessedIndex := 0;
end
else
begin
FLastAccessedIndex := -1;
end;
end;
if FLastAccessedIndex >- 1 then Result := TNotifyClass(FList[FLastAccessedIndex]).NotifyEvent;
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
Button2.OnClick := FNotifyList[FNotifyList.LastAccessedIndex + 1];
end;
{ TNotifyClass }
constructor TNotifyClass.Create(const msg: String);
begin
FMessage := msg;
end;
constructor TNotifyClass.Create(EV:TNotifyEvent);
begin
FNotifyEvent := EV;
end;
procedure TNotifyClass.NotifyEvent(Sender: TObject);
begin
if Assigned(FNotifyEvent) then FNotifyEvent(Sender)
else Showmessage(FMessage);
end;
procedure TForm6.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutDown := true;
FNotifyList:=TNotifyList.Create(
['Message 1'
,'Message 2'
,'Message 3'
,'Message 4'
,'Message 5'
,'Message 6'
,'Message 7'
,'Message 8'
,'Message 9'
,'Message 10']
);
FNotifyList.Add(TNotifyClass.Create(FormCloseEV));
end;
procedure TForm6.FormCloseEV(Sender: TObject);
begin
if MessageDLG('Close',mtConfirmation,[mbyes,mbCancel],0)=idYes then Close;
end;
procedure TForm6.FormDestroy(Sender: TObject);
begin
FNotifyList.Free;
end;
end.