473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 34439
What about...

public int GetTotalNodes(T reeView treeView)
{
return this.GetTotalNo des(treeView.No des);
}

private int GetTotalNodes(T reeNodeCollecti on nodes)
{
int rootNodes = nodes.Count;

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

return rootNodes ;
}

Greetings,

"John Rogers" <jo************ @aol.comescribi ó en el mensaje de
noticias:eW**** **********@TK2M SFTNGP02.phx.gb l...
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(T reeNodeCollecti on nodes)
* * * * * * * * {
* * * * * * * * * * * * int rootNodes = nodes.Count;

* * * * * * * * * * * * foreach (TreeNode node in nodes)
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * * rootNodes += this.GetTotalNo des(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*******@hotm ail.comwrote in message
news:3C******** *************** ***********@mic rosoft.com...
What about...

Dec 29 '07 #4
On Fri, 28 Dec 2007 15:57:51 -0800, <ch*******@gmai l.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("T reeNodes", 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("Tre eNodes", 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.

TreeNodeCollect ion nodes = tvMain.Nodes;
int rootNodes = nodes.Count;

foreach (TreeNode node in nodes)
{
rootNodes ++;
listBox.Items.A dd(node.Text); // this shows 10 nodes
}
MessageBox.Show (Convert.ToStri ng(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.BeginUpd ate();
tvMain.Nodes.Cl ear();
for (int i = 0; i < 10; ++i)
{
TreeNode ParentNode = tvMain.Nodes.Ad d("Parent Node: " +
Convert.ToStrin g(i));
ParentNode.Node s.Add("Child Node: " + Convert.ToStrin g(i));
}
tvMain.EndUpdat e();
// this is what i am checking the nodes with
GetTotalNodes(T reeView1);
public int GetTotalNodes(T reeView treeView)
{
return this.GetTotalNo des(treeView.No des);
}
private int GetTotalNodes(T reeNodeCollecti on nodes)
{
int rootNodes = nodes.Count;

foreach (TreeNode node in nodes)
{

//listBox.Items.A dd(node.Text + " " +
Convert.ToStrin g(Counter));
listBox.Items.A dd(node.Text);
rootNodes += this.GetTotalNo des(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<NodeInfoSa vedTree = new List<NodeInfo>( );

// add information to the list
SavedTree.Add(n ew NodeInfo(node.T ext, depth));

// access the info like this, but this does not work.
listBox.Add(Sav edTree.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<NodeInfoSa vedTree = 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(n ew NodeInfo(node.T ext, 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.T ext, 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(Sav edTree.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1300
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 and then tryied to add some nodes. TreeView.Nodes.Add ..... didin't work and gave me "Object regueired" error. Any advise pls Thanks Olga
6
13282
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
3151
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 level="any"/></xsl:attribute> </xsl:copy> </xsl:template>
14
15060
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
2020
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) Thanks in advance.
2
2384
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 could possibly be expanded to. Any ideas? //The test program has a button to set the path and a button to execute the path finding code. ...
8
12738
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 form. I only want to allow single selection, so using checkboxes is out of the question. It works as is, but it makes the form very cumbersome if...
1
2404
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
1498
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 elements of that array to populate a single tree. The first non-empty element is the root, the last is a file - everything in between is a folder.
1
3979
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 specs that instead of it should be , but that does
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.