Export (0) Print
Expand All

How to: Determine Which TreeView Node Was Clicked (Windows Forms)

When working with the Windows Forms TreeView control, a common task is to determine which node was clicked, and respond appropriately.

To determine which TreeView node was clicked

  1. Use the EventArgs object to return a reference to the clicked node object.

  2. Determine which node was clicked by checking the TreeViewEventArgs class, which contains data related to the event.

    protected void treeView1_AfterSelect (object sender, 
    System.Windows.Forms.TreeViewEventArgs e)
    {
       // Determine by checking the Text property.
       MessageBox.Show(e.Node.Text);
    }
    
    NoteNote

    As an alternative, you can use the MouseEventArgs of the MouseDown or MouseUp event to get the X and Y coordinate values of the Point where the click occurred. Then, use the TreeView control's GetNodeAt method to determine which node was clicked.

Show:
© 2014 Microsoft