1

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.

Ashwani Shukla
  • 609
  • 2
  • 11
  • 30

4 Answers4

2
   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!
    }
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
1

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

kdh
  • 927
  • 1
  • 8
  • 18
0
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(...);
}
Kelmen
  • 1,053
  • 1
  • 11
  • 24
0

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.

Community
  • 1
  • 1
user1567453
  • 1,837
  • 2
  • 19
  • 22