Share via


Check if right-mouse click ?

Question

Sunday, February 19, 2012 6:32 PM

Hi! I'm trying to write a program, I'm fairly new to c#....could anyone help me figure out how to check if the mouse is "right clicked" in an if statment?

if (right mouse click)

{

code

}

Thanks!

All replies (2)

Sunday, February 19, 2012 6:41 PM ✅Answered | 1 vote

You need to handle the MouseDown event for the object to which you want to control the mouse click and check the e.Button property. For example, to check right click on a form:

private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
         // Right cliclk.
     }
}

Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva


Sunday, February 19, 2012 7:08 PM ✅Answered

Hello Pumplead,

Hi! I'm trying to write a program, I'm fairly new to c#....could anyone help me figure out how to check if the mouse is "right clicked" in an if statment?

if (right mouse click)

{

code

}

Thanks!

add to the excellent suggestion of Marco the MouseClick event.

private void Form1_MouseClick(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button.Equals(MouseButtons.Right)) {
        MessageBox.Show(e.Button.ToString());
    }
}

Regards.