Treeview - problem clearing nodes...

  • Thread starter davidaustinarcher
  • Start date
D

davidaustinarcher

Hi,

I've been having a problem clearing nodes in my treeview. Given the
following node setup:

Grandad
---Dad
------Son

There are occasions when the root tree node should be set as Dad.
However, when setting it back as Grandad I have a treeview which ends
up looking like so:

Dad
---Son
Grandad

I used a shared root node (Grandad) to store my node collection. You
can re-create this with the following code:

Public Class Form1
Dim RootNode As TreeNode

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
RootNode = New TreeNode("Grandad")
RootNode.Nodes.Add("Dad")
RootNode.Nodes(0).Nodes.Add("Son")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add(RootNode)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add(RootNode.Nodes(0))
End Sub
End Class

Is this a bug or am I doing something wrong?

Best regards,

David
 
J

Jeff Hopper

David - The reason for this behavior is stumping me, but it's got to have
something to do with the way the TreeView control is referencing and
manipulating the actual class-level TreeNode object, because if you use a
freshly generated TreeNode each time you need to change the TreeView's root
node, the behavior is as desired. For example, you can write a function that
generates and returns the TreeNode (instead of having a class-level RootNode
variable):

Private Function GenerateRootNode() As TreeNode
Dim RootNode As TreeNode

RootNode = New TreeNode("Grandad")
RootNode.Nodes.Add("Dad")
RootNode.Nodes(0).Nodes.Add("Son")

Return RootNode
End Function

and then reference it like this:

TreeView1.Nodes.Add(GenerateRootNode)

Strangely, if you inspect the original RootNode variable (using your
original code) after the root of the TreeView control has been changed, it
still appears to be intact with the original hierarchy, but the TreeView
clearly indicates otherwise. I assume you're trying to avoid re-generating
the tree each time you need to re-set the visible root node, but I haven't
figured out how to accommodate that with a re-used variable yet. I'll be
curious to hear if someone else has a good explanation for this, and I'll
let you know if I come across one myself...

Jeff Hopper
Hopper Consulting, Inc.
 
D

davidaustinarcher

David - The reason for this behavior is stumping me, but it's got to have
something to do with the way the TreeView control is referencing and
manipulating the actual class-level TreeNode object, because if you use a
freshly generated TreeNode each time you need to change the TreeView's root
node, the behavior is as desired. For example, you can write a function that
generates and returns the TreeNode (instead of having a class-level RootNode
variable):

Private Function GenerateRootNode() As TreeNode
Dim RootNode As TreeNode

RootNode = New TreeNode("Grandad")
RootNode.Nodes.Add("Dad")
RootNode.Nodes(0).Nodes.Add("Son")

Return RootNode
End Function

and then reference it like this:

TreeView1.Nodes.Add(GenerateRootNode)

Strangely, if you inspect the original RootNode variable (using your
original code) after the root of the TreeView control has been changed, it
still appears to be intact with the original hierarchy, but the TreeView
clearly indicates otherwise. I assume you're trying to avoid re-generating
the tree each time you need to re-set the visible root node, but I haven't
figured out how to accommodate that with a re-used variable yet. I'll be
curious to hear if someone else has a good explanation for this, and I'll
let you know if I come across one myself...

Jeff Hopper
Hopper Consulting, Inc.














- Show quoted text -

Jeff,

Thank you for your reply. You are correct in that I am trying to avoid
re-loading my node collection, as there are sometimes thousands of
nodes.

The more I look at this I think it may be a bug with the treeview.
However I would be astounded if no-one else had encountered the
issue.

Best regards,

David
 

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