您将要遇到的第一个问题是基于您的for语句的异常;您应该将其更改为:
For i As Integer = 0 To arrLinks.Length - 1
,或者我的偏好:
For each nodeKey as String in arrLinks
下一个问题是,节点集合不包含所有在整个树中的节点,它仅包含顶级节点。此列表中的每个节点都有自己的一组子节点,每个子节点都有子节点等。
这意味着当您添加每个节点时,需要跟踪最后一个父节点并添加下一个子节点添加到该父节点或跟踪要添加到的级别的当前节点集合。
这将导致代码类似于以下内容(您可能需要调整NodeCollection和Node的类名称以及可能的Add语句(如果添加返回节点,则不记得顶部)):
Dim arrLinks() As String = Split(Url, "/")
Dim cNodes as NodeCollection
' Keep track of the current collection of nodes, starting with the tree's top level collection
cNodes = tvwDirs.Nodes
For each nodeKey As String in arrLinks
Dim currentNode as Node
If Not cNodes.ContainsKey(nodeKey) Then
' If the key is not in the current collection of nodes, add it and record the resultant record
currentNode = cNodes.Add(nodeKey, nodeKey)
Else
' Otherwise, record the current node
currentNode = cNodes(nodeKey)
End If
' Store the set of nodes that the next nodeKey will be added to
cNodes = currentNode.Nodes
Next
感谢您的帮助,我需要添加图像到树基于节点级别的视图,具有子节点的任何节点都将是文件夹,并且没有文件/感谢 – Smith