Reputation: 5335
Here i have below Treeview as shown in image.
for child node G g
i want to fetch all the parent nodes name from bottom to top and store into list. means in list should add G g, F f, D d, B b.
please suggest the solution.
thanks in advance...
Upvotes: 2
Views: 10201
Reputation: 5335
I have created one function for it which will add all the parent in the list until parent is not nothing for the selected node.
Public Function AllParents() As List(of String)
Dim listOfNodeHierarchy As New List(Of String)
listOfNodeHierarchy.Add(node.ToString)
'If Child Node has parent, add the parent name of the Child node to the list
While (node.Parent IsNot Nothing)
listOfNodeHierarchy.Add(node.Parent)
node = node.Parent
End While
return listOfNodeHierarchy
End Function
Upvotes: 2
Reputation: 81675
If you just need the list of text of the parent nodes, you can simply use the FullPath property of the selected node:
For Each s As String In _
TreeView1.SelectedNode.FullPath.Split(TreeView1.PathSeparator).Reverse
If you need the list of nodes, just keep checking the parent until it's nothing:
Private Function ParentNodes(fromNode As TreeNode) As IEnumerable(Of TreeNode)
Dim result As New List(Of TreeNode)
While fromNode IsNot Nothing
result.Add(fromNode)
fromNode = fromNode.Parent
End While
Return result
End Function
Upvotes: 0
Reputation: 76
This code will help you to obtain information about the parents node.
dim selectedNode as treeNode = YourTreeViewSelectedNode
dim nodePath as string= "" 'will store the full path
'You get the full Path of the node like 'Parent\Child\GrandChild
'The Procedure will fill nodePath
ObtainNodePath(selectedNode,nodePath)
msgbox(nodePath)
Private Sub ObtainNodePath(ByVal node As TreeNode, ByRef path As String)
If node.Parent IsNot Nothing Then
path = IO.Path.Combine(node.Text, path) ' Parent\Child
If node.Parent.Level = 0 Then Exit Sub
'Call again the method to check if can extract more info
ObtainNodePath(node.Parent, path)
End If
End Sub
Upvotes: 0