我可以在事件处理程序上使用闭包(即,TButton OnClick) [英] Can I use a closure on an event handler (ie, TButton OnClick)

查看:206
本文介绍了我可以在事件处理程序上使用闭包(即,TButton OnClick)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在事件处理程序上使用闭包,编译器会抱怨:

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

不兼容的类型:方法指针和常规过程

Incompatible types: "method pointer and regular procedure"

我明白了..但是有没有办法在方法指针上使用clouser?如何定义是否可以?

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

例如:

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

谢谢!

推荐答案

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

end )^ ) + $0C)^;

作品

这篇关于我可以在事件处理程序上使用闭包(即,TButton OnClick)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆