14

I am trying to make a minesweeper type game in visual c# and I want to have different things happen when I right click and left click a button, how do I do this?

I have tried this code but it only registers left clicks:

    private void button1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MessageBox.Show("Left");
        }
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            MessageBox.Show("Right");
        }

    }
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
SpencerJL
  • 203
  • 1
  • 2
  • 8
  • You already have the correct answer, so no need to rewrite that. I wrote my own minesweeper a few weeks ago and asked an [SO question](http://stackoverflow.com/questions/8485779/capture-simultaneous-right-and-left-click-event-triggers-on-label) that might help you. It's how to create the "click both mouse buttons at the same time on a number to unveil all the covered boxes surrounding it" function. If you already know how to do this, just ignore me :) – jb. Feb 26 '12 at 03:57
  • Cool, that might come in handy – SpencerJL Feb 26 '12 at 04:33

3 Answers3

13

You will have to use the MouseUp or MouseDown event instead of the Click event to capture right click.

Bala R
  • 107,317
  • 23
  • 199
  • 210
2

Just try with button1_MouseDown event instead of button1_MouseClick Event.It will solve your problem.

 private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
          //do something
        }
        if (e.Button == MouseButtons.Right)
        {
          //do something
        }
    }
Thilina H
  • 5,754
  • 6
  • 26
  • 56
0

Button is reacting only for MouseButtons.Left not for MouseButton.Right and not even for middle.

void Select(object sender, MouseEventArgs e)
{
    /* var btn = sender as CardButton;*/

    if (e.Button == MouseButtons.Left)
    {
        if (this.Selected == false)
        { 
            this.Selected = true;
        }
        else
        {
            this.Selected = false;
        }
    }
    if (e.Button == MouseButtons.Right)
    {
        if (this.Selected == false)
        {
            this.Selected = true;
        }
        else
        {
            this.Selected = false;
        }
    }

    Draw();
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108