Treeview add node problem

P

poldoj

Hi all, I have a treenode control and I would like to add a node in a
certain level as child. For example I know that with this code I can add a
level one node plus a level two node:

-------------------
Dim node As TreeNode
node = TreeView1.Nodes.Add("Level one node")
node.Nodes.Add("Level two node")
-------------------

Well, at this point is possible to add a child node at that "level two node"
with code? (whitout select it with the mouse, just with code )

Thanks
 
T

Tom Shelton

Hi all, I have a treenode control and I would like to add a node in a
certain level as child. For example I know that with this code I can add a
level one node plus a level two node:

TreeView1.Nodes(0).Nodes.Add ("Another node")

Is that what your looking for? Or am I misunderstanding the question?
 
P

poldoj

First of all, thanks for your reply Tom Shelton [MVP], I tried that code
-----
TreeView1.Nodes(0).Nodes.Add ("Another node")
-----

It just act like node.Nodes.Add("Level two node")
and if put a 1 instead of 0 an "out of range exception is throwed".
I need to add a level 3 node at an preexistent level 2, sorry for my bad
englsh. Example
---------
level 1
level 2a
level 2b
level 2c
level 3
---------

Its possible to add that "level 3" with code? I know that if I select with
the mouse "level 2c" than is possible to use

Dim node01 As New TreeNode

node01 = TreeView1.SelectedNode

node01.Nodes.Add("node added")

--------------
 
T

Tom Shelton

First of all, thanks for your reply Tom Shelton [MVP], I tried that code
-----
TreeView1.Nodes(0).Nodes.Add ("Another node")
-----

It just act like node.Nodes.Add("Level two node")
and if put a 1 instead of 0 an "out of range exception is throwed".
I need to add a level 3 node at an preexistent level 2, sorry for my bad
englsh. Example
---------
level 1
level 2a
level 2b
level 2c
level 3
---------

Its possible to add that "level 3" with code? I know that if I select with
the mouse "level 2c" than is possible to use

Dim node01 As New TreeNode

node01 = TreeView1.SelectedNode

node01.Nodes.Add("node added")

--------------

treeview1.Nodes (0).Nodes(2).Nodes.Add ("level 3")
 
P

Phill. W

poldoj said:
I know that with this code I can add a level one node plus a level
two node:

Dim node As TreeNode
node = TreeView1.Nodes.Add("Level one node")
node.Nodes.Add("Level two node")

Well, at this point is possible to add a child node at that "level two
node" with code?

Just extend what you've got, remembering that whenever you
call Nodes.Add(), you get the a reference to the Node that after it
has been added, so

Dim level1 As TreeNode
Dim level2 As TreeNode
Dim level3 As TreeNode

level1 = TreeView1.Nodes.Add("Level one node")
level2 = level1.Nodes.Add( "Level two node" )
level3 = level2.Nodes.Add( "Level three node" )

That's fine, so long as you're only adding to the node you processed
last. If you need to find an arbitrary node that's already /in/ the
TreeView and add a new child node to /that/, it's just a little more
complicated, since the TreeView (unlike its VB "proper" predecessor)
doesn't expose any sort of "key" for each Node by which you could
access any Node directly.

HTH,
Phill W.
 
P

poldoj

dear Phill. W, thanks for your reply. Indeed is exactly that I mean. I have
an "already-in-the-treeview" node, and then I would like to add a child at
that node. Not so easy task to accomplish after your words :(

complicated, since the TreeView...</quote>

Thanks however
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top