C# 手动提升按钮(或任何控件)单击事件。C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/817343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Raise button (or any control) click event manually. C#
提问by Asad Malik
Can anyone tell me how to raise click event of button control (or for that matter for any event).
谁能告诉我如何引发按钮控件的点击事件(或就此而言的任何事件)。
Platform: .net 2.0/3.0/3.5 Language: c# Domain: Windows Application, WinForms, etc.
平台:.net 2.0/3.0/3.5 语言:c# 领域:Windows 应用程序、WinForms 等。
采纳答案by Jorge Villuendas Zapatero
You can use the Button.PerformClickmethod.
您可以使用Button.PerformClick方法。
回答by Mehmet Aras
You can also look into Windows Accessibility or some UI automation framework that allows you to programmatically cause UI controls to respond to user gestures. If a control does not offer a way for its events to be programmatically triggered like PerformClick, you can derive from that control and expose a public method like PerformnXXX that when called internally invokes the event handlers subscribed to a particular event.
您还可以查看 Windows 辅助功能或某些 UI 自动化框架,这些框架允许您以编程方式使 UI 控件响应用户手势。如果控件没有提供像 PerformClick 那样以编程方式触发其事件的方法,您可以从该控件派生并公开一个像 PerformnXXX 这样的公共方法,该方法在内部调用时会调用订阅特定事件的事件处理程序。
回答by Christian Specht
Maybe the solution is much more simple:
也许解决方案要简单得多:
Maybe you don't really want your code "to click the button".
Do you just want to run the code which is behind the button from another place in the form?
也许您真的不希望您的代码“单击按钮”。
您是否只想从表单中的另一个位置运行按钮后面的代码?
If yes, put the code into a separate method (like "DoActionXXX") and call the method from the button and from everywhere else where you need it.
如果是,请将代码放入一个单独的方法中(例如“DoActionXXX”),然后从按钮和需要它的其他任何地方调用该方法。
回答by pankaj
button click and to new my manually window ex.. dital.cs open the may window
按钮单击并新建我的手动窗口,例如..dital.cs 打开五月窗口
回答by j2emanue
I dont think sending click events is the best way from a design point of you just make your event handler call another function. That way you always have access to that function.
我不认为发送点击事件是从设计角度来看最好的方法,只是让你的事件处理程序调用另一个函数。这样您就始终可以访问该功能。
so ..
所以 ..
void myEventHandler(ObjectwhoSentTheMessage O, Event e) {
void myEventHandler(ObjectwhoSentTheMessage O, 事件 e) {
MyOtherFunction();
MyOtherFunction();
//consume the event after if you wish etc
//如果你愿意等之后消费事件
}
}