且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

触发器控件的事件以编程方式进行

更新时间:2023-11-30 16:01:22

.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);
    }

}