468,106 Members | 1,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,106 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1. Private Sub ManningFiltersTreeView_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles ManningFiltersTreeView.AfterCheck
  2.  
  3. RecurseTree(ManningFiltersTreeView)
  4.  
  5. End Sub
  6.  
  7. Private Sub BuildArray(ByVal n As TreeNode)
  8.  
  9. MessageBox.Show(n.Text)
  10. Dim aNode As TreeNode
  11. For Each aNode In n.Nodes
  12. BuildArray(aNode)
  13. Next
  14.  
  15. End Sub
  16.  
  17. ' Call the procedure using the top nodes of the treeview.
  18.  
  19. Private Sub RecurseTree(ByVal aTreeView As TreeView)
  20.  
  21. Dim n As TreeNode
  22. For Each n In aTreeView.Nodes
  23. BuildArray(n)
  24. Next
  25.  
  26. End Sub
  27.  



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 ManningFiltersTreeView_Aftercheck procedure?
Oct 9 '08 #1
5 2527
mldisibio
190 Expert 100+
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?
Oct 9 '08 #2
mldisibio
190 Expert 100+
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 TreeNodeCollection, you can reduce your recursion to one method, which increases the readability a bit.

Expand|Select|Wrap|Line Numbers
  1. void ManningFiltersTreeView_AfterCheck(object sender, TreeViewEventArgs e){
  2.  List<string> nodeTextList = new List<string>();
  3.  ExtractTextFromNodes(ManningFiltersTreeView.Nodes, nodeTextList);
  4.  string[] nodeTextArray = nodeTextList.ToArray();
  5. }
  6. static void ExtractTextFromNodes(TreeNodeCollection nodeList, List<string> buffer) {
  7.  foreach (TreeNode n in nodeList) {
  8.   buffer.Add(n.Text);
  9.   ExtractTextFromNodes(n.Nodes, buffer);
  10.   }
  11. }
  12.  
Full example:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. namespace bytes {
  6.   class test {
  7.  
  8.     static void Main() {
  9.       TreeView myFamilyTree = createTreeView();
  10.       List<string> familyList = new List<string>();
  11.       ExtractTextFromNodes(myFamilyTree.Nodes, familyList);
  12.       string[] familyArray = familyList.ToArray();
  13.       Console.WriteLine(String.Join(Environment.NewLine, familyArray));
  14.     }
  15.  
  16.     static void ExtractTextFromNodes(TreeNodeCollection nodeList, List<string> buffer) {
  17.       foreach (TreeNode n in nodeList) {
  18.         buffer.Add(n.Text);
  19.         ExtractTextFromNodes(n.Nodes, buffer);
  20.       }
  21.     }
  22.  
  23.     static TreeView createTreeView() {
  24.       TreeView tempTree = new TreeView();
  25.       tempTree.BeginUpdate();
  26.       tempTree.Nodes.Add("Grandfather");
  27.       tempTree.Nodes[0].Nodes.Add("Father");
  28.       tempTree.Nodes[0].Nodes.Add("Uncle");
  29.       tempTree.Nodes[0].Nodes[0].Nodes.Add("Grandchild1 By Father");
  30.       tempTree.Nodes[0].Nodes[0].Nodes.Add("Grandchild2 By Father");
  31.       tempTree.Nodes[0].Nodes[1].Nodes.Add("Grandchild3 By Uncle");
  32.       tempTree.Nodes[0].Nodes[1].Nodes.Add("Grandchild4 By Uncle");
  33.       tempTree.Nodes[0].Nodes[0].Nodes[1].Nodes.Add("GreatGrandchild1 By Grandchild2");
  34.       tempTree.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("GreatGrandchild2 By Grandchild3");
  35.       tempTree.EndUpdate();
  36.       return tempTree;
  37.     }
  38.   }
  39. }
  40.  
Will someone post the VB version please, since I don't have a VB.Net test environment.

Mike
Oct 9 '08 #3
mldisibio
190 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. Friend Class test
  2.   ' Methods
  3.   Private Shared Sub Main()
  4.       Dim myFamilyTree As TreeView = test.createTreeView
  5.       Dim familyList As New List(Of String)
  6.       test.ExtractTextFromNodes(myFamilyTree.Nodes, familyList)
  7.       Dim familyArray As String() = familyList.ToArray
  8.       Console.WriteLine(String.Join(Environment.NewLine, familyArray))
  9.   End Sub
  10.   Private Shared Sub ExtractTextFromNodes(ByVal nodeList As TreeNodeCollection, ByVal buffer As List(Of String))
  11.       Dim n As TreeNode
  12.       For Each n In nodeList
  13.           buffer.Add(n.Text)
  14.           test.ExtractTextFromNodes(n.Nodes, buffer)
  15.       Next
  16.   End Sub
  17.   Private Shared Function createTreeView() As TreeView
  18.       Dim tempTree As New TreeView
  19.       tempTree.BeginUpdate
  20.       tempTree.Nodes.Add("Grandfather")
  21.       tempTree.Nodes.Item(0).Nodes.Add("Father")
  22.       tempTree.Nodes.Item(0).Nodes.Add("Uncle")
  23.       tempTree.Nodes.Item(0).Nodes.Item(0).Nodes.Add("Grandchild1 By Father")
  24.       tempTree.Nodes.Item(0).Nodes.Item(0).Nodes.Add("Grandchild2 By Father")
  25.       tempTree.Nodes.Item(0).Nodes.Item(1).Nodes.Add("Grandchild3 By Uncle")
  26.       tempTree.Nodes.Item(0).Nodes.Item(1).Nodes.Add("Grandchild4 By Uncle")
  27.       tempTree.Nodes.Item(0).Nodes.Item(0).Nodes.Item(1).Nodes.Add("GreatGrandchild1 By Grandchild2")
  28.       tempTree.Nodes.Item(0).Nodes.Item(1).Nodes.Item(0).Nodes.Add("GreatGrandchild2 By Grandchild3")
  29.       tempTree.EndUpdate
  30.       Return tempTree
  31.   End Function
  32. End Class
  33.  
Oct 9 '08 #4
Great! I did want a flattened array, I will use the array as my Criteria1: property in the Range.Autofilter 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.
Oct 9 '08 #5
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.
Oct 11 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by SoKool | last post: by
3 posts views Thread by Hrvoje Voda | last post: by
1 post views Thread by Hrvoje Voda | last post: by
reply views Thread by jus_do_it_b | last post: by
13 posts views Thread by Raman | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.