Delphi: How to assign an "Event to Object" event method?
I have a menu item and I am trying to assign an event handler to it OnClick
:
miFrobGizmo.OnClick := {something};
An event handler property OnClick
, like almost every other event handler, is defined as a method type TNotifyEvent
:
property OnClick: TNotifyEvent
where TNotifyEvent
:
TNotifyEvent = procedure(Sender: TObject) of object;
I have an object with a method that matches the signature TNotifyEvent
:
TAnimal = class(TObject)
public
procedure Frob(Sender: TObject);
end;
So, I think I should be able to use an object method and assign it to a click event handler :
var
Animal: TAnimal;
miFrobGizmo.OnClick := Animal.Frob;
Also, I am getting the error:
[Error]File.pas(1234): Not enough actual parameters
Maybe my brain fart, but I thought I could do it.
The details I didn't mention is that my object, which has a mapping method, has a method exposed through the interface:
IAnimal = interface
procedure Frob(Sender: TObject);
end;
TAnimal = class(TInterfacedObject, IAnimal)
public
procedure Frob(Sender: TObject);
end;
var
Animal: IAnimal;
miFrobGizmo.OnClick := Animal.Frob;
Follow-up question
a source to share
You cannot do this. It says "object procedure", not "interface procedure". This will allow you to do this if you use the object directly, but since you haven't, it doesn't think you are trying to assign an event handler, and instead, the parser tries to treat your code as a method call. Then he sees that you don't have any parameters to call the method, but the call requires one parameter, so it exits and throws an error.
a source to share