none
About the recursion for Arraylist RRS feed

  • Question

  •  Public Shared Function GetNodeText(ByVal nc As NodeCollection) As ArrayList
            Dim alText As New ArrayList
            For Each node As Node In nc
                alText.Add(node.Text)
                If node.HasChildNodes Then
                    alText = alText.AddRange(GetNodeText(node.Nodes))' How to do???
                End If
            Next
            Return alText
        End Function
    Wednesday, February 5, 2020 12:46 PM

All replies

  • Hello,

    Here is a pattern to follow using a TreeNode where the pattern should work but untested. Taken from here.

    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


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, February 5, 2020 12:54 PM
  • Thank you, but i want to use one function to return the all node text list.
    Wednesday, February 5, 2020 2:07 PM
  • Like this as a base pattern.

    Public Sub NodesRecursive(ParentNode As TreeNode)
        Console.WriteLine(ParentNode.Text)
        For Each SubNode As TreeNode In ParentNode.Nodes
            NodesRecursive(SubNode)
        Next
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, February 5, 2020 2:38 PM
  • Your question isn't clear.  Here is an example that uses a Treeview with some nodes.  The code to get all of the nodes is as follows.

        Private Function GetAll(StartNode As TreeNode) As List(Of TreeNode)
            Dim rv As New List(Of TreeNode)
            rv.Add(StartNode) 'up to you
            rv.AddRange(GetChildren(StartNode.Nodes))
            Return rv
        End Function
    
        Private Function GetChildren(ndC As TreeNodeCollection) As List(Of TreeNode)
            Dim rv As New List(Of TreeNode)
            For Each nd As TreeNode In ndC
                rv.Add(nd)
                rv.AddRange(GetChildren(nd.Nodes))
            Next
            Return rv
        End Function
    

    To test the code I did this

            Dim l As List(Of TreeNode) = GetAll(TreeView1.Nodes(0))
    
            Debug.WriteLine("")
            For Each nd As TreeNode In l
                Dim s As New String(ControlChars.Tab, nd.Level)
                Debug.Write(s)
                Debug.WriteLine(nd.Name)
            Next
    

    Hope you gain some insights.


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Wednesday, February 5, 2020 2:53 PM
  • Sorry, i used the Devcomponents AdvTree control. I only want to use a recursion function to get all nodes text to a list and return this list like

     Public Function GetAllNodeText(ByVal nc As NodeCollection) As ArrayList
            Dim alText As New ArrayList
            For Each node As Node In nc
                alText.Add(node.Text)
                If node.HasChildNodes Then
                    alText = alText.AddRange(GetNodeText(node.Nodes))'  I do not know how to write this line???
                End If
            Next
            Return alText
        End Function

    Then invoke it use

    Dim allNodeText as list = GetAllNodeText(Advtree1.Nodes)

    Wednesday, February 5, 2020 10:00 PM
  • Sorry, i used the Devcomponents AdvTree control. I only want to use a recursion function to get all nodes text to a list and return this list like

     Public Function GetAllNodeText(ByVal nc As NodeCollection) As ArrayList
            Dim alText As New ArrayList
            For Each node As Node In nc
                alText.Add(node.Text)
                If node.HasChildNodes Then
                    alText = alText.AddRange(GetNodeText(node.Nodes))'  I do not know how to write this line???
                End If
            Next
            Return alText
        End Function

    Then invoke it use

    Dim allNodeText as list = GetAllNodeText(Advtree1.Nodes)

    I would suggest asking this question at the vendor support

    https://www.devcomponents.com/support.aspx


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, February 5, 2020 10:09 PM