Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - Loading unknown amount of childs to TreeNode

I'm getting data from an API where archives are stored for each user. Each archive can have 1 to unlimited sub archives and each sub archive can have 1 to ulimited sub archives and so on.

How can I load every archive and their sub archives into a TreeNode without 400 loops. I thought about recursion but I'm not sure if there is a better way to do it.

VB Example:

Sub Main()
    For Each archive in Archives
        TreeView1.Nodes.Add(AllChilds(archive))
    Next
End Sub

Function AllChilds(parent_archive As Archive)
    If parent_archive.Archives.Count > 0 Then
        For Each subArchive As Archive In parent_archive.Archives
            If subArchive.Archives.Count > 0 Then
                TreeView1.Nodes.Add(subArchive.DisplayName)
                AllChilds(subArchive)
            Else
                Return subArchive
            End If
        Next subArchive
    Else
        Return parent_archive
    End If
End Function

C# example:

class SurroundingClass
{
    public void Main()
    {
        foreach (archive as Archive in archives.Archives)
            TreeView1.Nodes.Add(AllChilds(archive));
    }

    private void AllChilds(Archive parent_archive)
    {
        if (parent_archive.Archives.Count > 0)
        {
            foreach (Archive subArchive in parent_archive.Archives)
            {
                if (subArchive.Archives.Count > 0)
                {
                    TreeView1.Nodes.Add(subArchive.DisplayName);
                    AllChilds(subArchive);
                }
                else
                    return subArchive;
            }
        }
        else
            return parent_archive;
    }
}
question from:https://stackoverflow.com/questions/66059126/loading-unknown-amount-of-childs-to-treenode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...