如何为动态添加的控件创建事件处理程序 [英] How to create Event Handler for Dynamically added control

查看:84
本文介绍了如何为动态添加的控件创建事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时在表单上添加一个新的图片框控件

如何为此控件创建单击事件处理程序

Amit Shinde

解决方案

使用AddHandler


ie。


Addhandler Picturebox1.Click,Addressof

MethodorDelegateThathandlesclickandhassamesignitur e

..com

" Amit D.Shinde" <是******* @ yahoo.com>在消息中写道

news:9a ************************* @ posting.google.co m ... < blockquote class =post_quotes>我在运行时在表单上添加一个新的图片框控件
如何为此控件创建单击事件处理程序
Amit Shinde



" Amit D.Shinde" <是******* @ yahoo.com> schrieb

我在运行时在表单上添加一个新的图片框控件
如何为此控件创建单击事件处理程序




您无法在运行时创建处理程序,您必须在设计时编写它。

您可以_attach_事件处理程序到事件:Addhandler myobject.theevent,

addressof处理程序

-

Armin


Armin,


讨厌在这里不同意你,但是......


你可以在运行时使用委托类创建一个新的委托(它得到了

棘手) ,但你可以),然后你可以将事件等绑定到委托给

目标方法。如果你是如此倾向(即使看起来好像是b
矫枉过正而且没有真正*使用)你可以


1)加载一个组件来抓住一个事件发生...

2)获取要挂钩的事件的签名(.NET中的每个事件

在运行时自动创建一个委托类,你有在调试中观察你的
汇编以查看它,但是如果你遍历getTypes(),你会看到
看到为该事件创建了一个委托。 br />
3)使用CodeDom创建一个类或其他任何东西。

4)编译成一个DLL

5)引用新创建的程序集
6)抓住方法,使用委托创建者挂钩事件和方法。

7)添加处理程序。 =)


现在这就是说你动态地做所有事情(包括创建

方法)。 BUt你可以简单地挂钩另一个匹配

签名的方法(取决于你的严格程度,你可以检查基本类型为

以匹配sig。)而你可以在运行时动态完成所有操作。 =)


-CJ


" Armin Zingler" < AZ ******* @ freenet.de>在消息中写道

news:eA ************** @ TK2MSFTNGP10.phx.gbl ...

" Amit D.辛德" <是******* @ yahoo.com> schrieb

我在运行时在表单上添加一个新的图片框控件
如何为此控件创建单击事件处理程序
你不能在运行时创建一个处理程序,你有写在



designtime。你可以_attach_事件处理程序到一个事件:Addhandler
myobject.theevent,地址处理程序

-
Armin



I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control
Amit Shinde

解决方案

Use AddHandler

ie.

Addhandler Picturebox1.Click, Addressof
MethodorDelegateThathandlesclickandhassamesignitur e
..com
"Amit D.Shinde" <am*******@yahoo.com> wrote in message
news:9a*************************@posting.google.co m...

I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control
Amit Shinde



"Amit D.Shinde" <am*******@yahoo.com> schrieb

I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control



You can not create a handler at runtime, you have to write it at designtime.
You can _attach_ an event handler to an event: Addhandler myobject.theevent,
addressof handler
--
Armin


Armin,

hate to disagree with you here, but...

You can create a new delegate at runtime using the delegate class (it gets
tricky, but you can), and then you can bind events etc to the delegate to a
target method. And if you were so inclined (even though it seems like
overkill and no *real* use) you could

1) Load an assembly to grab an event out of...
2) Get the signiture of the event you want to hook (each event in .NET
automatically creates a delegate class at runtime, you have to watch your
assembly in debug to see it, but if you iterate through getTypes() you''ll
see that there is a delegate created for the event.
3) Use CodeDom to create a class or whatever.
4) compile into a DLL
5) reference the newly created assembly
6) grab the method, use the delegate creator to hook the event and method.
7) Add Handler. =)

Now this is saying you do everything dynamically (including the creation of
a method). BUt you can simply hook to another method that matches the
signiture (depending on how strict you make it,you can check basetypes as
well to match sig.) And you can do this all dynamically at runtime. =)

-CJ

"Armin Zingler" <az*******@freenet.de> wrote in message
news:eA**************@TK2MSFTNGP10.phx.gbl...

"Amit D.Shinde" <am*******@yahoo.com> schrieb

I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control
You can not create a handler at runtime, you have to write it at


designtime. You can _attach_ an event handler to an event: Addhandler myobject.theevent, addressof handler
--
Armin



这篇关于如何为动态添加的控件创建事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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