System.Windows.Forms Namesp ...


.NET Framework Class Library
MouseEventArgs Class

Provides data for the MouseUp, MouseDown, and MouseMove events.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Class MouseEventArgs _
    Inherits EventArgs
Visual Basic (Usage)
Dim instance As MouseEventArgs
C#
[ComVisibleAttribute(true)]
public class MouseEventArgs : EventArgs
Visual C++
[ComVisibleAttribute(true)]
public ref class MouseEventArgs : public EventArgs
JScript
public class MouseEventArgs extends EventArgs
Remarks

The MouseDown event occurs when the user presses the mouse button while the pointer is over a control. The MouseUp event occurs when the user releases the mouse button while the pointer remains over the control. The MouseMove event occurs when the user moves the mouse pointer over a control. A MouseEventArgs specifies which mouse button is pressed, how many times the mouse button was pressed and released, the coordinates of the mouse, and the amount the mouse wheel moved.

It is possible to receive a MouseDown event without a corresponding MouseUp, if the user switches focus to another application before releasing the mouse button.

These three events exist for the Control, AxHost, and NotifyIcon classes.

For information about the event model, see Events and Delegates.

Examples

The following code examples handles the MouseDown event on a TextBox control so that clicking the right mouse button selects all of the text in the control. This example requires that you have a form with a TextBox control named textBox1.

Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.ContextMenu = New ContextMenu()
End Sub

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    If (e.Button = Windows.Forms.MouseButtons.Right) Then
        TextBox1.Select(0, TextBox1.Text.Length)
    End If
End Sub
C#
private void Form1_Load(object sender, EventArgs e)
{
    // This line suppresses the default context menu for the TextBox control. 
    textBox1.ContextMenu = new ContextMenu();
    textBox1.MouseDown += new MouseEventHandler(textBox1_MouseDown);
}

void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        textBox1.Select(0, textBox1.Text.Length);
    }
}

The following code example uses the Location property to track left mouse clicks and draw a series of straight line segments in response to user input. The example does not redraw the lines if you hide the form and then redisplay it; this code has been omitted for simplicity.

Visual Basic
Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False

Private Sub Form1_MouseDownDrawing(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If HaveFirstPoint Then
        Dim g As Graphics = Me.CreateGraphics()
        g.DrawLine(Pens.Black, FirstPoint, e.Location)
        HaveFirstPoint = False
    Else
        FirstPoint = e.Location
        HaveFirstPoint = True
    End If
End Sub
C#
Point firstPoint;
Boolean haveFirstPoint;

public void EnableDrawing()
{
    this.MouseDown += new MouseEventHandler(Form1_MouseDownDrawing);
}

void Form1_MouseDownDrawing(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (haveFirstPoint)
    {
        Graphics g = this.CreateGraphics();
        g.DrawLine(Pens.Black, firstPoint, e.Location);
        haveFirstPoint = false;
    }
    else
    {
        firstPoint = e.Location;
        haveFirstPoint = true;
    }
}

The following code example uses the X and Y properties to display the current position of the mouse pointer in a ToolTip window.

Visual Basic
Dim TrackTip As ToolTip

Private Sub TrackCoordinates()
    TrackTip = New ToolTip()
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    Dim TipText As String = String.Format("({0}, {1})", e.X, e.Y)
    TrackTip.Show(TipText, Me, e.Location)
End Sub
C#
ToolTip trackTip;

private void TrackCoordinates()
{
    trackTip = new ToolTip();
    this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}

void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    String tipText = String.Format("({0}, {1})", e.X, e.Y);
    trackTip.Show(tipText, this, e.Location);
}
Inheritance Hierarchy

System..::.Object
  System..::.EventArgs
    System.Windows.Forms..::.MouseEventArgs
      System.Windows.Forms..::.DataGridViewCellMouseEventArgs
      System.Windows.Forms..::.HandledMouseEventArgs
      System.Windows.Forms..::.StatusBarPanelClickEventArgs
      System.Windows.Forms..::.TreeNodeMouseClickEventArgs
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Tags :


Community Content

MarkusSchaber
Details for mouse events
I'm missing some details here. What I found out due to experimentation are the following facts:
  • MouseUp and MouseDown contain the just the single mouse button whose change of state (pressed or released) generated the event, while MouseMove contains all mouse buttons which are currently being pressed as BitMask.
  • MouseWheel events don't contain any mouse button information at all.
  • MouseUp always is followed by a MouseMove event without moving the mouse.
  • The "Clicks" count on MouseDown events does only cont up to 2, and restarts at 1 again (so it cannot be used to identify triple or quadruple clicks).
  • The "Clicks" count on MouseUp events always seems to be 1.
  • When leaving the control while at least one button is pressed, there is no MouseLeave event, but the MouseMove events continue. You can even press additional buttons and get the MouseDown event and further MouseMove events. But when you release one button while still holding other buttons, you get an MouseUp event, and then don't get any more events until you re-enter the window, where the first event you get is an MouseLeave event, followed by a MouseEnter event, and then the MouseMove events with the now current set of pressed buttons.
Those were tested with Vista 64 on .NET 3.5 - I have no Idea whether this behaviour is guaranteed or may differ on other implementations / versions. These oddities and the accompagnying lack of documentation makes it difficult to implement state machines for complex mouse interactivity. I'd like to see some clearification and possibly justification here.
Tags :

Page view tracker