Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to do some thing as mentioned below

Button1.click

// some code
button2.click (in button.click event)
Posted

WOW.

Everyone is telling you how to click a button from the event handler of another button.

But, nobody has mentioned that you shouldn't be doing this at all! What you should be doing is moving your code from the event handlers into their own methods. Then you call those methods from the Click event handlers and you also get the benefit of being able to call that code from anywhere else in the form code WITHOUT having to simulate clicking other buttons.
 
Share this answer
 
Comments
Fabio V Silva 13-May-11 9:40am    
My 5.
You can call like this

Button1.Click += new System.EventHandler(this.Button1_Click);
Button2.Click += new System.EventHandler(this.Button2_Click);

private void Button1_Click(object sender, EventArgs e)
{
     //Some Code
     Button2_Click(Button2, e);
}
 
Share this answer
 
Comments
Tarun.K.S 12-May-11 6:44am    
5+.
If I get you right, you want to perform a click on button 2 programmatically, when the user clicks button 1, is that right?

If so:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  'Whatever you want to do

   button1.PerformClick()

End Sub



See this Link[^] for information about PerformClick.
 
Share this answer
 
v3
Comments
Tarun.K.S 12-May-11 6:34am    
In your link the language of the content is in Germany. Updated your solution. :)
And a 5.
Smithers-Jones 12-May-11 7:07am    
Oops. Sitting in a German office, damn this auto-translation thingie. I thought I selected the content to be shown in English. Thanks.
Hi,
you can either invoke the event handler of button2 with in the button1 click as

Button2_Click(nothing,e) or by Button2.PerformClick()

The differences are , in the first case you can re-use the event arguments where it is not possible in the second approach.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900