Thema: Delphi Eventlist

Einzelnen Beitrag anzeigen

Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.453 Beiträge
 
Delphi 12 Athens
 
#3

AW: Eventlist

  Alt 14. Mai 2018, 10:39
Günther hat ja schon den entscheidenden Hinweis mit der Deklaration des Event-Typs gegeben. Allerdings kannst du mit seiner Lösung nur gleichartige Events in der Eventlist speichern, was aber deine Vorgabe der verschiedenen Eventhandler nicht berücksichtigt.

Abhilfe schafft hier ein bisschen RTTI und die Verwendung von TValue als Listenelement:

Delphi-Quellcode:
unit core;

interface

uses
  System.Classes, System.Generics.Collections, System.Rtti;

TYPE
  TEventList = TDictionary<STRING, TValue>;

  TMyList = Class
  private
    flist: TEventList;
  public
    constructor Create;
    Destructor Destroy; override;
    function GetEvent<T>(const Name: string): T; overload;
    procedure SetEvent<T>(const Name: string; const Value: T); overload;
  End;

implementation

uses
  System.SysUtils;

constructor TMyList.Create;
begin
  inherited Create;
  flist := TEventList.Create;
end;

Destructor TMyList.Destroy;
begin
  flist.free;
  inherited Destroy;
end;

function TMyList.GetEvent<T>(const Name: string): T;
begin
  if (flist.containskey(name)) then
    result := flist[name].AsType<T>
  else
    RAise Exception.Create(name+' is not available !');
end;

procedure TMyList.SetEvent<T>(const Name: string; const Value: T);
begin
  if (not flist.containskey(name)) then
    flist.AddOrSetValue(name, TValue.From<T>(Value))
  else
    Raise Exception.Create(name+' already assigned !');
end;

end.
Delphi-Quellcode:
unit mainU;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,core;

type
  TMyEventHandler = procedure (Sender:TObject; const param1:String) of object;
  TMyEventHandler2 = procedure (Sender:TObject; const param1, param2:String) of object;

type
  TForm52 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    mylist : TMylist;
  public
    procedure MyEventHandler(Sender:TObject; const param1:String);
    procedure MyEventHandler2(Sender: TObject; const param1, param2: String);
  end;

var
  Form52: TForm52;

implementation

{$R *.dfm}

procedure TForm52.Button1Click(Sender: TObject);
begin
  mylist.SetEvent<TMyEventHandler>('Event', MyEventHandler);
  mylist.SetEvent<TMyEventHandler2>('Event2', MyEventHandler2);
end;

procedure TForm52.Button2Click(Sender: TObject);
begin
  MyList.GetEvent<TMyEventHandler>('Event')(nil, 'Wupti');
end;

procedure TForm52.Button3Click(Sender: TObject);
begin
  MyList.GetEvent<TMyEventHandler2>('Event2')(nil, 'Wupti', 'Hallo');
end;

procedure TForm52.FormCreate(Sender: TObject);
begin
  mylist := TMyList.create;
end;

procedure TForm52.FormDestroy(Sender: TObject);
begin
  mylist.free;
end;

procedure TForm52.MyEventHandler(Sender: TObject; const param1: string);
begin
  ShowMessage(Param1);
end;

procedure TForm52.MyEventHandler2(Sender: TObject; const param1, param2: String);
begin
  ShowMessage(Param1 + #13 + Param2);
end;

end.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat