Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
like image 478
SpencerJL Avatar asked Feb 26 '12 03:02

SpencerJL


2 Answers

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

like image 158
Bala R Avatar answered Sep 22 '22 09:09

Bala R


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
        }
    }
like image 45
Thilina H Avatar answered Sep 24 '22 09:09

Thilina H