Raise button (or any control) click event manually. C#

放肆的年华 提交于 2019-11-30 03:10:24

问题


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.


回答1:


You can use the Button.PerformClick method.




回答2:


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.




回答3:


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.




回答4:


button click and to new my manually window ex.. dital.cs open the may window




回答5:


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) {

MyOtherFunction();

//consume the event after if you wish etc

}



来源:https://stackoverflow.com/questions/817343/raise-button-or-any-control-click-event-manually-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!