Edit

Share via


MouseButton Enum

Definition

Defines values that specify the buttons on a mouse device.

C#
public enum MouseButton
Inheritance
MouseButton

Fields

Name Value Description
Left 0

The left mouse button.

Middle 1

The middle mouse button.

Right 2

The right mouse button.

XButton1 3

The first extended mouse button.

XButton2 4

The second extended mouse button.

Examples

The following example creates a MouseDown event handler that uses the MouseButton enumeration to determine which button was pressed. Depending on which button was pressed, the background of the control that fired the event is changed.

C#
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;
        }
    }
}

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also