Board index » delphi » Setting event handlers at runtime

Setting event handlers at runtime

Hi,
Can anyone tell me how to attach my own event handler to a VCL component at
run-time?  I need to be able to create my own components (e.g. comboboxes,
buttons) while the program is running (THAT I can do already), and then want
to trap the standard events (e.g. combobox OnChange, button clicked) that
apply to these components and direct them to a procedure which will already
be part of the program.
Any help would really be appreciated.
Thanks,
Peter
 

Re:Setting event handlers at runtime


Peter,

Like this:

  MyCombobox.OnChange := MyOnChangeEvent;

where MyOnChangeEvent is the name of a routine which conforms to the type
expected by the event handler, in this case TNotifyEvent.

HTH,

Chris.
-------------

Quote
"Peter Millican" wrote in message <2000Jan9.150427.9...@leeds.ac.uk>...
>Hi,
>Can anyone tell me how to attach my own event handler to a VCL component at
>run-time?  I need to be able to create my own components (e.g. comboboxes,
>buttons) while the program is running (THAT I can do already), and then
want
>to trap the standard events (e.g. combobox OnChange, button clicked) that
>apply to these components and direct them to a procedure which will already
>be part of the program.
>Any help would really be appreciated.
>Thanks,
>Peter

Re:Setting event handlers at runtime


Peter,

First, declare event-handler procedure inside your comonent. For example:

YourComponent = class(...)
private
  FButton: TButton; // "inner" component.
  ...
  procedure ClickHandler(Sender: TObject);
  ...
protected
  ...
  procedure Loaded; override;
  ...
end;

Then (within overrided Loaded method or in some other appropriate place),
attach this handler to the "inner" component.

procedure Loaded;
begin
  ...
  // FButton is already created.
  FButton.OnClick := ClickHandler;
  ...
end;

--
Branko Dimitrijevic
b...@bepp.8m.com
http://bepp.8m.com (Delphi components)

Quote
"Peter Millican" <pe...@nospam.scs.leeds.ac.uk> wrote in message

news:2000Jan9.150427.9104@leeds.ac.uk...
Quote
> Hi,
> Can anyone tell me how to attach my own event handler to a VCL component
at
> run-time?  I need to be able to create my own components (e.g. comboboxes,
> buttons) while the program is running (THAT I can do already), and then
want
> to trap the standard events (e.g. combobox OnChange, button clicked) that
> apply to these components and direct them to a procedure which will
already
> be part of the program.
> Any help would really be appreciated.
> Thanks,
> Peter

Re:Setting event handlers at runtime


Peter,

TNotifyEvent is defined as:

  type TNotifyEvent = procedure (Sender: TObject) of object;

So any procedure that you want to be an event handler must conform to this.
For example:

  type
    MyClass = class(TObject)
      procedure MyComboboxOnClick(Sender : TObject);
      end;

MyComboboxOnClick now conforms to the TNotifyEvent type.  It is a procedure,
it takes one parameter (a TObject) and it is a method of an object (that's
what the "of object" means).  Now MyComboboxOnClick can be assigned to any
combobox's OnChange event property:

  MyCombobox.OnChange := MyClass.MyComboboxOnClick;

All of the various event handler types are documented in the help file (or
at least they should be).  If they aren't in they help file, you might have
to dig around the VCL source code.

If you are still having problems, please post the code which is giving you
trouble.

HTH,

Chris.
------------

Quote
"Peter Millican" wrote in message <2000Jan10.143534.24...@leeds.ac.uk>...
>Thanks very much Chris, but ...

>>  MyCombobox.OnChange := MyOnChangeEvent;

>>where MyOnChangeEvent is the name of a routine which conforms to the type
>>expected by the event handler, in this case TNotifyEvent.

>... my problem was writing a procedure which conforms to that type.
>Everything I tried produced a type incompatibility problem.  What does a
>TNotifyEvent look like?
>All help much appreciated,
>Peter

Re:Setting event handlers at runtime


Thanks very much Chris, but ...

Quote
>  MyCombobox.OnChange := MyOnChangeEvent;

>where MyOnChangeEvent is the name of a routine which conforms to the type
>expected by the event handler, in this case TNotifyEvent.

... my problem was writing a procedure which conforms to that type.
Everything I tried produced a type incompatibility problem.  What does a
TNotifyEvent look like?
All help much appreciated,
Peter

Other Threads