42

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

MB_18
  • 1,620
  • 23
  • 37
Danny
  • 896
  • 1
  • 9
  • 16

7 Answers7

57

You should introduce a cast inside the click event handler

MouseEventArgs me = (MouseEventArgs) e;
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 1
    Never known that you could cast an EventArgs to a MouseEventArgs. Could you give a reference about that? – Steve Oct 18 '13 at 11:28
  • No @Steve, I think it is undocumented feature. IIRC I found it when I peek it through reflector :) – Sriram Sakthivel Oct 18 '13 at 11:34
  • 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. – Neil Jun 11 '15 at 07:43
  • 4
    For other readers: this undocumented feature is not available on all controls. This will work for PictureBox, but not for ListView, for example. – nicholas Jun 17 '15 at 15:41
  • 2
    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. – Jamie Sep 22 '16 at 20:37
  • It's rather called "polymorphism", not "terrible idea". :) – Konrad Oct 03 '17 at 08:43
38

You need MouseClick instead of Click event handler, reference.

switch (e.Button) {

    case MouseButtons.Left:
    // Left click
    break;

    case MouseButtons.Right:
    // Right click
    break;
    ...
}
Software Engineer
  • 3,906
  • 1
  • 26
  • 35
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.

ThunderGr
  • 2,297
  • 29
  • 20
  • 3
    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. – Pabinator Aug 01 '14 at 19:40
  • 3
    @Pabinator 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. – ThunderGr Aug 02 '14 at 08:13
5

Use MouseDown event

if(e.Button == MouseButton.Right)
Pang
  • 9,564
  • 146
  • 81
  • 122
2

Use MouseClick event instead of Click

progpow
  • 1,560
  • 13
  • 26
  • I tried that but i got an error, I probably didn't do it right, thanks anyway. – Danny Oct 18 '13 at 11:32
  • 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... – gg89 Jul 12 '15 at 05:49
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 ]

Hassaan Raza
  • 91
  • 1
  • 11
0

This code is true :

MouseEventArgs me = (MouseEventArgs)e;
if (me.Button == MouseButtons.Right)
   //Right Click
else
   //Left Click
Yasin
  • 103
  • 1
  • 1
  • 9