gpt4 book ai didi

VBA MSFORMS 与控件 - 有什么区别

转载 作者:行者123 更新时间:2023-12-04 16:40:07 29 4
gpt4 key购买 nike

向用户窗体添加控件时,以下有什么区别。我很困惑何时适合使用其中任何一种。

Dim aButton1 as MSFORMS.CommandButton
Dim aButton2 as Control.CommandButton
Dim aButton3 as CommandButton

最佳答案

先添加用户表单 .然后在 VBA IDE 按 F2 , 对象浏览器出现。在左上角是组合框,选择 MSForms .在类列表中,您可以看到属于 MSForms 对象库的类。

你可以看到命令按钮 控制在该列表中:

enter image description here

在你的代码中声明一个 CommandButton 类型的变量:

Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton

变量 button1 肯定是 MSForms 中的 CommandButton 类型。 button2 可以是您在本地 VBA 项目中定义的自己的类 ...但如果您的本地 VBA 项目不包含任何具有此类名称的类,它也会从 MSForms 中考虑。然而 如果您引用另一个对象库 说'MSFoo',它也将包含类CommandButton,你必须声明它们 完全合格 像这样:
Dim button1 As MSForms.CommandButton
Dim button2 As MSFoo.CommandButton

在你的代码中声明一个 Control 类型的变量:
Dim controlObject As MSForms.Control

使用 Control 类型的变量,就像控件的基类一样。例如。枚举控件集合:
For Each controlObject In Me.Controls
Debug.Print VBA.TypeName(controlObject)
Next controlObject

或者作为函数中的参数,它不仅需要一种类型的控制:
Private Sub PrinControlName(ByRef c As MSForms.Control)
Debug.Print c.Name
End Sub

因此,我认为使用像 MSForms.CommandButton 这样的完全限定名称通常是合适的。使用 Control.CommandButton 是错误的,除非您引用名为“Control”的对象库,其中包含类 CommandButton,否则不会编译。

编辑:

这里是 的例子本地创建的同名类 像 MSForm.CommandButton:
enter image description here

以及如何 类型名称 类型在这种情况下工作:
Option Explicit

Private m_buttonMsForms As MSForms.CommandButton
Private m_buttonLocal As CommandButton

Private Sub UserForm_Initialize()
Set m_buttonMsForms = Me.Controls.Add( _
"Forms.CommandButton.1", "testMsButton", True)
Set m_buttonLocal = New CommandButton

m_buttonLocal.Name = "testLocalButton"

Debug.Print "We have two instances of two different button types: " & _
m_buttonLocal.Name & " and " & m_buttonMsForms.Name

' Check instances with TypeName function
' TypeName function returns same results
If VBA.TypeName(m_buttonMsForms) = VBA.TypeName(m_buttonLocal) Then
Debug.Print "TypeName of both buton types returns same result"
End If

' Check instances with TypeOf operator
' TypeOf doen't work with not initialised objects
If m_buttonLocal Is Nothing Or m_buttonMsForms Is Nothing Then _
Exit Sub

' TypeOf operator can distinguish between
' localy declared CommandButton type and MSForms CommandButton
If TypeOf m_buttonLocal Is MSForms.CommandButton Then _
Debug.Print "m_buttonLocal Is MSForms.CommandButton"

If TypeOf m_buttonMsForms Is CommandButton Then _
Debug.Print "m_buttonMsForms Is CommandButton"

If TypeOf m_buttonLocal Is CommandButton Then _
Debug.Print "m_buttonLocal Is CommandButton"

If TypeOf m_buttonMsForms Is MSForms.CommandButton Then _
Debug.Print "m_buttonMsForms Is MSForms.CommandButton"

End Sub

Output:

We have two instances of two different button types: testLocalButton and testMsButton
TypeName of both buton types returns same result
m_buttonLocal Is CommandButton
m_buttonMsForms Is MSForms.CommandButton

关于VBA MSFORMS 与控件 - 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15457262/

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