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.
Actually, I think the code you got was exactly what you asked for, and
given the expanded nature of your question is similar to the code you will
eventually need to use.
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;
What type is "TreeView1"? Where does the TTreeNodes type come from?
The code you posted doesn't have any sort of recursion or "generation"
traversal of any sort that I see. Which suggests that the TTreeNodes type
exposes the entire tree as a flat hierarchy via its "Item" member. It's
hard to say for sure without knowing what that type is, but it's one
possible conclusion based on the code you posted.
So, with that in mind it's important for you to understand that the
TreeNodeCollection you get in .NET behaves differently. Its "Nodes"
member is a collection _only_ of the direct descendants of the item from
which you got the collection. You have to explicitly enumerate the
"Nodes" member of each of those descendants, and so on, in order to get
the entire tree.
Pedro's code does this via recursion, returning for each node the sum of
the number of direct descendants and the calculated value of nodes from
each of those descendants.
Now, you've added the requirement to be able to emit text information
based on each node. You can still do that, simply by including that code
in the basic function Pedro posted. Each node would be handled within the
foreach() loop, writing out whatever text is required.
[...]
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.
I don't see anything in the code you posted that treats child traversal
differently. Did you leave something out? That's another possible
conclusion based on the code you posted.
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
I don't understand that code at all. It simply counts the top-level nodes
twice. If you had a tree that had 15 total nodes, with 10 top-level
nodes, you'd still get a count of 20. Nothing about that code follows the
pattern provided by Pedro's example.
Instead of the twenty nodes that I have in the tree, I only see ten.
In that C# code example, the value displayed in the message box will
always be twice the actual number of top-level nodes. Simple as that. If
you're testing the code on a tree that has 20 total nodes in it, then the
coincidence is just misleading you into thinking that the code correctly
traverses the tree when counting. It doesn't do that at all.
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.
Well, to some extent recursion is very simple. In another sense, it's
very complicated, if you're not familiar with the concept. To deal witha
recursive structure like a TreeView in .NET, you will need to understand
recursion. So if you don't understand recursion, it's easy to see how
this might seem confusing.
If you don't understand recursion, you might think about reading up on
that first, before attempting to implement something that requires
recursion.
If you do understand recursion, maybe you could be more specific about why
you're trying to enumerate a recursive data structure without writing any
recursive code. The C# code example you posted certainly doesn't come
close to being what would produce correct results, and that's actually
true even if your tree had only top-level nodes.
Pete