MouseButtonEventArgs.ChangedButton Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the button associated with the event.

Namespace:   System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)

public MouseButton ChangedButton { get; }

Property Value

Type: System.Windows.Input.MouseButton

The button which was pressed.

The Mouse class provides additional properties and methods for determining the state of the mouse.

The following example creates a mouse button event handler that changes the color of an object depending on the mouse button which was pressed. The ChangedButton property is checked to determine which button was pressed.

private void MouseButtonDownHandler(object sender, MouseButtonEventArgs e)
{
    Control src = e.Source as Control;

    if (src != null)
    {
        switch (e.ChangedButton)
        {
            case MouseButton.Left:
                src.Background = Brushes.Green;
                break;
            case MouseButton.Middle:
                src.Background = Brushes.Red;
                break;
            case MouseButton.Right:
                src.Background = Brushes.Yellow;
                break;
            case MouseButton.XButton1:
                src.Background = Brushes.Brown;
                break;
            case MouseButton.XButton2:
                src.Background = Brushes.Purple;
                break;
            default:
                break;
        }
    }
}

.NET Framework
Available since 3.0
Return to top
Show: