locked
Check if right-mouse click ? RRS feed

  • Question

  • 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!

    Sunday, February 19, 2012 6:32 PM

Answers

  • 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

    • Proposed as answer by Dummy yoyo Monday, February 20, 2012 9:16 AM
    • Marked as answer by Dummy yoyo Wednesday, February 29, 2012 2:40 AM
    Sunday, February 19, 2012 6:41 PM
  • 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.

    • Proposed as answer by Dummy yoyo Monday, February 20, 2012 9:16 AM
    • Marked as answer by Dummy yoyo Wednesday, February 29, 2012 2:40 AM
    Sunday, February 19, 2012 7:08 PM

All replies

  • 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

    • Proposed as answer by Dummy yoyo Monday, February 20, 2012 9:16 AM
    • Marked as answer by Dummy yoyo Wednesday, February 29, 2012 2:40 AM
    Sunday, February 19, 2012 6:41 PM
  • 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.

    • Proposed as answer by Dummy yoyo Monday, February 20, 2012 9:16 AM
    • Marked as answer by Dummy yoyo Wednesday, February 29, 2012 2:40 AM
    Sunday, February 19, 2012 7:08 PM