TreeView.GetNodeAt Method

Definition

Retrieves the tree node that is at the specified location.

Overloads

GetNodeAt(Point)

Retrieves the tree node that is at the specified point.

GetNodeAt(Int32, Int32)

Retrieves the tree node at the point with the specified coordinates.

GetNodeAt(Point)

Source:
TreeView.cs
Source:
TreeView.cs
Source:
TreeView.cs

Retrieves the tree node that is at the specified point.

C#
public System.Windows.Forms.TreeNode GetNodeAt(System.Drawing.Point pt);
C#
public System.Windows.Forms.TreeNode? GetNodeAt(System.Drawing.Point pt);

Parameters

pt
Point

The Point to evaluate and retrieve the node from.

Returns

The TreeNode at the specified point, in tree view (client) coordinates, or null if there is no node at that location.

Remarks

You can pass the MouseEventArgs.X and MouseEventArgs.Y coordinates of the MouseDown event as the X and Y values of a new Point.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

GetNodeAt(Int32, Int32)

Source:
TreeView.cs
Source:
TreeView.cs
Source:
TreeView.cs

Retrieves the tree node at the point with the specified coordinates.

C#
public System.Windows.Forms.TreeNode GetNodeAt(int x, int y);
C#
public System.Windows.Forms.TreeNode? GetNodeAt(int x, int y);

Parameters

x
Int32

The X position to evaluate and retrieve the node from.

y
Int32

The Y position to evaluate and retrieve the node from.

Returns

The TreeNode at the specified location, in tree view (client) coordinates, or null if there is no node at that location.

Examples

The following code example lets the user edit nonroot tree nodes by using a ContextMenu. When the user right clicks the mouse, the TreeNode at that position is determined and stored in a variable named mySelectedNode. If a nonroot tree node was selected, it is put into an editable state, which allows the user to edit the node label. After the user stops editing the tree node label, the new label text is evaluated and saved. For this example, several characters are considered not valid in the label text. If one of the invalid characters is in the label string, or the string is empty, the user is notified of the error and the label is returned to its previous text.

C#
/* Get the tree node under the mouse pointer and
   save it in the mySelectedNode variable. */
private void treeView1_MouseDown(object sender,
  System.Windows.Forms.MouseEventArgs e)
{
   mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
   if (mySelectedNode != null && mySelectedNode.Parent != null)
   {
      treeView1.SelectedNode = mySelectedNode;
      treeView1.LabelEdit = true;
      if(!mySelectedNode.IsEditing)
      {
         mySelectedNode.BeginEdit();
      }
   }
   else
   {
      MessageBox.Show("No tree node selected or selected node is a root node.\n" +
         "Editing of root nodes is not allowed.", "Invalid selection");
   }
}

private void treeView1_AfterLabelEdit(object sender,
         System.Windows.Forms.NodeLabelEditEventArgs e)
{
   if (e.Label != null)
   {
     if(e.Label.Length > 0)
     {
        if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1)
        {
           // Stop editing without canceling the label change.
           e.Node.EndEdit(false);
        }
        else
        {
           /* Cancel the label edit action, inform the user, and
              place the node in edit mode again. */
           e.CancelEdit = true;
           MessageBox.Show("Invalid tree node label.\n" +
              "The invalid characters are: '@','.', ',', '!'",
              "Node Label Edit");
           e.Node.BeginEdit();
        }
     }
     else
     {
        /* Cancel the label edit action, inform the user, and
           place the node in edit mode again. */
        e.CancelEdit = true;
        MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
           "Node Label Edit");
        e.Node.BeginEdit();
     }
   }
}

Remarks

You can pass the MouseEventArgs.X and MouseEventArgs.Y coordinates of the MouseDown event as the x and y parameters.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9