WinForms / .NET:如何获取附加到事件的事件处理程序? [英] WinForms/.NET: How to get the event handler attached to an event?

查看:184
本文介绍了WinForms / .NET:如何获取附加到事件的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



伪代码,例如:

  public static EventHandler getChangeUICuesEventHandler(Control SomeControl)
{
EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues;
return changeUICuesEventHandler
}

样例用法(伪代码):

  Control control = GetControlByName(Console.ReadLine()); 
button1.ChangeUICues + = GetChangeUICuesEventHandler(control);


解决方案

我提供以下作为替代路径,而不是一个直接的答案...所以这里...



你可以通过确保你的控件与事件处理程序已经附加在一起有所不同,在这种情况下参数 someControl ,而是实现一个定义附加处理程序的接口。例如:

  public interface ChangUICuesHandler 
{
void OnChangeUICues(object sender,EventArgs e);
}

然后你可以这样使用:

  Control control = GetControlByName(Console.ReadLine()); 
if(!(control is ChangeUICuesHandler))
{
throw new Exception(Input Control does not implementation the interface);
}
var handlerControl = control as ChangeUICuesHandler;
button.ChangeUICues + = handler.OnChangeUICues;


How can i get the event handler attached to an event?

pseudocode, for example's sake:

public static EventHandler GetChangeUICuesEventHandler(Control SomeControl)
{
   EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues;
   return changeUICuesEventHandler
}

Sample usage (pseudo-code):

Control control = GetControlByName(Console.ReadLine());
button1.ChangeUICues += GetChangeUICuesEventHandler(control);

解决方案

I offer up the following as an alternative path rather than a direct answer... So here goes...

Can you approach this a little differently perhaps by ensuring that your control with the event handler already attached, in this case the parameter someControl, instead implements an interface that defines the attached handler. For example:

public interface ChangUICuesHandler
{
    void OnChangeUICues(object sender, EventArgs e);
}

Then you could use it like so:

Control control = GetControlByName(Console.ReadLine());
if (!(control is ChangeUICuesHandler))
{
    throw new Exception("Input Control does not implement the interface");
}
var handlerControl = control as ChangeUICuesHandler;
button.ChangeUICues += handler.OnChangeUICues;

这篇关于WinForms / .NET:如何获取附加到事件的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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