触发器控件的事件以编程方式进行 [英] Trigger control's event programmatically
问题描述
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屋!