代表用户控制问题 [英] delegate in user control problem

查看:59
本文介绍了代表用户控制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我设计了一个日历用户控件.
它生成一个特定的月份,并且所有日期都是按钮.

当您单击按钮(即1、2..29或30或31)时,此用户控件
您在约会中写东西.
但是我不知道如何在运行时创建事件处理程序,这有可能吗?

Hello,
I have designed a calendar user control.
It generates a specific month, and all the dates are buttons.

This user control, when you click on the button (which is 1,or,2..29 or 30 or 31)
you write in an appointment, or something.
But i dont know how to make an event handler on run time, it it possible ? how to do it ?

推荐答案

每个事件都是运行时的事件,因为它将仅基于特定条件在运行时执行,例如单击控件时,例如;
Every event is an event at runtime because it will only be executed at runtime based on a certain condition for example when a control is clicked eg;
button1.Click += new EventHandler(button1_Click);


如果要在单击控件时处理某些条件,则可以在events方法的主体中执行此操作.

http://msdn.microsoft.com/en-us/library/awbftdfh.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/aa645739%28v = vs.71%29.aspx [ ^ ]

代表和事件 [


If you want to handle certain conditions when a control is clicked, you would do this in the body of the events method.

http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^]

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx[^]

Delegates and Events[^]


向用户公开用户控件的事件,尤其是向用户公开设计师是一个简单的问题,但是许多开发人员对此有疑问并提出问题.我有一个最近得到的综合答案.请参阅:

有关用户控件,嵌套控件和封装的问题 [ ^ ].

另请参阅与OP的富有成果的讨论( Naerling ).这是一个非常重要的重要问题,因此我们俩都花了很多时间讨论所有细节.

—SA
Exposing events of the user control to the user, especially to the designer is a simple problem, but many developers have problems with it and ask questions. I have a comprehensive answer I produced recently. Please see:

A question about usercontrols, nested controls and encapsulation [^].

See also the fruitful discussion with OP (Naerling). It was an excellent and important question, so we both spent good time discussing all the detail.

—SA


问题的一部分是您是否可以在设计时创建事件处理程序.通常,您要做的只是创建一个事件处理程序,它实际上只是一个与事件委托的签名匹配的方法:

Part of your question was if you could make an event handler at design time. Normally all you need to do create an event handler, which is really just a method that matches the signature of the event delegate:

private void Handler (object sender, EventArg arg)



sender参数是.NET中包含的一项很棒的功能,它允许确定发送事件的类.通常,您可能希望将其转换为您知道的类型,可能是一个按钮:



The sender argument is a great feature that is included in .NET that allows the class that sent the event to be determeined. Normally you would want to cast it to the type you know it is, probably a button:

Button b = (Button) sender;



如果要处理多种类型的事件,并且需要使用Is 运算符来确定类型.

具有此强制转换变量可以确定发送方的属性,以便高级处理程序可以为事件做应做的事情.

其他答案提供有关如何将事件附加到此事件处理程序的信息.但是,您不再需要指定



If you are handling events for more than one type and need to use the Is operator to determine the type.

Having this cast variable allows properties of the sender to be determined so that the evant handler can do what it should for the event.

The other answers provide information on how to attach an event to this event handler. Howerver, you do not need to specify the

new EventHandler 

(该功能不在C#的初始发行版中),您需要做的只是:

anymore (feature not in the initual releases of C#), all you need is:

Button.Click += Handler;



有一种方法可以动态地创建事件处理程序,但这需要安装一些名为Roslyn的beta软件代码.



There is a way to actually dynamically create an event handler, but that requires some beta software code named Roslyn to be installed.


这篇关于代表用户控制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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