Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hoe can I get Index or name of child node on TreeView1_KeyDown Event ?

Actually I want to execute code on pressing of Enter Key ?


VB
Private Sub TreeView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TreeView1.KeyDown
        
If e.KeyCode.Equals(Keys.Enter) Then

     MsgBox(TreeView1.Nodes.Item(TreeView1.SelectedNode.Index).ToString)

 End If

 End Sub


This is give only parent node name.

Pls reply...
Posted

1 solution

If you want the text or name of the node you can use this code:
VB
Private Sub TreeView1_KeyDown(sender As Object, e As KeyEventArgs) Handles TreeView1.KeyDown
        If e.KeyCode.Equals(Keys.Enter) Then
            MsgBox(TreeView1.SelectedNode.Text)
            MsgBox(TreeView1.SelectedNode.Name)
        End If
End Sub


For the index number you can use this:
VB
Private Sub TreeView1_KeyDown(sender As Object, e As KeyEventArgs) Handles TreeView1.KeyDown
        If e.KeyCode.Equals(Keys.Enter) Then
            MsgBox(TreeView1.SelectedNode.Index)
        End If
End Sub


Just remember that it only gives it's location relative to the node it resides in. You won't be able to use this index to find the node relative to the root(unless this node resides in the root).
 
Share this answer
 
Comments
SelfCom 13-May-14 0:27am    
Very easy...
The whole 1 day I passed to find its solution. It is a very minor thing, but sometimes not easily getting actual way.

Thank you very much. I was trying to get parent-child index number. But forget it.
My query is solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900