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 -
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 ManningFiltersTreeView_Aftercheck procedure?
5 2853
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?
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. -
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: -
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
-
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
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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%""...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
| |