Visual C# Form right click button
Asked Answered
L

3

15

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

    }
Limit answered 26/2, 2012 at 3:48 Comment(2)
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 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 :)Infantilism
Cool, that might come in handyLimit
S
13

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

Sillsby answered 26/2, 2012 at 3:51 Comment(1)
Also check that eventArgs.button == MouseButtons.RightSlivovitz
H
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
        }
    }
Houdon answered 4/10, 2013 at 6:2 Comment(0)
E
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();
}
Ellery answered 26/12, 2017 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.