TreeView problem (should be simple...)

M

Metal Rabbit

Hi guys

I'm needing to create a fairly complex TreeView, but am faltering on a very
basic step (as you will gather, I'm new-ish to .net).

Simplified, I can't get the creation of two nodes (with children) to work.



I've got a TreeView called tvwSword and have put the following code into the
Form1_Load:


Dim nodeParent As TreeNode
Dim nodeChild As TreeNode

nodeParent = New TreeNode("2001")
tvwSword.Nodes.Add(nodeParent) ' adds parent of 2001
nodeParent.Nodes.Add("2001/1") ' adds child of 2001/1 to parent of 2001

nodeParent = New TreeNode("2002")
tvwSword.Nodes.Add(nodeParent) ' adds parent of 2002
nodeParent.Nodes.Add("2002/1") ' adds child of 2002/1 to parent of 2002

' the above lines work fine

nodeParent = New TreeNode("2001")
nodeChild = New TreeNode("2001/2")
nodeParent.Nodes.Insert(1, nodeChild) ' adds child of 2001/2 to parent of
2001

' these 3 lines do nothing ?????

tvwSword.Show()



Now, I would have expected this to read:

2001
--- 2001/1
--- 2001/2
2002
--- 2002/1

but it doesn't insert the second child (2001/2).

If I change it to

nodeParent = New TreeNode("2001")
nodeChild = New TreeNode("2001/2")
nodeParent.Nodes.Add(nodeChild)

I get the same effect.

Under VB6.0 I would have used keys and the problem of children is resolved,
but I can't see how to do it in VB.Net. I think it may be something to do
with SelectedNode, but I can't see what.

What am I missing????

TIA

Rabbit
 
C

Cor

Hi Metal Rabbit
nodeParent = New TreeNode("2001")

Here you declare a new treeNode that you never add to the tree

To get your expected result, you can do.
nodeParent=tvwSword.Nodes(0)
nodeChild = New TreeNode("2001/2")
nodeParent.Nodes.Insert(1, nodeChild) ' adds child of 2001/2 to parent of

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

* "Metal Rabbit said:
' the above lines work fine

nodeParent = New TreeNode("2001")
nodeChild = New TreeNode("2001/2")
nodeParent.Nodes.Insert(1, nodeChild) ' adds child of 2001/2 to parent of

I don't understand what's the problem... Why not use something like
this?

\\\
Dim t As TreeNode = New TreeNode("Parent")
t.Nodes.Add("Child1")
t.Nodes.Add("Child2")
Me.TreeView1.Nodes.Add(t)
///
 
C

Cor

Hi Herfried,

I can say see my post, but I also see again disapearing messages in the
Microsoft newsgroups, is that with you also?

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
I can say see my post, but I also see again disapearing messages in the
Microsoft newsgroups, is that with you also?

No, for some reason everything works... I don't have any problems
accessing the groups today (but some others have).
 
A

Armin Zingler

Cor said:
I can say see my post, but I also see again disapearing messages in the
Microsoft newsgroups, is that with you also?

Probably due to reindexing of the messages in this group. Had some minor
problems yesterday night, too.
 
M

Metal Rabbit

Hi Cor:

That gets me closer, but what I'm trying to do is to add children to
existing nodes according to certain values. For instance, if I already have
high-level nodes of

2001
2002
2004
2005
2007
2009

and I read a field value (from a database) of "2004" (with another field of
"2004/12" elsewhere in the record), then I want to add a child of "2004/12"
to the high-level node of 2004, giving:

2001
2002
2004
----2004/12
2005
2007
2009

Do I need to store the indexes of each of the top level nodes, find the
appropriate index and then use

nodeParent=tvwSword.Nodes(index)

or is there a way to easily find the index 'on the fly'?

Thanks

Rabbit
 
C

Cor

Hi Metal Rabbit,

I could wait for it.

I have also answered your next qeustion
"2001" is a certain value.


I hope this helps?

Cor

\\\
For Each nd As TreeNode In tvwSword1.Nodes
If nd.Text = "2001" Then
ind = nd.Index
Exit For
End If
Next
nodeParent = tvwSword1.Nodes(ind)
ind = nodeParent.LastNode.Index()
nodeChild = New TreeNode("2001/" & (ind + 2).ToString)
nodeParent.Nodes.Insert(ind + 1, nodeChild) ' adds child of 2001/2 to parent
of 2001:
///
 

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