unit Unit1;
interface
uses
SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Generics.Defaults, Generics.Collections, StdCtrls;
type
TCallback = TProc<TObject>;
TForm1 =
class(TForm)
RegisterButton: TButton;
UnregisterButton: TButton;
CallButton: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure RegisterButtonClick(Sender: TObject);
procedure UnregisterButtonClick(Sender: TObject);
procedure CallButtonClick(Sender: TObject);
procedure MyCallback(Sender: TObject);
private
FList: TList<TCallback>;
procedure RegisterCallback(CB: TCallback);
procedure UnregisterCallback(CB: TCallback);
procedure DoCallbacks(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FList := TList<TCallback>.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FList.Free;
end;
procedure TForm1.RegisterButtonClick(Sender: TObject);
begin
RegisterCallback(MyCallback);
end;
procedure TForm1.UnregisterButtonClick(Sender: TObject);
begin
UnregisterCallback(MyCallback);
end;
procedure TForm1.CallButtonClick(Sender: TObject);
begin
DoCallbacks(Self);
end;
procedure TForm1.MyCallback(Sender: TObject);
begin
ShowMessage('
Hallo');
end;
procedure TForm1.RegisterCallback(CB: TCallback);
begin
if FList.IndexOf(CB) < 0
then
FList.Add(CB);
end;
procedure TForm1.UnregisterCallback(CB: TCallback);
begin
//if FList.IndexOf(CB) >= 0 then
// ShowMessage('ist drin');
FList.Remove(CB);
end;
procedure TForm1.DoCallbacks(Sender: TObject);
var
i: Integer;
begin
for i := FList.Count - 1
downto 0
do
FList[i](Sender);
end;
end.