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.

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
       ' Determine by checking the Node property of the TreeViewEventArgs.
       MessageBox.Show(e.Node.Text)
    End Sub
    
    

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

    private:
       void treeView1_AfterSelect(System::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.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft