使用项目列表填充Treeview

时间:2017-01-30 15:59:19

标签: vb.net treeview

美好的一天,

我有两个对象列表(比如说MyObjList作为新列表(整数)),并根据它们的索引我想建立一个从项目0开始的树视图,并以列表中的最后一项结束。我希望这些对象看起来像下面的东西。我可以添加第一个节点,但之后不能计算如何将下一个节点添加为每个先前添加的节点的子节点(如果这是有意义的话)。非常感谢你的帮助。 我附上了一张图片,以便根据以下答案更好地理解我希望它看起来与我目前得到的内容相比。treeview populated with two lists of objects

0-->
    1
     --> 
        2
         -->
           3--> 
               4


      Dim myObjList As New List(Of integer)
            myObjList.Add("0")
            myObjList.Add("1")
            myObjList.Add("2")
            myObjList.Add("3")
            myObjList.Add("4")

我们能做到吗?

Can we achieve this

2 个答案:

答案 0 :(得分:3)

如果我做对了,你试图添加Nodes,以便TreeView看起来像这样:

enter image description here

这就是手动的样子:

TreeView1.Nodes.Add(list(0).ToString())
TreeView1.Nodes(0).Nodes.Add(list(1).ToString())
TreeView1.Nodes(0).Nodes(0).Nodes.Add(list(2).ToString())
TreeView1.Nodes(0).Nodes(0).Nodes(0).Nodes.Add(list(3).ToString())
TreeView1.Nodes(0).Nodes(0).Nodes(0).Nodes(0).Nodes.Add(list(4).ToString())

这当然是一种真正的痛苦,并且不具备可扩展性。我要看的是递归地Nodes添加到TreeView,如下所示:

Public Class Form1

    Dim list As New List(Of Integer)

    Private Sub AddMoreChildren(ByVal parent As TreeNode)

        If parent.Level < list.Count - 1 Then
            Dim child As New TreeNode(list(parent.Level + 1).ToString())

            parent.Nodes.Add(child)

            AddMoreChildren(child)
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        list.Add(0)
        list.Add(1)
        list.Add(2)
        list.Add(3)
        list.Add(4)

        Dim root As New TreeNode(list.First.ToString())
        AddMoreChildren(root)
        TreeView1.Nodes.Add(root)

    End Sub

End Class

从上面的代码中,您将获得与屏幕截图中显示的相同的结果。

这样做的好处是代码会继续致电AddMoreChildren,直到List中的所有项目都被添加为止。

已修改合并两个列表

如果您有两个列表,那么我要做的就是将它们组合在一起,使用ConcatDistinct创建一个独特的List

  

请注意,我已将List更改为此示例的String类型,并且在设置值时不再需要使用.ToString

Public Class Form1

    Dim firstList As New List(Of String)
    Dim secondList As New List(Of String)
    Dim mainList As New List(Of String)

    Private Sub AddMoreChildren(ByVal parent As TreeNode)

        If parent.Level < mainList.Count - 1 Then
            Dim child As New TreeNode(mainList(parent.Level + 1))

            parent.Nodes.Add(child)

            AddMoreChildren(child)
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        firstList.Add("S")
        firstList.Add("A3")
        firstList.Add("U13")
        firstList.Add("ABC")
        firstList.Add("PQ")
        firstList.Add("1234")

        secondList.Add("S")
        secondList.Add("A3")
        secondList.Add("U13")
        secondList.Add("XYZ")
        secondList.Add("ST")
        secondList.Add("5678")

        mainList = firstList.Concat(secondList).Distinct().ToList()

        Dim root As New TreeNode(mainList.First)
        AddMoreChildren(root)
        TreeView1.Nodes.Add(root)

    End Sub

End Class

这将为您提供以下输出:

enter image description here

根据OP的更新进行编辑

这是OP基于更新后的内容:

enter image description here

这需要更多的代码,并且不是很整洁,但确实可以完成这项任务。在这里我必须引入另一个名为index的变量。这纯粹是为了跟踪mainList中的当前索引,而不是使用If parent.Level

我还必须操纵mainList,所以我会试着解释一下。

首先让我们使用Intersect。这用于获取在firstListsecondList中找到值的列表,其中包含 S A3 U13 < /强>:

Dim root As TreeNode
mainList = firstList.Intersect(secondList).ToList()
root = New TreeNode(mainList.First)

现在我们已经将这些内容添加到TreeView

index = 1
AddMoreChildren(root)
TreeView1.Nodes.Add(root)

我将index设置为 1 ,因为我已经拥有根节点,并且当我进入AddMoreChildren时想要从 A3 开始。 AddMoreChildren方法也发生了轻微变化:

Private Sub AddMoreChildren(ByVal parent As TreeNode)

    If index < mainList.Count Then
        Dim child As New TreeNode(mainList(index).ToString())

        parent.Nodes.Add(child)

        index += 1

        AddMoreChildren(child)
    End If

End Sub
  

请注意现在使用index代替parent.Level

接下来,由于您想要从 U13 分支出来,我们需要获取此索引。这很简单:

Dim indexOfLastNode As Integer = mainList.Count - 1

接下来,我们将 U13 设置为根节点:

root = GetLastAddedChildNode(TreeView1.Nodes(0), indexOfLastNode)

GetLastAddedChildNode的代码是:

Private Function GetLastAddedChildNode(ByVal parentNode As TreeNode,
                                       ByVal level As Integer) As TreeNode

    Dim newNode As New TreeNode

    For Each node As TreeNode In parentNode.Nodes
        If node.Level = level Then
            newNode = node
            Exit For
        End If
        newNode = GetLastAddedChildNode(node, level)
    Next

    Return newNode

End Function

然后我们操纵mainList,以便我们从firstList获取secondList中不属于AddMoreChildren的所有值。这使我们得到 ABC PQ 1234 。我们使用Except来完成此操作。然后,我们可以拨打index并将 ABC PQ 1234 添加到 U13

  

注意,在致电AddMoreChildren之前,不要忘记将mainList重置为0,以便我们可以添加secondList中的所有项目。

然后我们使用Except Public Class Form1 Dim firstList As New List(Of String) Dim secondList As New List(Of String) Dim mainList As New List(Of String) Dim index As Integer Private Sub AddMoreChildren(ByVal parent As TreeNode) If index < mainList.Count Then Dim child As New TreeNode(mainList(index).ToString()) parent.Nodes.Add(child) index += 1 AddMoreChildren(child) End If End Sub Private Function GetLastAddedChildNode(ByVal parentNode As TreeNode, ByVal level As Integer) As TreeNode Dim newNode As New TreeNode For Each node As TreeNode In parentNode.Nodes If node.Level = level Then newNode = node Exit For End If newNode = GetLastAddedChildNode(node, level) Next Return newNode End Function Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load firstList.Add("S") firstList.Add("A3") firstList.Add("U13") firstList.Add("ABC") firstList.Add("PQ") firstList.Add("1234") secondList.Add("S") secondList.Add("A3") secondList.Add("U13") secondList.Add("XYZ") secondList.Add("ST") secondList.Add("5478") Dim root As TreeNode mainList = firstList.Intersect(secondList).ToList() root = New TreeNode(mainList.First) index = 1 AddMoreChildren(root) TreeView1.Nodes.Add(root) Dim indexOfLastNode As Integer = mainList.Count - 1 If TreeView1.Nodes.Count = 1 Then root = GetLastAddedChildNode(TreeView1.Nodes(0), indexOfLastNode) mainList = firstList.Except(secondList).ToList() If root.Text.Equals(firstList(indexOfLastNode)) Then index = 0 AddMoreChildren(root) mainList = secondList.Except(firstList).ToList() index = 0 AddMoreChildren(root) End If End If End Sub End Class 执行相同的操作。这是一个完整的完整代码。它可能更有意义:

{{1}}

以下是输出的屏幕截图:

enter image description here

答案 1 :(得分:1)

为您整理此代码:

Dim tmpNode As TreeNode = Nothing

For Each num In myObjList.Reverse
    Dim newNode = New TreeNode(num.ToString)

    If tmpNode IsNot Nothing Then
        newNode.Nodes.Add(tmpNode)
    End If

    tmpNode = newNode
Next

反过来浏览列表并将每个节点添加为最后一个节点的子节点。

编辑:这个也是,也不需要撤销列表

Dim topNode As TreeNode = Nothing
Dim latestNode As TreeNode = Nothing

For Each num In myObjList
    Dim tmpNode = New TreeNode(num.ToString)

    If myObjList.IndexOf(num) = 0 Then
        topNode = tmpNode
        latestNode = topNode
    Else
        latestNode.Nodes.Add(tmpNode)
        latestNode = tmpNode
    End If
Next