Treeview Subnode Name - Return Name

S

Some Person

All,

I don't have any problems getting the name or text of a root level node.
But when I try to get the subnode text or name, it always fails or returns
empty depending on code.

How do I get a sub node name?

TIA

Private Sub treeView1_NodeMouseClick(ByVal sender As Object, ByVal e As
TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick

Dim newSelected As TreeNode = e.Node

MsgBox(newSelected.Name.ToString) 'Returns Root Node Level

MsgBox(newSelected.Nodes(0).Name.ToString) 'Crashes app

End Sub
 
M

Morten Wennevik [C# MVP]

Hi,

Your code doesn't take into account that the node selected may not have
child nodes. Change your code to

If newSelected.Nodes.Count > 0 Then
MsgBox(newSelected.Nodes(0).Name.ToString)
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top