SpencerJL
SpencerJL

Reputation: 213

Visual C# Form right click button

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

    }

Upvotes: 15

Views: 21897

Answers (3)

Vojtěch Kessler
Vojtěch Kessler

Reputation: 1

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

Upvotes: 0

Thilina H
Thilina H

Reputation: 5804

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
        }
    }

Upvotes: 2

Bala R
Bala R

Reputation: 108937

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

Upvotes: 13

Related Questions