3

I'm having a problem with the treeview I cant find a way to find the click event in each child nodes

Here's a sample image enter image description here

here's what Ive tried so far.

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    If TreeView1.SelectedNode.Level = 0 Then
        MsgBox("1")
    ElseIf TreeView1.SelectedNode.Level = 1 Then
        MsgBox("2")
    ElseIf TreeView1.SelectedNode.Level = 2 Then
        MsgBox("3")
    ElseIf TreeView1.SelectedNode.Level = 3 Then
        MsgBox("4")
    ElseIf TreeView1.SelectedNode.Level = 4 Then
        MsgBox("5")
    End If
End Sub

The problem is in every child node the message box always says it's 2

5
  • So you are saying the level is always 1 ? I had made my own functions while working with TreeViews ... You could make a function that returns the level of the child you just clicked, but its weird that this "basic" function doesnt seem to work in your case Commented Mar 6, 2012 at 10:50
  • Which is perfectly normal because the Level property gets the depth of the current Node in the TreeView. If you select one of the two parent nodes, the MessageBox will show '1'. So please explain further what you want to achieve and what is not working at this time. Commented Mar 6, 2012 at 10:53
  • if(selectedNode.Parent != null) its child node Commented Mar 6, 2012 at 10:55
  • Thanks for your comments guys..What I want is to get click event of every child nodes..do you have any idea? Commented Mar 6, 2012 at 10:56
  • In the image as you can see above how can I know that the child node "Search" is Clicked? Commented Mar 6, 2012 at 11:00

1 Answer 1

6

The event you're using is the good event. If you want to get the new selected node, simply use the EventArgs object in your event handler (parameter named e). Following code is C# not VB.Net, but it's really simple:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    // Use the e parameter to get the new selected node
    MessageBox.Show(e.Node.Text);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.