I am creating a windows store application for which I have to program computer to perform a click on different button after the user has clicked a button. I have Implemented the logic for computer click. But the code b1.Click += btnClick;
doesn't help me to perform a click event on the b1 button
. Please tell how to do so. And please mention the namespace too if any extra to be used.

- 609
- 2
- 11
- 30
-
I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 08 '14 at 03:23
-
1then how would one know that regarding what he has to answer. – Ashwani Shukla Jan 08 '14 at 03:27
-
By the tags, like [tag:c#]. These are the words in rectangles under the question. – John Saunders Jan 08 '14 at 03:28
4 Answers
private void button6_Click(object sender, EventArgs e)
{
//just make sure your button initialized on form!!
this.button7.Click += new EventHandler(button7_Click);
EvenArgs ee = new EventArgs();
button7_Click(this.button7, ee); //this will fire button event!
}

- 13,316
- 6
- 32
- 53
b1.Click += btnClick;
should work for subscribing to a button click event. When the user taps/clicks on the button, the btnClick
handler will be fired.
Now, if I read your question properly, are you asking to perform a button click? If so, you can call the event handler btnClick
yourself: btnClick(this, null);

- 927
- 1
- 8
- 18
-
1
-
If you need it for some reason, you can create a new RoutedEventArgs object and pass it when you call the handler. You probably don't need it though, as it doesn't seem to contain anything useful, so it's probably okay to leave it null. – kdh Jan 08 '14 at 03:37
-
1
b1.Click += btnClick;
b2.Click += btn2Click;
b3.Click += btn3Click;
void btnClick(...)
{
...
// perform a click on different button after the user has clicked a button.
btn2Click(...);
btn3Click(...);
}

- 1,053
- 1
- 11
- 24
-
1not so I use the same function for all button click events. i.e. btnClick – Ashwani Shukla Jan 08 '14 at 03:32
As andrew says you should be able to add multiple events to an event handler like so:
b1.Click += btnClick1;
b1.Click += btnClick2;
For that^ you might want to check if the event handler is already attached to the event like this question explains.
Also as mentioned by both Andrew and Nagaraj you can just call the function from the event handler like:
btnClick2(this, null);
and it will execute the code ( albeit with a little less control since you won't be able to remove it from the click handler without a bunch of extra effort ).
Option three would be to create a function with the desired functionality you want for both buttons and just call that from both the handlers instead of making another button "click". It's more modular and obvious what is happening in your code then.

- 1
- 1

- 1,837
- 2
- 19
- 22