I am trying to setup a Treeview Object, Set the nodes, and then update a control to have the values appropriately formatted. Right now I have the following code that works when I have it setting a control, but not a control from a variable. How do I set a local control from a variable?
Private Sub Form_Load()
Dim iml As ImageList
Dim TreeView0 As TreeView
Set TreeView0 = New TreeView
Set iml = New ImageList
iml.ListImages.Add , "Open", LoadPicture("Folder\Open.bmp")
iml.ListImages.Add , "Closed", LoadPicture("Folder\Close.bmp")
iml.ListImages.Add , "Document", LoadPicture("Folder\Document.bmp")
iml.ListImages.Add , "test", LoadPicture("Folder\Document.bmp")
iml.ListImages.Add , "test2", LoadPicture("Folder\Document.bmp")
iml.ListImages.Add , "test3", LoadPicture("Folder\Document.bmp")
iml.ListImages.Add , "test4", LoadPicture("Folder\Document.bmp")
iml.ListImages.Add , "test5", LoadPicture("Folder\Document.bmp")
Set TreeView0.ImageList = iml
TreeView0.Nodes.Add , , "Open"
TreeView0.Nodes.Add , , "Closed"
TreeView0.Nodes.Add "Open", tvwChild, "Document"
TreeView0.Nodes.Add "Open", tvwChild, "test"
TreeView0.Nodes.Add "Open", tvwChild, "test2"
TreeView0.Nodes.Add "Closed", tvwChild, "test3"
TreeView0.Nodes.Add "Closed", tvwChild, "test4"
TreeView0.Nodes.Add "Closed", tvwChild, "test5"
TreeView0.Nodes(1).Image = "Closed"
TreeView0.Nodes(1).Text = "Closed [+]"
TreeView0.Nodes(2).Image = "Closed"
TreeView0.Nodes(2).Text = "Closed [+]"
TreeView0.Nodes(3).Image = "Document"
TreeView0.Nodes(3).Text = "Document"
TreeView0.Nodes(4).Image = "test"
TreeView0.Nodes(4).Text = "test"
TreeView0.Nodes(5).Image = "test2"
TreeView0.Nodes(5).Text = "test2"
TreeView0.Nodes(6).Image = "test3"
TreeView0.Nodes(6).Text = "test3"
TreeView0.Nodes(7).Image = "test4"
TreeView0.Nodes(7).Text = "test4"
TreeView0.Nodes(8).Image = "test5"
TreeView0.Nodes(8).Text = "test5"
'THIS LINE FAILS (Property Invalid)
TreeViewTest = TreeView0
End Sub
Private Sub TreeView0_DblClick() 'OK
If TreeView0.Nodes(1).Expanded = True Then
TreeView0.Nodes(1).Image = "Open"
TreeView0.Nodes(1).Text = "Open [-]"
ElseIf TreeView0.Nodes(1).Expanded = False Then
TreeView0.Nodes(1).Image = "Closed"
TreeView0.Nodes(1).Text = "Closed [+]"
End If
If TreeView0.Nodes(2).Expanded = True Then
TreeView0.Nodes(2).Image = "Open"
TreeView0.Nodes(2).Text = "Open [-]"
ElseIf TreeView0.Nodes(2).Expanded = False Then
TreeView0.Nodes(2).Image = "Closed"
TreeView0.Nodes(2).Text = "Closed [+]"
End If
End Sub
You can't set a form control from a variable. What you can do is declare a variable, then set the form control to that variable and modify that variable's properties. With MSForms (presumably Access forms?) you can also declare form variables and then add them to forms at runtime.
So I see two problems with your code. The first is that since the TreeControl is an ActiveX variable, you need to declare the TreeView0 variable as a CustomControl not a TreeView. Then you need to set the form control to the variable. ie Set TreeView0 = Me.TreeViewTest.
So the code would be:
Private Sub Form_Load()
Dim iml As ImageList
Dim TreeView0 As CustomControl
'Set TreeView0 = New TreeView -> not needed as you set the control to the variable
Set iml = New ImageList
Set TreeView0 = Me.TreeViewTest
'Do the rest of you initialisation here.
End Sub
If you want to reuse this code (why else would you want to set it from a variable?) then you simply change this to:
Private Sub Form_Load()
InitMyTree(Me.TreeViewTest)
InitMyTree(Me.TreeViewTest1)
End Sub
And then sinmply the following function (note you don't need to declare the variable within the function).
Private Sub InitMyTree(TreeView0 as CustomControl)
Dim iml As ImageList: Set iml = New ImageList
iml.ListImages.Add , "Open", LoadPicture("Folder\Open.bmp")
iml.ListImages.Add , "Closed", LoadPicture("Folder\Close.bmp")
'Finish initialisation here.
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With