How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?
You should introduce a cast inside the click
event handler
MouseEventArgs me = (MouseEventArgs) e;
You need MouseClick
instead of Click
event handler, reference.
switch (e.Button) {
case MouseButtons.Left:
// Left click
break;
case MouseButtons.Right:
// Right click
break;
...
}
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.