基于url结构创建treeview节点.net
我正在尝试根据这样的目录结构填充树视图节点
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
上面的代码适用于添加基/父节点
说我有一个像这样的urlexample.com/dir1/dir2/file
在这种情况下,它应该在父节点 dir1 中创建一个子节点 dir2
我很困惑将子节点添加到现有节点
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将遇到的第一个问题是基于 for 语句的异常;您应该将其更改为:
或者,我的偏好:
下一个问题是 Nodes 集合不包含整个树中的所有节点,它只包含顶级节点。此列表中的每个节点都有自己的一组子节点,并且每个子节点都有子节点等。
这意味着在添加每个节点时,您需要跟踪最后一个父节点并将下一个子节点添加到该父节点节点或跟踪您要添加到的级别的当前节点集合。
这将产生类似于以下的代码(您可能需要调整 NodeCollection 和 Node 的类名称以及可能的 Add 语句(不要记住 add 是否返回 Node)):
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)):
未经测试,可能包含语法或拼写错误:
简要说明:
MakeTreeNodes() 是入口点。我建议修改它以接受字符串 URL,以及可能的 URI 重载。
它使用 URI 的主机名称创建根节点。
然后它调用递归函数CreateNode()。这将使用当前段创建一个新的 TreeNode,然后调用自身传递新创建的节点和下一个索引值。
对于递归函数来说这是相当标准的。
Untested, may contain syntax or spelling errors:
Brief explanation:
MakeTreeNodes() is the entry point. I would suggest modifying it to accept a string URL, as well as possibly an overload for a URI.
It creates a root node with the host anme of the URI.
Then it calls the recursive function CreateNode(). This creates a new TreeNode with the current segment, and then calls itsself passing the newly created node and the next index value.
It's pretty standard faire for a recursive function.