触发器控件的事件以编程方式进行 [英] Trigger control's event programmatically

查看:114
本文介绍了触发器控件的事件以编程方式进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个WinFoms项目。只有一个按钮(例如 button1 )。



问题是:是否可以通过代码触发 ButtonClicked 事件,而不用真正点击它?

解决方案

.NET框架使用一种模式,对于每个事件 X 方法 protected void OnX(EventArgs e){} 引发事件 X 。请参阅此 Msdn文章。要从声明类外部引发事件,您将必须派生类并添加公共包装器方法。在 Button 的情况下,它将如下所示:

  class MyButton :System.Windows.Forms.Button 
{

public void ProgrammaticClick(EventArgs e)
{
base.OnClick(e);
}

}


Assume that I have a WinFoms project. There is just one button (e.g. button1).

The question is: is it possible to trigger the ButtonClicked event via code without really clicking it?

解决方案

The .NET framework uses a pattern where for every event X there is a method protected void OnX(EventArgs e) {} that raises event X. See this Msdn article. To raise an event from outside the declaring class you will have to derive the class and add a public wrapper method. In the case of Button it would look like this:

class MyButton : System.Windows.Forms.Button
{

    public void ProgrammaticClick(EventArgs e)
    {
        base.OnClick(e);
    }

}

这篇关于触发器控件的事件以编程方式进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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