473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2853
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

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

Similar topics

5
by: SoKool | last post by:
Can anyone point me to a site where I can get a free treeview control to use in ASP .NET or any tutorial that can help me build my own treeview control. I intend to use a treeview to generate a...
3
by: Hrvoje Voda | last post by:
I manage to fill treeview control with data and I would like to search through that data. In child I have names of films, and when I click on one of them I would like to show it's information in...
1
by: Hrvoje Voda | last post by:
How can I check if the parent root is "Opened"? I mean if a user has expand the treeview and he can see the child root ? Hrcko
3
by: Peter | last post by:
Hello, We are inserting a side menu to our application using a class that is writing HTML on all our pages. This is a part of the code as an example: writer.Write(" <table WIDTH=""100%""...
0
by: jus_do_it_b | last post by:
I am trying to capture the treeview control events on the client side and do some action accordingly but i could not accomplish it till now. I was able to do it using the IE treeview control...
10
by: p3t3r | last post by:
I have a treeview sourced from a SiteMap. I want to use 2 different CSS styles for the root level nodes. The topmost root node should not have a top border, all the other root nodes should have a...
4
by: Henry | last post by:
Does anybody have a real-world sample of buiding a treeview control using data from database tables? All the sample code I have found either builds the treeview manually or uses a file directory...
1
by: shil | last post by:
Hi, I am trying to list some files from server using .net 2005 treeview control, which I could successfully do it. Also, I would like to open the Open/Save dialog box when user clicks on a file...
13
by: Raman | last post by:
Hi All, Could any one tell me How to concatenate two strings using recursion. And also how to trim a string using recursion.(in C, offcourse) Regards, Raman Chalotra
2
by: CraigMuckleston | last post by:
I have a Treeview Control on my Windows Form with a directory structure, ie a list of nodes. This is actually a directory listing from my c drive (similiar to Windows Exlorer). I can iterate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.