Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# is it possible to trigger the button_click event with code?

Tags:

c#

.net

winforms

i dont want to click the button. instead, i just want to run the click event code. how would i do this?

like image 647
Alex Gordon Avatar asked Nov 08 '10 22:11

Alex Gordon


3 Answers

button1_click(null, new EventArgs() );
like image 74
cichy Avatar answered Oct 05 '22 23:10

cichy


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.

like image 34
rsenna Avatar answered Oct 06 '22 01:10

rsenna


From inside or outside the Form where the Button is? Simplest thing is to just call the function:

Button1_Click(Button1, EventArgs.Empty);
like image 32
Ben Voigt Avatar answered Oct 06 '22 00:10

Ben Voigt