tags:

views:

437

answers:

2

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. 
}
A: 

You're right, Jose, it's with MouseClick event. But you must add a little delegate:

this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

And use this method in your form:

    private void MyMouseDouwn(object sender, MouseEventArgs e) 
    {
        if (e.Button == MouseButtons.Right)
           this.Text = "Right";

        if (e.Button == MouseButtons.Left)
            this.Text = "Left";
    }
yelinna
This is not correct for WPF, you'd need to use a MouseButtonEventArgs, and it doesn't have a button property, but instead every button's state.
rmoore
but even with private void OnClick(object sender, MouseButtonEventArgs e), it gives me the same error of error of "No overload for 'OnClick' matches delegate 'System.Windows.RoutedEventHandler'
paradisonoir
I deleted the OnClick delegate.
yelinna
+1  A: 

If you're just using the Button's Click event, then the only mouse button that will fire it is the primary mouse button.

If you still need to know specifically whether it was the left or right button, then you can use the SystemInformation to obtain it.

void OnClick(object sender, RoutedEventArgs e)
 {
  if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped
  {
   // It's the right button.
  }
  else
  {
   // It's the standard left button.
  }
 }

Edit: The WPF equivalent to SystemInformation is SystemParameters, which can be used instead. Though you can include System.Windows.Forms as a reference to obtain the SystemInformation without adversely effecting the application in any way.

rmoore
thanks for the hint.Though mine is not a System.Windows.Form.It's an XAML window (WPF).
paradisonoir
... yes, all my development is in XAML or Silverlight. This is for WPF, as you can clearly see by the RoutedEventArgs, System.Windows.Forms.Systeminformation is just the best and quickest way to get the Computer's system information in WPF.
rmoore
Yes it worked very well.
paradisonoir