It would help if you explained the reasons why you want to do this – a lot
of problems are caused by using the wrong "solution" in the first place, so
there may be a better way of achieving what you want to do without needing
to find methods by name at run-time. However, the following code may give
you some ideas.
WARNING!!! Use entirely at your own risk! Although there’s nothing in it
that isn’t in the Delphi Help Files it relies on the way Delphi stores
method pointers internally, and that could easily change in future releases
(it may already have done, for all I know – I use Delphi 5). Also note, I’ve
left out all the error-checking code you would need (such as there not being
a method of the name you’re looking for).
*******************
unit ClickMe;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
StdCtrls, Controls, Forms, Dialogs;
const
NumProcs = 2;
type
PTNEvent = ^TNotifyEvent;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
Buttons: array[1..NumProcs] of TButton;
Methods: array[1..NumProcs] of TMethod;
ClickCount: integer;
published
procedure MyClick1 (Sender: TObject);
procedure MyClick2(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.MyClick1(Sender: TObject);
begin
Inc(ClickCount);
ShowMessage(‘Click 1′ + #13#10 + IntToStr(ClickCount) + ‘ clicks’);
end;
procedure TForm1.MyClick2(Sender: TObject);
begin
Inc(ClickCount);
ShowMessage(‘Click 2′ + #13#10 + IntToStr(ClickCount) + ‘ clicks’);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
CurLeft, CurTop: integer;
CurAddr: PTNEvent;
begin
CurLeft := 0;
CurTop := 0;
for i := 1 to NumProcs do begin
Methods[i].Code := MethodAddress(‘MyClick’ + IntToStr(i));
Methods[i].Data := Self;
end;
for i:= 1 to NumProcs do begin
Buttons[i] := TButton.Create(Self);
Buttons[i].Left := CurLeft;
Buttons[i].Top := CurTop;
Buttons[i].Parent := Self;
Buttons[i].Caption := IntToStr(i);
Pointer(CurAddr) := @Methods[i];
Buttons[i].Onclick := CurAddr^;
Inc(CurTop, Buttons[i].Height);
end;
end;
end.
*************
Ray
"maier" <w…@firemail.de> wrote in message
news:eh3a6u4uuh90de4jf7oc3l1lsm6h150dri@4ax.com…
- Hide quoted text — Show quoted text -
> HI i want to set an Event for each OnDisconnect
> For i := 0 to 2 do begin
> IRC[i].OnDisconnect := xx[i]Disconnect;
> end;
> events xx1Disconnect, xx2Disconnect … exists !
> but it doesent work
> thx