How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?
Asked Answered
T

8

44

I have a method to detect the left click event that visual studio made by double clicking on the form.

private void pictureBox1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Left click");
}

I want to have a right-click event by right-clicking on the same object.

I read online that you can use this switch:

private void pictureBox1_Click(object sender, EventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right){MessageBox.Show("Right click");}
    if (e.Button == System.Windows.Forms.MouseButtons.Left){MessageBox.Show("Left click");}
}

The trouble is that when I do e.Button it has has yields an error error:

System.EventArgs does not contain a definition for Button...

So I fix this by changing the EventArgs.e to MouseEventArgs.e

But then there is a new error in Form1Designer where the event line is:

this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);

The error says:

No overload for pictureBox1_Click matches delegate System.EventHandler

How do I fix this? Thanks for reading

Tress answered 18/10, 2013 at 11:15 Comment(0)
D
58

You should introduce a cast inside the click event handler

MouseEventArgs me = (MouseEventArgs) e;
Derris answered 18/10, 2013 at 11:19 Comment(6)
Never known that you could cast an EventArgs to a MouseEventArgs. Could you give a reference about that?Filberto
No @Steve, I think it is undocumented feature. IIRC I found it when I peek it through reflector :)Derris
This seems strange to me, but most of life does! Regarding using a mouse, I would add a mouse Click event where it passes in MouseEventArgs then you get the button states from there. I have to admit I would not recommend the above as good practice, Just my thoughts on the matter.Scapegrace
For other readers: this undocumented feature is not available on all controls. This will work for PictureBox, but not for ListView, for example.Numismatology
This is a terrible idea. You are begging to be broken when something internal changes. Instead, you should use the solution below and handel MouseClick instead of Click.Northwester
It's rather called "polymorphism", not "terrible idea". :)Mothball
V
41

You need MouseClick instead of Click event handler, reference.

switch (e.Button) {

    case MouseButtons.Left:
    // Left click
    break;

    case MouseButtons.Right:
    // Right click
    break;
    ...
}
Valer answered 18/10, 2013 at 11:19 Comment(1)
Thanks I tried to do it but I probably didn't do it right, thanks for your answerTress
L
25

For me neither the MouseClick or Click event worked, because the events, simply, are not called when you right click. The quick way to do it is:

 private void button1_MouseUp(object sender, MouseEventArgs e)
 {
        if (e.Button == MouseButtons.Right)
        {
            //do something here
        }
        else//left or middle click
        {
            //do something here
        }
 }

You can modify that to do exactly what you want depended on the arguments' values.

WARNING: There is one catch with only using the mouse up event. if you mousedown on the control and then you move the cursor out of the control to release it, you still get the event fired. In order to avoid that, you should also make sure that the mouse up occurs within the control in the event handler. Checking whether the mouse cursor coordinates are within the control's rectangle before you check the buttons will do it properly.

Lepus answered 1/8, 2014 at 13:31 Comment(2)
May be instead of using the MouseUp event use the MouseDown event so you don't have to worry about what you said on your warning.Lu
@Lu The MouseDown is producing unnatural behavior. You expect the action to happen when you release the button, not when you press on it. The warning is given because the event does not work as it should. It is supposed to only fire if the cursor is on the control when you release the button. It must be a .NET glitch or something.Lepus
R
5

Use MouseDown event

if(e.Button == MouseButton.Right)
Rampant answered 25/10, 2018 at 9:30 Comment(0)
T
2

Use MouseClick event instead of Click

Tombstone answered 18/10, 2013 at 11:19 Comment(2)
I tried that but i got an error, I probably didn't do it right, thanks anyway.Tress
I wish MouxeClick event capture right mouse click reliably. but for .net 4.5 compiled with c# visual stuydio 2013 professtional, it always fails for button...Mazzard
T
1

This would definitely help Many!

private void axWindowsMediaPlayer1_ClickEvent(object sender, AxWMPLib._WMPOCXEvents_ClickEvent e)
    {
        if(e.nButton==2)
        {
            contextMenuStrip1.Show(MousePosition);
        }
    }

[ e.nbutton==2 ] is like [ e.button==MouseButtons.Right ]

Throne answered 21/6, 2018 at 19:13 Comment(0)
D
0

This code is true :

MouseEventArgs me = (MouseEventArgs)e;
if (me.Button == MouseButtons.Right)
   //Right Click
else
   //Left Click
Deenadeenya answered 16/12, 2022 at 13:46 Comment(0)
S
0

Using the latest .NET versions (8 for example), you can do it this fun way:

private void MyButton_Click(object sender, EventArgs e)
{
    if (e is MouseEventArgs { Button: MouseButtons.Left })
    {
        // Left click logic here
    }
    else
    {
        // Right or middle click logic here
    }
}

But as someone else suggested, I also recommend using the MouseClick Event instead of the default Click Event. Because it comes with the MouseEventArgs parameter already, and you can use it like so:

private void MyButton_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left) { }
    else if (e.Button == MouseButtons.Right) { }
}

No more casts. :)

Stinnett answered 16/5, 2024 at 11:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.