WinForms:如何以编程方式触发事件处理程序?

时间:2008-12-16 22:19:25

标签: .net winforms events

我想以编程方式为控件调用事件处理程序。例如:

DateTimePicker dtpLastConsummated;

我想触发dtpLastConsummated的TextChanged事件处理程序,我该怎么做?

在其他语言中,我会称之为:

dtpLastConsummated.TextChanged(this, new EventArgs());

但在.NET中,您可以拥有多个事件处理程序:

dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_TextChanged);
dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_AnotherHandler);
dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_MoreHandlers);
...
dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_Nminus1);

所以你需要一种方法来触发所有附加的事件处理程序。


答案

以下代码将触发事件:

Toolkit.FireEvent(dtpLastConsummated, "TextChanged", new EventArgs());

这是静态工具包函数的代码:

/// <summary>
/// Programatically fire an event handler of an object
/// </summary>
/// <param name="targetObject"></param>
/// <param name="eventName"></param>
/// <param name="e"></param>
public static void FireEvent(Object targetObject, string eventName, EventArgs e)
{
   /*
    * By convention event handlers are internally called by a protected
    * method called OnEventName
    * e.g.
    *     public event TextChanged
    * is triggered by
    *     protected void OnTextChanged
    * 
    * If the object didn't create an OnXxxx protected method,
    * then you're screwed. But your alternative was over override
    * the method and call it - so you'd be screwed the other way too.
    */

   //Event thrower method name //e.g. OnTextChanged
   String methodName = "On" + eventName;

   MethodInfo mi = targetObject.GetType().GetMethod(
         methodName, 
         BindingFlags.Instance | BindingFlags.NonPublic);

   if (mi == null)
      throw new ArgumentException("Cannot find event thrower named "+methodName);

   mi.Invoke(targetObject, new object[] { e });
}

注意:我不是在.NET框架中创建每个控件的子类,而是每个第三方控件,并说服企业值得开发人员改进每个表单以使用我的自定义控件

4 个答案:

答案 0 :(得分:11)

触发TextChanged事件的3条建议:

手动更改文字:

        string s = dateTimePicker1.Text;
        dateTimePicker1.Text = String.Empty;
        dateTimePicker1.Text = s;

从DateTimePicker继承并创建一个公开/调用DateTimePicker的受保护OnTextChanged的新方法

public class MyDateTimePicker : DateTimePicker
{
    public void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
    }
}

如果您不喜欢OOP并希望打破封装,可以通过反射访问受保护的OnTextChanged方法:

        MethodInfo onTextChanged = dateTimePicker1.GetType().GetMethod("OnTextChanged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        onTextChanged.Invoke(dateTimePicker1, new object[] { new EventArgs() });

答案 1 :(得分:8)

Windows窗体中的按钮是一种特殊情况,因为它有一个PerformClick方法来完成您所说的内容。但是,对于其他控件中的其他事件,实际上没什么相似的。

答案 2 :(得分:4)

您可以从该类继承并创建一个调用受保护的OnSomeEvent()方法的新方法。

class MyButton : Button
{
    public void CauseClick()
    {
        this.OnClick();
    }
}

正如Matt所指出的,您可以对此特定示例使用PerformClick()。但是,大多数事件没有相应的公共函数来触发它们。

答案 3 :(得分:0)

以下代码帮我解雇了

ddlTopic_SelectedIndexChanged(this, new EventArgs());

实际活动

private void ddlTopic_SelectedIndexChanged(object sender, EventArgs e)
{
   // do something
}