Delphi – use a closure on an event handler (ie, TButton OnClick)

delphidelphi-2009

If I try to use a closure on an event handler the compiler complains with :

Incompatible types: "method pointer and regular procedure"

which I understand.. but is there a way to use a clouser on method pointers? and how to define if can?

eg :

Button1.Onclick = procedure( sender : tobject ) begin ... end;

Thanks!

Best Answer

@Button1.OnClick := pPointer(Cardinal(pPointer( procedure (sender: tObject) 
begin 
  ((sender as TButton).Owner as TForm).Caption := 'Freedom to anonymous methods!' 

end )^ ) + $0C)^;

works in Delphi 2010

Related Topic