Board index » delphi » assign event handler w/out object inspector

assign event handler w/out object inspector

How would I assign an event handler without using the Object Inspector?
In the code I create an instance of a component,
Fcomponent:=Tcomponent.create(Fcomponent)

which means I can't assign a procedure by clicking on the events tab of the
Inspector.  How is it done?

-phreak

 

Re:assign event handler w/out object inspector


MyEdit.OnClick := SomeProcedure. Make sure that SomeProcedure has the right
prototype e.g. procedure SomeProcedure(Sender: TObject);

Incidentally, I think you meant to say
Fcomponent:=Tcomponent.create(Tcomponent)

Charles Johnson

Re:assign event handler w/out object inspector


In article <19981026183110.16439.00002...@ng-fi1.aol.com>, plcphr...@aol.com

Quote
(Plcphreak) writes:
>How would I assign an event handler without using the Object Inspector?
>In the code I create an instance of a component,
>Fcomponent:=Tcomponent.create(Fcomponent)

>which means I can't assign a procedure by clicking on the events tab of the
>Inspector.  How is it done?

An event handler is a pointer to a method of the correct type (ie it has the
appropriate parameters for that event handler). Because Delphi references and
de-references (ie changes names to pointers to that variable and vice-versa)
for you, you only need to quote the name. S0 :-

FComponent.OnClick := MyEventHandlerForOnClick;

 . . . and somewhere . . .

procedure TMyObject.MyEventHandlerForOnClick(Sender : TObject);
begin
  {etc etc}
end;

The procedure must be of the appropriate type (the OnClick is of TNotifyEvent
which is declared as "procedure(Sender : TObject) of object").

Within the event handler, Sender is the object which caused the event, and Self
is the instance of TMyObject whose event handler you're calling (usually but
not essentially, an instance of the TComponent referred to in your posting).

BTW I strongly advise against naming instances of an object with an 'F' prefix.
By widely used convention the 'F' prefix is reserved for private variables in a
class, particularly for private storage for properties.

Alan Lloyd
alangll...@oal.com

Re:assign event handler w/out object inspector


In article <19981027050149.03390.00000...@ngol07.aol.com>, cehjohn...@aol.com

Quote
(CEHJohnson) writes:
>Incidentally, I think you meant to say
>Fcomponent:=Tcomponent.create(Tcomponent)

Actually, let's revise this (in the light of my own imprecision and Alan's
comments)
IF for instance the component were a contained class of an object, you would
say
FComponent := TComponent.Create(self)

Charles Johnson

Other Threads