473,394 Members | 1,674 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,394 software developers and data experts.

Count all nodes in a treeview

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
10 34396
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
* * * * * * * * 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
Thanks, that worked great.

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

Dec 29 '07 #4
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
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
>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
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
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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Olga | last post by:
Hi guys, I want to use Microsoft treeview control (no other) in my asp page and fill it with parent-child information from datatable. I draged and dropped treeview object from toolbox ty my page...
6
by: Diodak | last post by:
How i'can Count nodes in XML? 1. how get count nodes our base in XML for level <txt> <article> <txt> <disc>aaaaaaa</disc> <misc>bbbbbbb</misc> <pict>bbbbbccc</pict> </txt> <txt>
3
by: Andy Fish | last post by:
Hi, I'm trying to use <xsl:number> to generate a sequence number of all nodes that match a particular pattern, e.g: <xsl:template match="foo"> <xsl:copy> <xsl:attribute name="id"><xsl:number...
14
by: Mr.D | last post by:
How do I save/load the contents of a Treeview to a file? I have found several good examples written i VB6, but not a single one for VB.NET. Please help. ---- Tim
4
by: Marc R. | last post by:
Hi all, I won't ask an complete example here but some good links for that issue would be perfect. (the I will only need to do the save function to save the current position of the nodes) ...
2
by: James L | last post by:
I have finally developed some code that allows you to re populate a tree view and re select the last node that was clicked. However, you have to hard code it for the number of levels the tree view...
8
by: Matt MacDonald | last post by:
Hi All, I have a form that displays hierarchical categories in a treeview. Ok so far so good. What I was to do is have users be able to select a node in the treeview as part of filling out the...
1
by: JS1 | last post by:
Hi there, How do I count the number of <Item> nodes for each <Message> in the following xml document? (Xml Document) <Message> <Order> <Item>1</Item> <Item>2</Item>
11
by: pbd22 | last post by:
Hi. I am getting odd treeview results and hope you can help. I am parsing a string, "x/y/z", turning it into an array (that always seems to start with an empty string) and then using the...
1
by: Derek Hart | last post by:
I wish to get a count of nodes in an xml file in vb.net, so I am using the following, as a simple example: MyCount = xmlNodePart.Current.Select.//MyNode/Record).Count I saw in some xml...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.