Using Recursion to iterate through Treeview control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dutsnekcirf
    New Member
    • Oct 2008
    • 10

    Using Recursion to iterate through Treeview control

    I'm new to the concept of recursion and it's quite confusing to me. I found an article here on MSDN that talks about how to iterate through the nodes in a treeview. I've able to get the code example to work in my project, but rather than have it display a message box for each treenode, I'd like it to take the text property of each treenode and add it to an array.



    Here's the code that I got:



    Code Snippet
    Code:
    Private Sub ManningFiltersTreeView_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles ManningFiltersTreeView.AfterCheck
    
    RecurseTree(ManningFiltersTreeView)
    
    End Sub
    
    Private Sub BuildArray(ByVal n As TreeNode)
    
    MessageBox.Show(n.Text)
    Dim aNode As TreeNode
    For Each aNode In n.Nodes
    BuildArray(aNode)
    Next
    
    End Sub
    
    ' Call the procedure using the top nodes of the treeview.
    
    Private Sub RecurseTree(ByVal aTreeView As TreeView)
    
    Dim n As TreeNode
    For Each n In aTreeView.Nodes
    BuildArray(n)
    Next
    
    End Sub



    So basically where it says MessageBox.Show (n.Text), can I have it instead add the value of n.text to an array and then pass that back to my ManningFiltersT reeView_Afterch eck procedure?
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    Doing so will flatten the tree hierarchy into a single dimension array. Before a code suggestion is posted, is a flat array acceptable?

    Or did you want a jagged array that mimics the node structure?

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      Assuming you are okay with a flattened hierarchy, this will do what you are looking for. Also notice that since both TreeView and TreeNode have a TreeNodeCollect ion, you can reduce your recursion to one method, which increases the readability a bit.

      Code:
      void ManningFiltersTreeView_AfterCheck(object sender, TreeViewEventArgs e){
       List<string> nodeTextList = new List<string>();
       ExtractTextFromNodes(ManningFiltersTreeView.Nodes, nodeTextList);
       string[] nodeTextArray = nodeTextList.ToArray();
      }
      static void ExtractTextFromNodes(TreeNodeCollection nodeList, List<string> buffer) {
       foreach (TreeNode n in nodeList) {
        buffer.Add(n.Text);
        ExtractTextFromNodes(n.Nodes, buffer);
        }
      }
      Full example:
      Code:
      using System;
      using System.Collections.Generic;
      using System.Windows.Forms;
      
      namespace bytes {
        class test {
      
          static void Main() {
            TreeView myFamilyTree = createTreeView();
            List<string> familyList = new List<string>();
            ExtractTextFromNodes(myFamilyTree.Nodes, familyList);
            string[] familyArray = familyList.ToArray();
            Console.WriteLine(String.Join(Environment.NewLine, familyArray));
          }
      
          static void ExtractTextFromNodes(TreeNodeCollection nodeList, List<string> buffer) {
            foreach (TreeNode n in nodeList) {
              buffer.Add(n.Text);
              ExtractTextFromNodes(n.Nodes, buffer);
            }
          }
      
          static TreeView createTreeView() {
            TreeView tempTree = new TreeView();
            tempTree.BeginUpdate();
            tempTree.Nodes.Add("Grandfather");
            tempTree.Nodes[0].Nodes.Add("Father");
            tempTree.Nodes[0].Nodes.Add("Uncle");
            tempTree.Nodes[0].Nodes[0].Nodes.Add("Grandchild1 By Father");
            tempTree.Nodes[0].Nodes[0].Nodes.Add("Grandchild2 By Father");
            tempTree.Nodes[0].Nodes[1].Nodes.Add("Grandchild3 By Uncle");
            tempTree.Nodes[0].Nodes[1].Nodes.Add("Grandchild4 By Uncle");
            tempTree.Nodes[0].Nodes[0].Nodes[1].Nodes.Add("GreatGrandchild1 By Grandchild2");
            tempTree.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("GreatGrandchild2 By Grandchild3");
            tempTree.EndUpdate();
            return tempTree;
          }
        }
      }
      Will someone post the VB version please, since I don't have a VB.Net test environment.

      Mike

      Comment

      • mldisibio
        Recognized Expert New Member
        • Sep 2008
        • 191

        #4
        Code:
        Friend Class test
          ' Methods
          Private Shared Sub Main()
              Dim myFamilyTree As TreeView = test.createTreeView
              Dim familyList As New List(Of String)
              test.ExtractTextFromNodes(myFamilyTree.Nodes, familyList)
              Dim familyArray As String() = familyList.ToArray
              Console.WriteLine(String.Join(Environment.NewLine, familyArray))
          End Sub
          Private Shared Sub ExtractTextFromNodes(ByVal nodeList As TreeNodeCollection, ByVal buffer As List(Of String))
              Dim n As TreeNode
              For Each n In nodeList
                  buffer.Add(n.Text)
                  test.ExtractTextFromNodes(n.Nodes, buffer)
              Next
          End Sub
          Private Shared Function createTreeView() As TreeView
              Dim tempTree As New TreeView
              tempTree.BeginUpdate
              tempTree.Nodes.Add("Grandfather")
              tempTree.Nodes.Item(0).Nodes.Add("Father")
              tempTree.Nodes.Item(0).Nodes.Add("Uncle")
              tempTree.Nodes.Item(0).Nodes.Item(0).Nodes.Add("Grandchild1 By Father")
              tempTree.Nodes.Item(0).Nodes.Item(0).Nodes.Add("Grandchild2 By Father")
              tempTree.Nodes.Item(0).Nodes.Item(1).Nodes.Add("Grandchild3 By Uncle")
              tempTree.Nodes.Item(0).Nodes.Item(1).Nodes.Add("Grandchild4 By Uncle")
              tempTree.Nodes.Item(0).Nodes.Item(0).Nodes.Item(1).Nodes.Add("GreatGrandchild1 By Grandchild2")
              tempTree.Nodes.Item(0).Nodes.Item(1).Nodes.Item(0).Nodes.Add("GreatGrandchild2 By Grandchild3")
              tempTree.EndUpdate
              Return tempTree
          End Function
        End Class

        Comment

        • dutsnekcirf
          New Member
          • Oct 2008
          • 10

          #5
          Great! I did want a flattened array, I will use the array as my Criteria1: property in the Range.Autofilte r method in an Excel Spreadsheet.

          I haven't given your code a test yet since I'm not at work but will do so once I'm in.

          Thanks for your reply.

          Comment

          • dutsnekcirf
            New Member
            • Oct 2008
            • 10

            #6
            I LOVE you guys!!!!! It's working. It's taken me a month to get it working right and with your help it's finally working perfectly.

            I originally had it working with just checkboxes in a control group, but this treeview code has taken my code from 500 and something lines of code down to something like 30 lines of code including comments.

            Thank you, Thank you, Thank you.

            Comment

            Working...