Export (0) Print
Expand All
Expand Minimize
0 out of 3 rated this helpful - Rate this topic

Control.MousePosition Property

Gets the position of the mouse cursor in screen coordinates.

[Visual Basic]
Public Shared ReadOnly Property MousePosition As Point
[C#]
public static Point MousePosition {get;}
[C++]
public: __property static Point get_MousePosition();
[JScript]
public static function get MousePosition() : Point;

Property Value

A Point that contains the coordinates of the mouse cursor relative to the upper-left corner of the screen.

Remarks

The MousePosition property returns a Point that represents the mouse cursor position at the time the property was referenced.

Example

[Visual Basic, C#, C++] The following example puts a TreeNode label into an editble state when the user presses ALT-E while the mouse cursor is over the tree node. After the user is done editing the label, the labels cannot be edited again until the ALT-E key combination is pressed again. This example assumes you have a TreeView on a Form. The tree view should also have at least one TreeNode in its Nodes collection.

[Visual Basic] 
Private Sub treeView1_KeyDown(sender As Object, _
  e As KeyEventArgs) Handles treeView1.KeyDown
   ' If the 'Alt' and 'E' keys are pressed,
   ' allow the user to edit the TreeNode label. 
   If e.Alt And e.KeyCode = Keys.E Then
      treeView1.LabelEdit = True
      ' If there is a TreeNode under the mose cursor, begin editing. 
      Dim editNode As TreeNode = treeView1.GetNodeAt( _
        treeView1.PointToClient(Control.MousePosition))
      If Not (editNode Is Nothing) Then
         editNode.BeginEdit()
      End If
   End If
End Sub

Private Sub treeView1_AfterLabelEdit(sender As Object, _
  e As NodeLabelEditEventArgs) Handles treeView1.AfterLabelEdit
   ' Disable the ability to edit the TreeNode labels.
   treeView1.LabelEdit = False
End Sub

[C#] 
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
   /* If the 'Alt' and 'E' keys are pressed,
      * allow the user to edit the TreeNode label. */
   if(e.Alt && e.KeyCode == Keys.E)
         
   {
      treeView1.LabelEdit = true;
      // If there is a TreeNode under the mose cursor, begin editing. 
      TreeNode editNode = treeView1.GetNodeAt(
         treeView1.PointToClient(Control.MousePosition));
      if(editNode != null)
      { 
         editNode.BeginEdit();
      }
   }
}

private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
   // Disable the ability to edit the TreeNode labels.
   treeView1.LabelEdit = false;
}

[C++] 
private:
    void treeView1_KeyDown(Object* /*sender*/, KeyEventArgs* e) {
        /* If the 'Alt' and 'E' keys are pressed,
         * allow the user to edit the TreeNode label. */
        if (e->Alt && e->KeyCode == Keys::E) {
            treeView1->LabelEdit = true;
            // If there is a TreeNode under the mose cursor, begin editing.
            TreeNode* editNode = treeView1->GetNodeAt(
                treeView1->PointToClient(Control::MousePosition));
            if (editNode != 0) {
                editNode->BeginEdit();
            }
        }
    }

    void treeView1_AfterLabelEdit(Object* /*sender*/, NodeLabelEditEventArgs* /*e*/) {
        // Disable the ability to edit the TreeNode labels.
        treeView1->LabelEdit = false;
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Control Class | Control Members | System.Windows.Forms Namespace | Point

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.