How can i know selectednode of treeview is rootnode or childnode or child of childnode.
Printable View
How can i know selectednode of treeview is rootnode or childnode or child of childnode.
Have you read the documentation for the TreeNode class? If you have then you know what properties it has so you know the answer to your question.
I read that document so i can solve that problem like this :
But i want to check all nodes for determine what class of node does it belong to ?Code:Dim Count As Integer = mytreeview.SelectedNode.GetNodeCount(True)
if count = 0 then
Msgbox(mytreeview.SelectedNode.FullPath & " is last child node")
End if
So, are you saying that you don't know how to traverse the tree? If so then the answer is with recursion. A tree is an inherently recursive structure so recursion is the best way to traverse a tree is using recursion. In the case of a TreeView you can do it like so:Obviously displaying the full path in a message box is just an example but you can process the node in any way you see fit. You would start the ball rolling by call that method and passing the Nodes property of your TreeView. It will then visit every node in the tree.vb.net Code:
Private Sub ProcessNodes(ByVal nodes As TreeNodeCollection) For Each node As TreeNode In nodes 'Process node, e.g. MessageBox.Show(node.FullPath) 'Process child nodes. Me.ProcessNodes(node.Nodes) Next End Sub
Note that that code will perform a depth-first search, i.e. the node's children are processed before it siblings. It's the most natural so it should be your first choice. If you specifically need a breadth-first search though:You might also want to take note of the TreeNode.Level property, which is included in the documentation.vb.net Code:
Private Sub ProcessNodes(ByVal nodes As TreeNodeCollection) For Each node As TreeNode In nodes 'Process node, e.g. MessageBox.Show(node.FullPath) Next For Each node As TreeNode In nodes 'Process child nodes. Me.ProcessNodes(node.Nodes) Next End Sub