Ok.. My mistake... objects from foreach are read-only.
Here's a modified version:
TreeNode deepestNode = null;
int deepestLevel = 0;
foreach (TreeNode node in treeView1.Nodes)
{
// This will get all the top level nodes - and now, let's
iterate
// through each one.
int level = 0;
TreeNode nodeTemp = node;
while (nodeTemp.Nodes.Count > 0)
{
level++;
nodeTemp = nodeTemp.Nodes[0];
}
// Check if we hit the deepest
if (level >= deepestLevel)
{
deepestNode = nodeTemp;
deepestLevel = level;
}
}
// Now add your new node
deepestNode.Nodes.Add(newNode);
-vJ
Jeroen Ceuppens said:
Thx!
There is still a problem:
C:\Documents and Settings\Eindwerk\Mijn documenten\BARCO\Eindwerk\Project
Files\thuis\29november\TestTree\Form1.cs(206): Property or indexer
'System.Windows.Forms.TreeNode.Parent' cannot be assigned to -- it is read
only
this error i get on node = node.Nodes[0];
Do you know what i can do to make it not read only?
Thx
JC
Vijaye Raji said:
Sorry - small correction... Check below
As I told before, there's no pre-defined method/property for your problem.
You need to come up with an algorithm.... Do you think something like
this
will solve your problem?
TreeNode deepest;
int deepestLevel = 0;
foreach (TreeNode node in treeView.Nodes)
{
// This will get all the top level nodes - and now, let's iterate
// through each one.
int level = 0;
while (node.Nodes.Count > 0)
{
level++;
node = node.Nodes[0];
}
// Check if we hit the deepest
if (level >= deepestLevel)
{
deepestNode = node;
// CORRECTION: This line is important.
deepestLevel = level
}
}
// Now, add your new node to the deepest node
deepestNode.Nodes.Add(newNode);
-vJ
I need to put a new node at the end of the tree, that end is not te
lowest
in de list but the deepest
(the one with the most + before it)
Node A
Node 1
Node 2
Node 3
Node 4: Deepest
Node B: not this one
I need to Put everytime a new child to the Deepest so:
first:
Node A
then
Node A
Node 1
then
Node A
Node 1
Node 2
and so on
I tried many things, like TreeNodeCollection and
treeViewSequence.Nodes.GetEnumerator() But i can't find a way to fix
this,
it is difficult to get the child, help me....
Thx
JC