How to get a list of all child nodes in a TreeView in .NET

后端 未结 10 1101
抹茶落季
抹茶落季 2020-12-15 21:28

I have a TreeView control in my WinForms .NET application that has multiple levels of childnodes that have childnodes with more childnodes, with no defined depth. When a use

10条回答
  •  旧时难觅i
    2020-12-15 22:03

    Use recursion

    Function GetChildren(parentNode as TreeNode) as List(Of String)
      Dim nodes as List(Of String) = New List(Of String)
      GetAllChildren(parentNode, nodes)
      return nodes
    End Function
    
    Sub GetAllChildren(parentNode as TreeNode, nodes as List(Of String))
      For Each childNode as TreeNode in parentNode.Nodes
        nodes.Add(childNode.Text)
        GetAllChildren(childNode, nodes)
      Next
    End Sub
    

提交回复
热议问题