How can I implement simultaneous pressing of two Button ?

The task is as follows:

  • if Button1 pressed, then when Button1 pressed, the message " Button1 + Button3 " is displayed ;
  • if the Button2 button is pressed, the message " Button2 + Button4 " is displayed when Button4 pressed.

Suppose there is such a code:

 private void Category1_Click(object sender, EventArgs e) { if((Control)sender == Button1) { if((Control)sender == Button3) { MessageBox.Show("Button1+Button3"); } else if((Control)sender == Button4) { MessageBox.Show("Button1+Button4"); } } if((Control)sender == Button2) { if((Control)sender == Button4) { MessageBox.Show("Button2+Button4"); } } } 

The code problem is that the conditional construct if((Control)sender == Button3) does not work (due to the fact that after clicking on Button3 Button1 not pressed). So, you need to make sure that pressing Button1 saved until, for example, Button2 pressed. Please help.

    2 answers 2

    One option is to use Checkbox elements styled as a button .

    Here is an example for button1 and button3

     private void Form1_Load(object sender, EventArgs e) { //Добавил для простоты стилизацию чекбоксов в это место checkBox1.Appearance = Appearance.Button; checkBox3.Appearance = Appearance.Button; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked && checkBox3.Checked) { MessageBox.Show("Button1+Button3"); } } 
    • The easiest and most effective method, imho. - BlackWitcher

    Unfortunately, there is no answer. I had to do it with Bool, that is, if you click on Button1, bool will take the value true. And then you can build the next block.

    • Well, it is logical that way. It will be very good if you bring the code. - Alexander Petrov