Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
Control Class
Control Events
 MouseCaptureChanged Event
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
Control..::.MouseCaptureChanged Event

Updated: September 2010

Occurs when the control loses mouse capture.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
Public Event MouseCaptureChanged As EventHandler
C#
public event EventHandler MouseCaptureChanged
Visual C++
public:
 event EventHandler^ MouseCaptureChanged {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
F#
member MouseCaptureChanged : IEvent<EventHandler,
    EventArgs>

In rare scenarios, you might need to detect unexpected input. For example, consider the following scenarios.

  • During a mouse operation, the user opens the Start menu by pressing the Windows key or CTRL+ESC.

  • During a mouse operation, the user switches to another program by pressing ALT+TAB.

  • During a mouse operation, another program displays a window or a message box that takes focus away from the current application.

Mouse operations can include clicking and holding the mouse on a form or a control, or performing a mouse drag operation. If you have to detect when a form or a control loses mouse capture for these and related unexpected scenarios, you can use the MouseCaptureChanged event.

The following code example demonstrates the MouseCaptureChanged event for a Button control.

Visual Basic
Private Sub Button1_MouseDown(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) Handles Button1.MouseDown
    Debug.WriteLine("Button1_MouseDown")
End Sub

Private Sub Button1_MouseUp(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) Handles Button1.MouseUp
    Debug.WriteLine("Button1_MouseUp")
End Sub

Private Sub Button1_MouseCaptureChanged(ByVal sender As System.Object, _
ByVal e As EventArgs) Handles Button1.MouseCaptureChanged
    Debug.WriteLine("Button1_MouseCaptureChanged")
End Sub
C#
private void button1_MouseDown(object sender, MouseEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("button1_MouseDown");
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("button1_MouseUp");
}

private void button1_MouseCaptureChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("button1_MouseCaptureChanged");
}

To test this example, run it in the debugger by pressing F5. Open the Output window in Visual Studio so that you can see when events are raised. Click the Button and notice the following output.

button1_MouseDown

button1_MouseUp

button1_MouseCaptureChanged

Now, click and hold the left mouse button on the Button control. While still clicking the mouse, press ALT+TAB to switch to another program. Notice that the MouseCaptureChanged event is raised enabling you to potentially handle this scenario. Depending on your actions, the MouseUp event might not be raised. You can also try this test with the Windows key or CTRL+ESC.

button1_MouseDown

button1_MouseCaptureChanged

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

September 2010

Added more detail about when this event occurs.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker