Is there a way in Delphi to assign an anonymous method to a button event?

I was wondering if there is a way in Delphi to assign an anonymous method to a form control event.

For instance:

Button1.OnClick := procedure (Sender: TObject) begin ShowMessage('') end; 

Of course this gives me an error

[dcc32 error] Control.Controller.pas (51): E2009 Incompatible types: "method pointer and regular procedure"

This is because the method must belong to the object, but then it will not be anonymous.

Perhaps there are several works for this

+6
source share
1 answer

It's impossible. You must make the event handler a method type, not an anonymous method.

You will need to wrap your anonymous method in the method. Either an instance method (record or class) or a class method. For instance:

+8
source

Source: https://habr.com/ru/post/974350/


All Articles