This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

Capture Right, Middle and "X" Button Clicks

When a user interacts with a Windows Forms application using the mouse, most operations will be controlled with the left (or primary) mouse button. However, it is often important to be able to detect when the user clicks one of the other buttons.

Click Event

The most commonly used event for capturing mouse actions is the Click event. This event is raised whenever the user clicks a Windows Forms control. Unfortunately the event is only raised when the user clicks the control using their primary mouse button. Sometimes you will wish to detect when other mouse buttons are clicked. To achieve this you must use capture alternative events.

NB: For simplicity I will refer to the primary button as the "left button" for the remainder of this article. In reality, the primary button may be changed by the user according to their preferences.

MouseDown and MouseUp Events

Windows Forms controls provide two further events that can be used to determine when the user has pressed or released a mouse button. These are named MouseDown and MouseUp. MouseDown is raised when any button is pressed and MouseUp when a button is released, if certain conditions are met. This differs from Click slightly, as the Click event is raised when the mouse button is pressed and then released.

The MouseDown event does not suit our purpose for this article because it is raised too early in the clicking process when compared with the Click event. With a standard click, the user can depress the left mouse button and then decide not to perform the click by moving the mouse pointer away from the control before releasing the button. This would not be possible with MouseDown.

The MouseUp event is ideal because it will not be raised if the user decides to abort the click. It will also not be raised if the mouse button is depressed before it enters the control in question. Furthermore, the event passes information in a MouseEventArgs object that contains a property named "Button". This property holds an enumeration constant that specifies which button was clicked.

Example Code

To demonstrate the use of the MouseUp event, create a new Windows Forms application and add a button named "TestButton" to the initial form. Use the Visual Studio designer to add the MouseUp event to the button and modify the event's code as follows. This code simply determines the button that was clicked and shows a message.

private void TestButton_MouseUp(object sender, MouseEventArgs e)
    string message = string.Format("{0} button clicked.", e.Button);

Try running the program and clicking the control with each of your mouse buttons. If you have a mouse with five buttons you will be able to detect the usual left, right and middle clicks, as well as the two extra buttons. These are named "XButton1" and "XButton2" in the enumeration.

NB: You should never introduce functionality into an application that can only be accessed using the middle button or the two "X" buttons, as not all users have suitable mice.

8 September 2009