-
Jan 12th, 2022, 06:29 PM
#1
TreeView Node References
I know I'm correct about this, but I just wanted to say it out-loud anyway.
I've got a TreeView to which I add several nodes. (Nesting of nodes, parent/child, really doesn't matter for this.)
Now, as part of my processing, I hang onto some references of specific nodes (Dim oNode As Node, and then Set it). And then, I subsequently delete the node from the tree (tvw.Nodes.Remove SomeIndex).
Now, the node is visually gone from the tree, but I've still got an instantiation because I've still got a reference, correct? And the node won't be completely gone until I've cleared all references (set to Nothing, or fall out of scope). Correct?
It's just the disappearing visually that was a bit of a mind-warp for me.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 12th, 2022, 07:01 PM
#2
Re: TreeView Node References
It's not really that confusing. You have a reference to the node but the TreeView doesn't so of course it won't show up visually. The TreeView has no awareness of it. It might as well not exist as far as the TreeView is concerned. You have to think of it in terms of who has the reference to the Node object.
-
Jan 12th, 2022, 07:06 PM
#3
Re: TreeView Node References
Originally Posted by Elroy
Now, as part of my processing, I hang onto some references of specific nodes (Dim oNode As Node, and then Set it). And then, I subsequently delete the node from the tree (tvw.Nodes.Remove SomeIndex).
I don't have that much experience in usage of the TreeView in VB6, I've only ever used it a couple times but I have used it a lot more in VB.Net and I tend to build the entire node structure purely using Node objects and I just add the base Node to the TreeView afterwards. My memory is a little fuzzy but VB6 TreeView Node objects should be capable of being arranged entirely without the use of an actual TreeView just as they are in VB.Net. Nodes are just objects like any other and have no special meaning without a TreeView.
-
Jan 12th, 2022, 07:28 PM
#4
Re: TreeView Node References
Originally Posted by Niya
I tend to build the entire node structure purely using Node objects and I just add the base Node to the TreeView afterwards.
What if you have multiple root nodes? I try to not use nesting where it's not needed. It also takes more horizontal screen/form space. But anyway, yeah, it's a good point that the nodes don't really need a TreeView control.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 12th, 2022, 08:04 PM
#5
Re: TreeView Node References
your reference will keep that class instance alive (node object) regardless if the treeview itself has released its reference to it.
some properties of it will be in a weird state and throw errors like if you try to access its children. Its kind of a half dead object
as its surrounding context has been invalidated so be careful how you use it. Even if alive its pretty much a stale reference.
Code:
Dim n As Node
Private Sub Form_Load()
Set n = tv.Nodes.Add(, , , "first")
tv.Nodes.Add n, tvwChild, , "child"
MsgBox n.Children
tv.Nodes.Clear
End Sub
Private Sub Command1_Click()
MsgBox n.Text
MsgBox n.Children 'error the items control has been deleted
End Sub
-
Jan 12th, 2022, 08:29 PM
#6
Re: TreeView Node References
Originally Posted by dz32
some properties of it will be in a weird state and throw errors like if you try to access its children. Its kind of a half dead object
as its surrounding context has been invalidated so be careful how you use it. Even if alive its pretty much a stale reference.
Yea, this is what my memory was fuzzy on. I remember VB6 TreeNode objects being weird like that. I've fallen into the habit of dealing entirely with the nodes themselves without involvement of a TreeView since moving to VB.Net. For example:-
Code:
Dim t As New TreeNode
t.Text = "Root"
t.Nodes.Add(New TreeNode With {.Text = "Child 1"})
t.Nodes.Add(New TreeNode With {.Text = "Child 2"})
t.Nodes.Item(0).Nodes.Add(New TreeNode With {.Text = "Child 1 of Child 1"})
t.Nodes.Item(1).Nodes.Add(New TreeNode With {.Text = "Child 1 of Child 2"})
'Add the entire tree structure to TreeView
'by adding root Node
TreeView1.Nodes.Add(t)
Is there no way to do this in VB6?
-
Jan 12th, 2022, 08:35 PM
#7
Re: TreeView Node References
Originally Posted by Elroy
What if you have multiple root nodes?
An array or list could represent all the root nodes and through that you access the child nodes.
But all this is probably moot now since dz32 pointed out that Node objects in VB6 don't work correctly when orphaned from a TreeView.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|