#687 – What Happens If You Forget to Release The Mouse

A user interface element can capture the mouse, asking to receive all future mouse events, no matter where the mouse cursor is located.  You’ll typically capture the mouse in a mouse down or related event and then release the mouse in the corresponding mouse up event so that things go back to normal.

But what happens if something goes wrong and you forget to release the mouse?  At that point, the element that captured the mouse will continue to receive mouse input.  This means that other elements in your application that use the mouse will no longer function.

In the example below, the Raphael label captures the mouse on mouse down, but does not release it on mouse up.  Then when I click on the Button, the Raphael label is still getting the mouse events.


#685 – Capturing the Mouse

When you are handling mouse events, the event is generally fired by the control that the mouse is over.  For example, if you hold down the mouse button over one control, move the mouse, and release over another control, the first control will see the MouseDown event and the second will see the MouseUp event.

A control can request that it receive all future mouse events by executing the CaptureMouse event.  When it sees the event it was waiting for, it can then call the ReleaseMouseCapture event to go back to normal behavior.

        private void MouseDown_Raph(object sender, MouseButtonEventArgs e)
            Console.WriteLine("Raphael sees MouseDown");

        private void MouseUp_Raph(object sender, MouseButtonEventArgs e)
            Console.WriteLine("Raphael sees MouseUp");

With the code above, the “Raphael” label captures the mouse on a MouseDown, allowing it to see the corresponding MouseUp event, no matter where the mouse pointer is moved.

#657 – Detecting Double Clicks in User Interface Elements

You can react to a user double-clicking on a user interface element by handling one of the mouse button events and checking the MouseButtonEventArgs.ClickCount field.  When a user double-clicks on an element, all of the mouse down and mouse up events will be fired twice.  During the second round of events, the ClickCount property will have a value of 2.

Here’s an example, where we instrument the MouseDown and MouseUp events for a Label.

When we just click on the element, we see a MouseDown and a MouseUp event, with a ClickCount of 1.

If we double-click on the Label, we’ll see two sets of events.  The second MouseDown event will report a ClickCount of 2.


We could distinguish between double-clicking with the left mouse button vs. the right mouse button by handling either the MouseLeftButtonDown and MouseRightButtonDown events.


#655 – Bubbling Mouse Events Swallowed by Some Controls

When you click a mouse button on a Label control, you’ll see a series of tunneling and bubbling events, as shown below.

However, if you click a mouse button on a TextBox control, you’ll only see some of these events fire.  Specifically, handlers that you define will only see the PreviewXxx versions of the events.  The other events are swallowed by the TextBox control.

In general, controls that do something as the result of a user pressing a mouse button will swallow the related events.  Clicking within a TextBox gives it focus.  Clicking on a Button or ComboBox also results in the control responding to the click, so these controls also swallow the non-preview events.