am trying to pupulate a treeview nodes base on directory structure like this
Dim arrLinks() As String = Split(Url, "/")
For i As Integer = 0 To arrLinks.Length
If tvwDirs.Nodes.ContainsKey(arrLinks(0)) = False Then
tvwDirs.Nodes.Add(arrLinks(0), arrLinks(0))
End If
Next
The above code works for add base/parent node
say i have a urllike this example.com/dir1/dir2/file
in this case, it should create a child node dir2 in parent node dir1
am getting confused add child nodes to the existing nodes
The first issue you are going to run into is an exception based on your for statement; you should either change it to:
or, my preference:
The next issue is that the Nodes collection does not contain all of the Nodes in the entire tree, it only contains the top level nodes. Each node in this list has its own set of child nodes and each of those children has child nodes, etc.
This means that as you add each node, you need to keep track of the last parent node and add the next child to that parent node or keep track of the current collection of nodes for the level that you are adding to.
This will result in code similar to the following (you may need to adjust the class names for NodeCollection and Node and possible the Add statement (don’t remember off the top if add returns a Node or not)):