基于url结构创建treeview节点.net

发布于 2024-12-12 05:59:36 字数 415 浏览 0 评论 0原文

我正在尝试根据这样的目录结构填充树视图节点

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-12-19 05:59:36

您将遇到的第一个问题是基于 for 语句的异常;您应该将其更改为:

For i As Integer = 0 To arrLinks.Length - 1

或者,我的偏好:

For each nodeKey as String in arrLinks

下一个问题是 Nodes 集合不包含整个树中的所有节点,它只包含顶级节点。此列表中的每个节点都有自己的一组子节点,并且每个子节点都有子节点等。

这意味着在添加每个节点时,您需要跟踪最后一个父节点并将下一个子节点添加到该父节点节点或跟踪您要添加到的级别的当前节点集合。

这将产生类似于以下的代码(您可能需要调整 NodeCollection 和 Node 的类名称以及可能的 Add 语句(不要记住 add 是否返回 Node)):

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

The first issue you are going to run into is an exception based on your for statement; you should either change it to:

For i As Integer = 0 To arrLinks.Length - 1

or, my preference:

For each nodeKey as String in arrLinks

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)):

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
策马西风 2024-12-19 05:59:36

未经测试,可能包含语法或拼写错误:

Sub MakeTreeNodes
  Dim tURI As Uri = New Uri("proto://domain.tld/dir1/dir2/dir3")
  Dim tNode as TreeNode = New TreeNode(tURI.DnsSafeHost)

  If 1 < tURI.Segments.Length
    CreateNode(tURI.Segments, 1, tNode)
  End If

  SomeTreeView.Nodex.Add(tNode)

End Sub


Private Sub CreateNode(byval tSegments() As String, ByVal tIndex As Int16, ByRef tParentNode As TreeNode) As TreeNode

  Dim tNode As TreeNode = New TreeNode(tSegments(tIndex))

  If (tSegments.Length - 1) < tIndex
    CreateNode(tSegments, tIndex + 1, tNode)
  End If

  tParentNode.Nodes.Add(tNode)

End Function

简要说明:
MakeTreeNodes() 是入口点。我建议修改它以接受字符串 URL,以及可能的 URI 重载。
它使用 URI 的主机名称创建根节点。

然后它调用递归函数CreateNode()。这将使用当前段创建一个新的 TreeNode,然后调用自身传递新创建的节点和下一个索引值。
对于递归函数来说这是相当标准的。

Untested, may contain syntax or spelling errors:

Sub MakeTreeNodes
  Dim tURI As Uri = New Uri("proto://domain.tld/dir1/dir2/dir3")
  Dim tNode as TreeNode = New TreeNode(tURI.DnsSafeHost)

  If 1 < tURI.Segments.Length
    CreateNode(tURI.Segments, 1, tNode)
  End If

  SomeTreeView.Nodex.Add(tNode)

End Sub


Private Sub CreateNode(byval tSegments() As String, ByVal tIndex As Int16, ByRef tParentNode As TreeNode) As TreeNode

  Dim tNode As TreeNode = New TreeNode(tSegments(tIndex))

  If (tSegments.Length - 1) < tIndex
    CreateNode(tSegments, tIndex + 1, tNode)
  End If

  tParentNode.Nodes.Add(tNode)

End Function

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文