gpt4 book ai didi

vba - 从 Treeview 对象变量设置 Treeview 控件?

转载 作者:行者123 更新时间:2023-12-01 09:26:37 29 4
gpt4 key购买 nike

我正在尝试设置一个 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

最佳答案

您不能从变量设置表单控件。您可以做的是声明一个变量,然后将表单控件设置为该变量并修改该变量的属性。使用 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

然后简单地执行以下函数(注意您不需要在函数内声明变量)。

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

关于vba - 从 Treeview 对象变量设置 Treeview 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943185/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com