Board index » delphi » How to assign event handlers at run time?

How to assign event handlers at run time?

I'm writing an application where I will have some components created
dynamically.  I was wondering if there is a way to assign event handlers
to these components by using function pointers.  Any ideas?

Dan

 

Re:How to assign event handlers at run time?


Dan,

Take a look at the event handler in question, and write a method of the
form with the same parameters.  For example, the OnClick event handler for
Button1 on Form1 looks like
  procedure TForm1.Button1Click(Sender: TObject);

This event is defined as a TNotifyEvent type.  (You do not have to know
this right now, but in case you are interested.)  TNotifyEvent is defined
as:
  type
    TNotifyEvent = procedure(Sender: TObject) of object;

Meaning that any method that you write with one parameter of type TObject
is also a TNotifyEvent.  Your method can therefore be assigned to ANY event
handler that is the same type, just as you can assign the number 94 to any
integer variable.  (You cannot assign 94 directly to a string, nor can you
assign a procedure of type TNotifyEvent to an event handler expecting
different parameters).  This should explain why the Object Inspector in
Delphi allows you to choose from only the compatible event handlers in the
dropdown comboboxes on the Events page.

Anyway, inside the form class definition, place a declaration for your
event handler like so:
  type
    TForm1 = class(TForm)
      ...
    private
      procedure MyEventHandler(Sender: TObject);
    public
    end;

And in the implementation section, write the method:
  procedure TForm1.MyEventHandler(Sender: TObject);
  begin
    //Your implementation
  end;

When you create your new button, you can assign the event handler just as
you would any other property of the button:
  procedure TForm1.FormClick(Sender: TObject);
  var
    MyButton: TButton;
  begin
    MyButton := TButton.Create(Self);
    with MyButton do begin
      Left := 10;
      Top := 10;
      Caption := '&Delphi Rocks!!!';
      Width := 100;
      Default := true;
      Parent := Self;
      OnClick := MyEventHandler; // <<-- Here it is!!!
    end;
  end;

Notice that you do not pass a parameter to MyEventHandler, because you are
not actually calling the function in that statement.  You are just telling
the button what function it should call when the event occurs.  This is the
method pointer you were after.

Ken
--------------------------------------
Notice at no time do my fingers leave my hands...
Quality training touches your technical side and helps you grow.
--------------------------------------
Master Certified Delphi 3 Instructor
Master Certified C++Builder Instructor
http://www.knowledgeable.com
k...@knowledgeable.com
+

Dan Axtell <d...@mail1.nai.net> wrote in article
<338A1CF3.5...@mail1.nai.net>...

Quote
> I'm writing an application where I will have some components created
> dynamically.  I was wondering if there is a way to assign event handlers
> to these components by using function pointers.  Any ideas?

> Dan

Re:How to assign event handlers at run time?


Quote
Kenneth A. Faw wrote:

> the button what function it should call when the event occurs.  This is the
> method pointer you were after.

> Ken
> --------------------------------------

Thanks a lot. I will try just that.

Dan

Re:How to assign event handlers at run time?


Quote
Dan Axtell wrote:

> I'm writing an application where I will have some components created
> dynamically.  I was wondering if there is a way to assign event handlers
> to these components by using function pointers.  Any ideas?

> Dan

=====================================
Hi, Dan.
I suspect you mean something like :

procedure TForm1.SomeProc;
begin
   Button1 := TButton.Create(self);
   Button1.OnClick := Button1Pressed;
end;

procedure TForm1.Button1Pressed(Sender: TObject);
begin
   // do your stuff here...
end;

Re:How to assign event handlers at run time?


Try this:

TForm1 = class(TForm);//for example
...
private
  AButton:TButton;
  procedure BtnClick(Sender:TObject);
...
end;

procedure TForm1.FormCreate(Sender:......
begin
.....
  AButton:=TButton.Create(self); // let's create it in form's OnCreate....
  AButton.OnClick:=BtnClick;
...
end;

Note that U should declare procedures like "BtnClick" similar to type of
object's handler type(TNotifyEvent for BtnClick).

HTH, Vick
<V...@lsp.nsu.ru>

Dan Axtell <d...@mail1.nai.net> ???? a ?t
<338A1CF3.5...@mail1.nai.net>...

Quote
> I'm writing an application where I will have some components created
> dynamically.  I was wondering if there is a way to assign event handlers
> to these components by using function pointers.  Any ideas?

> Dan

Other Threads