Problem with TreeView-TreeNode

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

Hi,

I want to make something that adds a TreeNode to the end of TreeView, that
TreeView Looks like

+ Level 1
+ Level 2
+ Level 3
- Level last

The problem is, how do I know at which place I should add the node? It must
be added on a deeper level as the last node before.
So: first i must select the last Node of the TreeView and then I should at
the new node at the next level........

Please help me to get the right commands.......

Thx
JC
 
There are a number of ways to accomplish what you want. The easiest
approach would be to have a reference to the last added node and add your
new node as a child of that.

If holding a reference is not possible, you can compute the deepest node
pretty easily by walking through the TreeNodes in the TreeView. If as
noted, the deepest node is always the last added node, you can do,

// Haven't compiled this - please check for errors
// Assumption: Deepest node is always the last node
while (true)
{
if (tn.Nodes.Count > 0)
tn = tn.Nodes[Nodes.Count - 1];
else
break;
}

- vJ
 
Hi, thx for the idea!

I have a problem: i changed a little bit but still a get these error:
C:\Documents and Settings\Eindwerk\Mijn documenten\BARCO\Eindwerk\Project
Files\thuis\29november\TestTree\Form1.cs(176): Cannot implicitly convert
type 'System.Windows.Forms.TreeNode' to 'TestTree.TreeView'

something in
treeViewSequence = treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

is wrong, maybe you have got an idea? I tried to do this:

treeViewSequence.Nodes =
treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

But it is also bad :( => C:\Documents and Settings\Eindwerk\Mijn
documenten\BARCO\Eindwerk\Project
Files\thuis\29november\TestTree\Form1.cs(176): Property or indexer
'System.Windows.Forms.TreeView.Nodes' cannot be assigned to -- it is read
only


hile (true)
{

if (treeViewSequence.Nodes.Count > 0)

treeViewSequence = treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

else

break;

}

treeViewSequence.Nodes.Add(copy);



Vijaye Raji said:
There are a number of ways to accomplish what you want. The easiest
approach would be to have a reference to the last added node and add your
new node as a child of that.

If holding a reference is not possible, you can compute the deepest node
pretty easily by walking through the TreeNodes in the TreeView. If as
noted, the deepest node is always the last added node, you can do,

// Haven't compiled this - please check for errors
// Assumption: Deepest node is always the last node
while (true)
{
if (tn.Nodes.Count > 0)
tn = tn.Nodes[Nodes.Count - 1];
else
break;
}

- vJ

Jeroen Ceuppens said:
Hi,

I want to make something that adds a TreeNode to the end of TreeView, that
TreeView Looks like

+ Level 1
+ Level 2
+ Level 3
- Level last

The problem is, how do I know at which place I should add the node? It must
be added on a deeper level as the last node before.
So: first i must select the last Node of the TreeView and then I should at
the new node at the next level........

Please help me to get the right commands.......

Thx
JC
 
Jeroen said:
something in
treeViewSequence =
treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

treeViewSequence is a TreeView object and you're attempting to assign
the return value (a TreeNode object) to it. That won't work.
treeViewSequence.Nodes =
treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

Here you're trying to assign a specific TreeNode to the Nodes property
of treeViewSequence. That will not work either. What you need is to
assign a reference to a specific TreeNode to a TreeNode variable:

TreeNodeCollection nodes = treeViewSequence.Nodes;
TreeNode lastNode = nodes[nodes.Count - 1];

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Thx, but this doesn't does exactly what i want, i need to put a new node at
the end of the tree, that end is not te lowest in de list be 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




Frank Oquendo said:
Jeroen said:
something in
treeViewSequence =
treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

treeViewSequence is a TreeView object and you're attempting to assign
the return value (a TreeNode object) to it. That won't work.
treeViewSequence.Nodes =
treeViewSequence.Nodes[treeViewSequence.Nodes.Count - 1];

Here you're trying to assign a specific TreeNode to the Nodes property
of treeViewSequence. That will not work either. What you need is to
assign a reference to a specific TreeNode to a TreeNode variable:

TreeNodeCollection nodes = treeViewSequence.Nodes;
TreeNode lastNode = nodes[nodes.Count - 1];

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Back
Top