从树视图对象变量设置树视图控件?
Posted
技术标签:
【中文标题】从树视图对象变量设置树视图控件?【英文标题】:Treeview Control Set From Treeview Object Variable? 【发布时间】:2014-04-08 16:40:34 【问题描述】:我正在尝试设置 Treeview 对象,设置节点,然后更新控件以使值具有适当的格式。现在我有以下代码在我设置控件时有效,但不是来自变量的控件。如何从变量中设置本地控件?
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
【问题讨论】:
您是否尝试过对象变量的“Set”关键字:set TreeViewTest = TreeView0 以及为什么需要“从变量设置本地控件”而不是设置控件本身? 是的。它会引发对财产的无效使用。为什么不看看你是否可以而不是设置控件?随身携带更多工具从来都不是坏事。 请试试这个'Set Me.TreeViewTest.Object = TreeView0' 使用Set Me.TreeView0.Object = TreeViewTest
返回错误Object doesn't support this property or method
【参考方案1】:
您不能从变量中设置表单控件。您可以做的是声明一个变量,然后将表单控件设置为该变量并修改该变量的属性。使用 MSForms(可能是 Access 表单?)您还可以声明表单变量,然后在运行时将它们添加到表单中。
所以我发现您的代码存在两个问题。首先是因为 TreeControl 是一个 ActiveX 变量,所以您需要将 TreeView0
变量声明为 CustomControl
而不是 TreeView
。然后您需要将表单控件设置为变量。即Set TreeView0 = Me.TreeViewTest
。
所以代码是:
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
如果您想重用这段代码(为什么要从变量中设置它?)那么您只需将其更改为:
Private Sub Form_Load()
InitMyTree(Me.TreeViewTest)
InitMyTree(Me.TreeViewTest1)
End Sub
然后sinmply下面的函数(注意你不需要在函数中声明变量)。
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
【讨论】:
以上是关于从树视图对象变量设置树视图控件?的主要内容,如果未能解决你的问题,请参考以下文章