Hello,
I have a form with a treeview on it, and i have a class who have a
property returning a Treeview
I want to affect my treeview with the property of the class
Private Sub Form1_Load....
Dim myclass as ClassTV
TreeView1 = myclass.TV
end sub
But it doesn't work!!!
I have the same problem with a label
Dim lbl as new label
lbl.text="Test"
label1=lbl (label1 is the name of the control who is in my form)
If someone can help me
Thanks a lot
This is why:
Under the page called Form1.designer.vb (expand the form in the
explorer) you see the line that says:
dim TreeView1 as New Treeview()
This does two things. First it creates a pointer called TreeView1 that
can point to ANY treeview control. The second thing it does is create
a new treeview object in memory and assign it to your pointer. It
could be written seperately like this:
dim TreeView1 as TreeView() 'create reference that can point to a
treeview
TreeView1 = new TreeView() 'create a treeview object in memory and
store it's address in TreeView1
If you search through Form1.designer.vb you will see another line of
code that reads:
form1.controls.add(TreeView1)
(if your using panels, or some container object like a tab control or
group box this line will be different - but somewhere
".controls.add(TreeView1)" will be present)
This associates the treeview with the form. The contents of your
memory could be illistrated like this:
Form1 Object in memory
|
|
-------TreeView1 (reference in memory)
| |
| |
| -----> actual TreeView Object in memory (the
treeview1 reference points to the treeview)
| ^
------- Controls (reference in memory)| (the
controls reference points to the treeview)
When form_load runs you set TreeView1 to be a new treeview
TreeView1 = myClass.TV
Now the memory looks like this:
Form1 Object
|
|
|
-------TreeView1 reference
| |
| ------> TreeView Object (the one returned by
myClass.tv)
|
|
------- Controls
|
|
------> TreeView Object (the same one you've
always had)
So you see your not actually replacing the form's original treeview,
your replacing your own reference to it which is why the screen
doesn't change. To solve this you need to remove the original object
from the form's display and then readd it.
Me.Controls.Remove(TreeView1)
TreeView1 = MyClass.TV
Me.Controls.Add(TreeView1)
'optional
'treeview1.left = 300
'treeview1.top = 300
'treeview1.width = 300
If your wondering why .Remove(TreeView1) works. It's because even
though they are different references (TreeView1 and an item in the
Controls collection) they both point to the SAME object. (see the
first diagram with an illistration)
So remove the original object from the form, then update TreeView1
reference, then add the control back on to the form again. You may
need to set the position and size attributes again.
Hopefully this explains the theory behind your problem and helps you
understand it.
Phillip Taylor B.Sc. (HONS)
Systems Developer