By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Count all nodes in a treeview

P: n/a
This code only counts the parent nodes or rootnodes in a treeview,
how do you count all the nodes in a treeview?

// one way
int NodeCounter = 0;
foreach (TreeNode currentNode in TreeView1.Nodes)
NodeCounter++;

// other way
int total = TreeView1.Nodes.Count;


Dec 28 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
What about...

public int GetTotalNodes(TreeView treeView)
{
return this.GetTotalNodes(treeView.Nodes);
}

private int GetTotalNodes(TreeNodeCollection nodes)
{
int rootNodes = nodes.Count;

foreach (TreeNode node in nodes)
{
rootNodes += this.GetTotalNodes(node.Nodes);
}

return rootNodes ;
}

Greetings,

"John Rogers" <jo************@aol.comescribió en el mensaje de
noticias:eW**************@TK2MSFTNGP02.phx.gbl...
This code only counts the parent nodes or rootnodes in a treeview,
how do you count all the nodes in a treeview?

// one way
int NodeCounter = 0;
foreach (TreeNode currentNode in TreeView1.Nodes)
NodeCounter++;

// other way
int total = TreeView1.Nodes.Count;

Dec 28 '07 #2

P: n/a
* * * * * * * * private int GetTotalNodes(TreeNodeCollection nodes)
* * * * * * * * {
* * * * * * * * * * * * int rootNodes = nodes.Count;

* * * * * * * * * * * * foreach (TreeNode node in nodes)
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * * rootNodes += this.GetTotalNodes(node.Nodes);
* * * * * * * * * * * * }

* * * * * * * * * * * * return rootNodes ;
* * * * * * * * }
so, count all nodes not just tree nodes... I dont get it... *as
always*

//CY
Dec 29 '07 #3

P: n/a
Thanks, that worked great.

"Pedro Luna Montalvo" <pe*******@hotmail.comwrote in message
news:3C**********************************@microsof t.com...
What about...

Dec 29 '07 #4

P: n/a
On Fri, 28 Dec 2007 15:57:51 -0800, <ch*******@gmail.comwrote:
so, count all nodes not just tree nodes... I dont get it... *as
always*
What's not to get? The only nodes in a TreeView are going to be
TreeNodes. The statement "count all nodes not just tree nodes" doesn't
make any sense. "All nodes" is the same as "all tree nodes".

Pete
Dec 29 '07 #5

P: n/a
I thik I jumped the gun here. When I saw the actual number of nodes
it made me think that this is it, unfortunately its not.

Let me see if I can explain clearly what I am trying to do. I am trying
to traverse the entire tree to do the following.

1. Store the Node Text to an ini file
2. Store the Node Level to an ini file
3. Store the Node ImageIndex to an ini file.

I have code that I have used in C++Builder which is very close to C#.
But I still have to do a bit of translating to get everything to copile and
work correctly. This is the code I used in C++Builder and it works great.

//-------------------------------------------------------
TTreeNodes *Nodes = TreeView1->Items;
int val = Nodes->Count;

Ini->EraseSection("TreeNodes");

for( int i = 1; i < val; i++ )
{
Ini->WriteString("TreeNodes", IntToStr(i) + "NodeText",
Nodes->Item[i]->Text);
Ini->WriteInteger("TreeNodes", IntToStr(i) + "NodeLevel",
Nodes->Item[i]->Level);
Ini->WriteInteger("TreeNodes", IntToStr(i) + "NodeIcon",
Nodes->Item[i]->ImageIndex);
if((Nodes->Item[i]->HasChildren) && (Nodes->Item[i]->Expanded))
Ini->WriteBool("TreeNodes", IntToStr(i) + "Expand",
Nodes->Item[i]->Expanded);
}
//------------------------------------------------------------
First I start with the Parent Node as I go down the tree, then if there is a
child I traverse the childnode
and get all of it's settings too. Now having to do this in C# is a bit of a
challenge for me since it's a new
language for me.

I tried this code like this to see if I could actually traverse every node,
but it always falls short.

TreeNodeCollection nodes = tvMain.Nodes;
int rootNodes = nodes.Count;

foreach (TreeNode node in nodes)
{
rootNodes ++;
listBox.Items.Add(node.Text); // this shows 10 nodes
}
MessageBox.Show(Convert.ToString(rootNodes)); // this shows 20
nodes

Instead of the twenty nodes that I have in the tree, I only see ten.
Somehow I am not understanding how
to traverse down through the children nodes too. I am trying to keep the
code very simple so I can understand
it.
Thanks everyone

John


Dec 29 '07 #6

P: n/a
>What type is "TreeView1"? Where does the TTreeNodes type come from?
TreeView1 is a Treeview, TTreeNodes is Nodes from the tree.

The code that I posted from C++Builder recurses an entire tree and you can
get whatever properties you want. I would much have desired to stay with
C++Builder rather than going to learn a new language. But since there is
no support for pocket pc, I have no choice but to learn C#. Since what I
am writing is a program that will work on th desktop and on the Pocket PC.

I stripped down Pedro's code a bit so I could get the understanding of the
recursion. This foreach() stuff is confusing, since I don't have a number
from
the loop like I though I would.

Anyway, let me try it again and see if i can get the properties I need.
Because
after I save the settings, I still have to load them back into the Tree.
John
Dec 29 '07 #7

P: n/a
I have to admit that I just don't get this.

// this is what I am adding my nodes to the tree with
tvMain.BeginUpdate();
tvMain.Nodes.Clear();
for (int i = 0; i < 10; ++i)
{
TreeNode ParentNode = tvMain.Nodes.Add("Parent Node: " +
Convert.ToString(i));
ParentNode.Nodes.Add("Child Node: " + Convert.ToString(i));
}
tvMain.EndUpdate();
// this is what i am checking the nodes with
GetTotalNodes(TreeView1);
public int GetTotalNodes(TreeView treeView)
{
return this.GetTotalNodes(treeView.Nodes);
}
private int GetTotalNodes(TreeNodeCollection nodes)
{
int rootNodes = nodes.Count;

foreach (TreeNode node in nodes)
{

//listBox.Items.Add(node.Text + " " +
Convert.ToString(Counter));
listBox.Items.Add(node.Text);
rootNodes += this.GetTotalNodes(node.Nodes);
Counter++;

}
return rootNodes;
}
This is what I am looking for. If I have 20 nodes, when I iterate the tree,
I am looking
for numbers from 1-20 telling me there are 20 nodes. If I have 3000 nodes,
I want the
numbers to go from 1-3000 telling me there are 3000 nodes on that tree.

This foreach() isn't giving me any numbers like I would get if I do a loop
like
for(int x = 0; x < treeview.nodes.count; ++x)

At least with that code I get a numeric value. That is important if I am
going to store the
tree layout and restore it again. That is unless someone is willing to show
me how to
store and load a treeview layout, because right now I have absolutely no
clue.
Thanks
Dec 29 '07 #8

P: n/a
Peter,

I have one more question. I have been reading and searching quite a
bit
to find the answer, but I have not had any luck since I don't know
what I
am looking for.

struct NodeInfo
{
public readonly string name;
public readonly int depth;

public NodeInfo(string name, int depth)
{
this.name = name;
this.depth = depth;
}
}

After you create your struct and pass the values to the struct, how do
you access
the values? I was assuming that it would be like this

// create my list
List<NodeInfoSavedTree = new List<NodeInfo>();

// add information to the list
SavedTree.Add(new NodeInfo(node.Text, depth));

// access the info like this, but this does not work.
listBox.Add(SavedTree.name);

This is strange, thats all I have to say.
John



Dec 30 '07 #9

P: n/a
On Sat, 29 Dec 2007 18:49:38 -0800, John Rogers <jo************@aol.com>
wrote:
[...]
After you create your struct and pass the values to the struct, how do
you access
the values?
By accessing an instance of the struct and referencing the public member
fields.
I was assuming that it would be like this

// create my list
List<NodeInfoSavedTree = new List<NodeInfo>();
This creates a List that can contain instances of NodeInfo. It's sort of
like initializing an array like this:

NodeInfo[] array = new NodeInfo[10];

except that you don't need to know how many elements you want in advance..
// add information to the list
SavedTree.Add(new NodeInfo(node.Text, depth));
This initializes an instance of NodeInfo and pass that instance to the
List.Add() method. At this point the List now contains a copy of that
instance of NodeInfo. It's sort of like assigning a value to an array
element:

array[5] = new NodeInfo(node.Text, depth);

except that you don't have to keep track of where the current place to add
an element in the array is, and if the data structure becomes filled, it
automatically reallocates itself internally when you try to add more.
// access the info like this, but this does not work.
listBox.Add(SavedTree.name);
The SavedTree variable is the list itself. It doesn't have a "name"
property or field.

The semantics of the List<class are similar in many respects to arrays..
Just as you have to index an element in an array to get at the values
stored there, you have to index an element in the List<to get at the
values stored there. The List<itself doesn't know anything about those
values any more than an array would.

The code I posted shows how you _do_ get at the information in the
NodeInfo instances. See the RestoreTree() method for examples.
This is strange, thats all I have to say.
I don't know why it's strange. Either you're not getting enough sleep, or
you need to get back to basics and review the basic .NET data structures
(or maybe even C arrays). :)

Pete
Dec 30 '07 #10

P: n/a
>I don't know why it's strange. Either you're not getting enough
>sleep, or you need to get back to basics and review the basic .NET
data structures
Believe me this is so different from what I am used to, I am reading a
few books
dealing with C#. But I like books with real world examples and I
don't see
many examples out there. Plus I haven't done any coding in about two
years.

Thanks again Pete
John
Dec 30 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.