我如何按名称添加一个事件处理程序的事件吗? [英] How can I add an event handler to an event by name?

查看:130
本文介绍了我如何按名称添加一个事件处理程序的事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加的事件处理程序对我的表单中的每个控制。形式是一个简单的信息框会弹出,并点击它的任何地方做同样的事情(有点像O​​utlook的电子邮件通知。)要做到这一点,我写了一个递归方法添加鼠标点击处理每个控制,具体如下:

 私人无效AddMouseClickHandler(调节控制,MouseEventHandler处理器)
{
    control.MouseClick + =处理程序;
    的foreach(控制子控件中control.Controls)
        AddMouseClickHandler(子控件,处理程序);
}
 

不过,如果我想添加一个处理程序为所有的的MouseDown 的MouseUp 的活动,我想必须写两个方法。我敢肯定有解决的办法,但我不能找到它。我要像一个方法:

 私人无效AddRecursiveHandler(调节控制,事件事件,事件处理程序处理)
{
    control.event + =处理程序;
    的foreach(控制子控件中control.Controls)
        AddRecursiveHandler(子控件,事件处理程序);
}
 

解决方案

您可以传递的事件名称为一个字符串,然后使用反射来实现这一点,但是这将是相当难看(和减缓以及)。不幸的是,事件作为参数传递给方法没有别的办法。

不过,你可以写这个优雅的使用lambda函数。而不是写函数添加处理程序,你可以写一个函数来调用一个lambda EX pression各个控制:

 私人无效TraverseControls(调节控制,动作<控制&F)的温度
{
    F(控制);
    的foreach(控制子控件中control.Controls)
        TraverseControls(子控件,F);
}
 

然后你可以使用一个电话解决您原来的问题:

  TraverseControls(形式,CTL => {
  ctl.MouseDown + =处理程序;
  ctl.MouseUp + =处理程序); });
 

在拉姆达EX pression CTL => {...} 将被要求在树中的每个控制和内部拉姆达EX pression,你可以添加处理程序来控制的任何事件。你也可以这样写使用两个电话(加处理程序的MouseDown 中的第一个和的MouseUp 在第二个)。

相较于溶液中使用反射,这是更快,更安全。

I'm attempting to add an event handler for every control on my form. The form is a simple info box which pops up, and clicking anywhere on it does the same thing (sort of like Outlook's email notifier.) To do this, I've written a recursive method to add a MouseClick handler to each control, as follows:

private void AddMouseClickHandler(Control control, MouseEventHandler handler)
{
    control.MouseClick += handler;
    foreach (Control subControl in control.Controls)
        AddMouseClickHandler(subControl, handler);
}

However, if I wanted to add a handler for all of the MouseDown and MouseUp events, I'd have to write two more methods. I'm sure there's a way around this, but I can't find it. I want a method like:

private void AddRecursiveHandler(Control control, Event event, EventHandler handler)
{
    control.event += handler;
    foreach (Control subControl in control.Controls)
        AddRecursiveHandler(subControl, event, handler);
}

解决方案

You could pass the event name as a string and then use Reflection to implement this, but that would be quite ugly (and slow as well). Unfortunately, there is no other way to pass events as arguments to a method.

However, you can write this elegantly using lambda functions. Instead of writing function to add handler, you can write a function that calls a lambda expression for every control:

private void TraverseControls(Control control, Action<Control> f) 
{ 
    f(control); 
    foreach (Control subControl in control.Controls) 
        TraverseControls(subControl, f); 
} 

Then you can solve your original problem using a single call:

TraverseControls(form, ctl => { 
  ctl.MouseDown += handler;
  ctl.MouseUp += handler); });

The lambda expression ctl => { .. } will be called for every control in the tree and inside the lambda expression, you can add handlers to any events of the control. You could also write this using two calls (adding handlers to MouseDown in the first one and to MouseUp in the second one).

Compared to solution using Reflection, this is faster and safer.

这篇关于我如何按名称添加一个事件处理程序的事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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