...add a procedural type to TList?
|
Autor:
César Nicolás Peña Núñez |
[ Print tip
] | | |
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
TEventObject = class
protected
FEvent: TNotifyEvent;
published
property Event: TNotifyEvent read FEvent write FEvent;
end;
// Zur Liste hinzufügen
// To add to the list
procedure TForm1.Button1Click(Sender: TObject);
var
A: TEventObject;
begin
A := TEventObject.Create;
A.Event := Button1Click; // or any TNotifyEvent
ListBox1.Items.AddObject('Button1Click', A);
end;
// Den Event aufrufen
// To call the event
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if ListBox1.ItemIndex <> -1 then
TEventObject(ListBox1.Items.Objects[ListBox1.ItemIndex]).Event(Self);
end;
end.
// 2. Lösung
// 2nd solution
{....}
private
FList: TList;
end;
{....}
procedure TForm1.FormCreate(Sender: TObject);
begin
FList := TList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('event');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
P: TNotifyEvent;
begin
// Get pointer to event
// Pointer des Events holen
P := Button1Click;
// Add Pointer to List
// Pointer zur Liste hinzufügen
FList.Add(@P);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
P: TNotifyEvent;
begin // Get first event from list
// Den ersten Event aus der Liste holen
@P := FList[0];
// Execute event
// Event ausführen
P(Sender);
end;
|