MouseClick event on a Button

C

Chris Dunaway

I added a MouseClick event to a Button control. It responds properly
to the Left click of the button, but not a middle click or right click.

Is it possible to get a right click and/or middle click for a standard
Button control?
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

In this case, you will want to handle the MouseDown or MouseUp event.
The MouseEventArgs instance that is passed in to your event handler will
have a Button property which will indicate which button was pressed down/up.

Hope this helps.
 
C

Chris Dunaway

Nicholas said:
In this case, you will want to handle the MouseDown or MouseUp event.

Thanks, I used the MouseUp and that worked fine, but I still curious
why the MouseClick event does not work on a button? Since it is
derived from Control, it should support it. Unless it is some Windows
limitation.
 
B

Ben Voigt

Chris Dunaway said:
Thanks, I used the MouseUp and that worked fine, but I still curious
why the MouseClick event does not work on a button? Since it is
derived from Control, it should support it. Unless it is some Windows
limitation.

Button.OnMouseUp, which triggers the MouseClick event, specifically checks
for the left mouse button (disassembled with .NET Reflector):

protected override void OnMouseUp(MouseEventArgs mevent)
{
if ((mevent.Button == MouseButtons.Left) && base.MouseIsPressed)
{
bool flag1 = base.MouseIsDown;
if (base.GetStyle(ControlStyles.UserPaint))
{
base.ResetFlagsandPaint();
}
if (flag1)
{
Point point1 = base.PointToScreen(new Point(mevent.X,
mevent.Y));
if ((UnsafeNativeMethods.WindowFromPoint(point1.X,
point1.Y) == base.Handle) && !base.ValidationCancelled)
{
if (base.GetStyle(ControlStyles.UserPaint))
{
this.OnClick(mevent);
}
this.OnMouseClick(mevent);
}
}
}
base.OnMouseUp(mevent);
}
 
H

Herfried K. Wagner [MVP]

Chris Dunaway said:
Thanks, I used the MouseUp and that worked fine, but I still curious
why the MouseClick event does not work on a button? Since it is
derived from Control, it should support it. Unless it is some Windows
limitation.


I believe the reason is that standard Windows button controls only respond
to a click with the left mouse button.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top