i dont want to click the button. instead, i just want to run the click event code. how would i do this?
-
1The short answer is Yes!abhi– abhi2010-11-08 22:11:17 +00:00Commented Nov 8, 2010 at 22:11
-
1If you have control of the code in the click event handler, the correct thing to do is to extract that code to a separate method that can be called from anywhere.snarf– snarf2010-11-08 22:24:39 +00:00Commented Nov 8, 2010 at 22:24
Add a comment
|
5 Answers
Option: use PerformClick().
Take a look here: How to: Call a Button's Click Event Programmatically.
The advantage here is that the event behavior will be exactly the same as a real button click (as oposed of calling button1_click directly).
But none of this options is the best IMHO. If you need to call the event handler code, you need a new method! Just refactor the old button1_click into a new, standard method and call it from wherever you want.
2 Comments
Powerlord
+1: I've always assumed
PerformClick is better than calling the button's handler directly, as the framework knows better than you do as to anything special it needs to do. It also addresses Ben Voigt's comment about the sender argument.Spooks
+1 for refactor of the old button1_click, makes so much sense
Try a keybd_event with p/invoke http://msdn2.microsoft.com/en-us/library/ms646304(VS.85).aspx