我有一个字符串列表,其格式如下:
apple
apple/red
apple/green
apple/green/lime
banana
banana/yellow/dotted
其中每个字符串都是一个节点,而/之后的文本是该节点的子节点。 我试图制作代码,将这种格式的列表转换为treenodes,但我似乎无法解决这个问题。你能提供代码来做到这一点吗?
答案 0 :(得分:1)
尝试这样的事情:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim nodeData As New List(Of String)(New String() { _
"apple", _
"apple/red", _
"apple/green", _
"apple/green/lime", _
"banana", _
"banana/yellow/dotted"})
Dim TN As TreeNode
For Each nodePath As String In nodeData
TN = Nothing
For Each node As String In nodePath.Split("/"c)
If IsNothing(TN) Then
If TreeView1.Nodes.ContainsKey(node) Then
TN = TreeView1.Nodes(node)
Else
TN = TreeView1.Nodes.Add(node, node)
End If
Else
If TN.Nodes.ContainsKey(node) Then
TN = TN.Nodes(node)
Else
TN = TN.Nodes.Add(node, node)
End If
End If
Next
Next
End Sub
答案 1 :(得分:0)
您使用什么编程语言?
编辑:
'assuming you have a treeview on your form,
appleNode = TreeView.appleNode.Nodes.Add("Apple")
bananaNode = TreeView.bananaNode.Nodes.Add("Banana")
appleNodeChild1(0) = appleNode.Nodes.Add("Red") ' creates Apple/Red
appleNodeChild1(1) = appleNode.Nodes.Add("Green") ' creates Apple/Green
appleNodeChild2(0) = appleNodeChild1(1).Nodes.Add("Lime") 'creates Apple/Green/Lime
bananaNodeChild1(0) = bananaNode.Nodes.Add("Yellow") ' creates Banana/Yellow
bananaNodeChild2(0) = bananaNodeChild1(0).Nodes.Add("Dotted") 'creates Banana/Yellow/Dotted