Componentes - acessando eventos pelo nome (string) |
Top Previous Next |
TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } // manually enter these 2 lines and the MyClick implementation published procedure MyClick(Sender: TObject); end; {...} uses TypInfo;
procedure TForm1.Button1Click(Sender: TObject); var meth: TMethod; begin // prepare method to be assigned meth.Code := MethodAddress('MyClick'); meth.Data := Self; // Form1 SetAMethod(Button2, 'OnClick', meth); end;
procedure Form1.SetAMethod(c: TControl; ev: string; m: TMethod); var pi: PPropInfo; begin // does control have that event? pi := GetPropInfo(c.ClassInfo, ev); // if so, assign it to the the passed method if Assigned(pi) then SetMethodProp(c, pi, m); end;
procedure TForm1.MyClick(Sender: TObject); begin showmessage('hello!'); end;
|