Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine which mouse button raised the click event in WPF?

Tags:

c#

mouseevent

wpf

I have a button that I trigger OnClick whenever there is a click on that button. I would like to know which Mouse button clicked on that button?

When I use the Mouse.LeftButton or Mouse.RightButton, both tell me "realsed" which is their states after the click.

I just want to know which one clicked on my button. If I change EventArgs to MouseEventArgs, I receive errors.

XAML: <Button Name="myButton" Click="OnClick">

private void OnClick(object sender, EventArgs e)
{
//do certain thing. 
}
like image 867
paradisonoir Avatar asked Mar 01 '23 15:03

paradisonoir


1 Answers

You can cast like below:

MouseEventArgs myArgs = (MouseEventArgs) e;

And then get the information with:

if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
    // do sth
}

The solution works in VS2013 and you do not have to use MouseClick event anymore ;)

like image 152
Sebastian Xawery Wiśniowiecki Avatar answered Mar 04 '23 11:03

Sebastian Xawery Wiśniowiecki