I'm going to assume that you have an Archive
class that looks something like this:
Public Class Archive
Public Property DisplayName As String
Public ReadOnly Property Archives As List(Of Archive)
End Class
and that Archives
in your code is a List(Of Archive)
or something else that implements IEnumerable(Of Archive)
. Just like the nodes of a TreeView
, that is an inherently recursive data structure. As such, recursion is probably the best way to traverse it and build the tree of nodes as you go, e.g.
Private Sub ArchivesToTreeNodes(archives As IEnumerable(Of Archive), nodes As IList)
For Each archive In archives
Dim node As New TreeNode(archive.DisplayName)
ArchivesToTreeNodes(archive.Archives, node.Nodes)
nodes.Add(node)
Next
End Sub
Sample usage:
Dim nodes As New List(Of TreeNode)
ArchivesToTreeNodes(Archives, nodes)
TreeView1.Nodes.AddRange(nodes.ToArray())
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…