Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a closure on an event handler (ie, TButton OnClick)

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!

like image 810
MrMahgu Avatar asked Dec 11 '08 17:12

MrMahgu


People also ask

Which of the event handlers are supported in button object?

For example, when the user clicks the mouse over a Submit button, three input handlers are triggered: onmousedown, onmouseup, and onclick. And, as a result of this mouse-click, the HTML form that contains the button generates an onsubmit event.

What does a closure allow in JavaScript?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.


1 Answers

@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

like image 184
Codenoid Avatar answered Sep 23 '22 17:09

Codenoid