本文介绍了VBA MSFORMS 与控件 - 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向用户窗体添加控件时,以下有什么区别.我不知道什么时候适合使用其中任何一种.

将 aButton1 变暗为 MSFORMS.CommandButton将 aButton2 调暗为 Control.CommandButton将 aButton3 调暗为 CommandButton
解决方案

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

您可以在该列表中看到 CommandButtonControl:

要在代码中声明类型为 CommandButton 的变量:

Dim button1 As MSForms.CommandButton将 button2 调暗为 CommandButton

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

Dim button1 As MSForms.CommandButtonDim button2 作为 MSFoo.CommandButton

要在代码中声明 Control 类型的变量:

Dim controlObject As MSForms.Control

使用 Control 类型的变量,就像控件的基类一样.例如.枚举控件集合:

对于 Me.Controls 中的每个 controlObjectDebug.Print VBA.TypeName(controlObject)下一个控制对象

或者作为函数中的参数,它不仅需要一种类型的控制:

Private Sub PrinControlName(ByRef c As MSForms.Control)Debug.Print c.Name结束子

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

这是本地创建的同名类的示例,例如 MSForm.CommandButton:

以及 TypeNameTypeOf 在这种情况下如何工作:

选项显式私有 m_buttonMsForms 作为 MSForms.CommandButton私有 m_buttonLocal 作为命令按钮私有子 UserForm_Initialize()设置 m_buttonMsForms = Me.Controls.Add( _"Forms.CommandButton.1", "testMsButton", True)设置 m_buttonLocal = 新命令按钮m_buttonLocal.Name = "testLocalButton"Debug.Print "我们有两种不同按钮类型的两个实例:" &_m_buttonLocal.Name &" 和 " &m_buttonMsForms.Name' 使用 TypeName 函数检查实例' TypeName 函数返回相同的结果如果 VBA.TypeName(m_buttonMsForms) = VBA.TypeName(m_buttonLocal) 那么Debug.Print "两种按钮类型的 TypeName 返回相同的结果"万一' 使用 TypeOf 运算符检查实例' TypeOf 不适用于未初始化的对象如果 m_buttonLocal 什么都不是,或者 m_buttonMsForms 什么都不是,那么 _退出子' TypeOf 运算符可以区分' 本地声明的 CommandButton 类型和 MSForms CommandButton如果 TypeOf m_buttonLocal 是 MSForms.CommandButton 那么 _Debug.Print "m_buttonLocal 是 MSForms.CommandButton"如果 TypeOf m_buttonMsForms 是 CommandButton 那么 _Debug.Print "m_buttonMsForms 是命令按钮"如果 TypeOf m_buttonLocal 是 CommandButton 那么 _Debug.Print "m_buttonLocal 是 CommandButton"如果 TypeOf m_buttonMsForms 是 MSForms.CommandButton 那么 _Debug.Print "m_buttonMsForms 是 MSForms.CommandButton"结束子

输出:

我们有两种不同按钮类型的两个实例:testLocalButton 和 testMsButton两种按钮类型的 TypeName 返回相同的结果m_buttonLocal 是命令按钮m_buttonMsForms 是 MSForms.CommandButton

When adding controls to a userform, what is the difference between the following. I am confused as to when it is appropriate to use any particular one of these.

Dim aButton1 as MSFORMS.CommandButton
Dim aButton2 as Control.CommandButton
Dim aButton3 as CommandButton
解决方案

Add UserForm first. Then in VBA IDE press F2, the object browser appears. In the upper left corner is combo box, select MSForms. In the classes list you can see the classes which belongs to MSForms object library.

You can see CommandButton and Control in that list:

To declare a variable of type CommandButton in your code:

Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton

The variable button1 is of type CommandButton from MSForms for sure. The button2 could be your own class defined in your local VBA Project ... but if your local VBA project doesn't contain any class with such name it will be considered from MSForms as well. However if you reference another object library say 'MSFoo' which will contain class CommandButton as well you will have to declare them fully qualified like this:

Dim button1 As MSForms.CommandButton
Dim button2 As MSFoo.CommandButton

To declare a variable of type Control in your code:

Dim controlObject As MSForms.Control

Use variable of type Control like kind of a base class for controls. E.g. to enumarate Controls collection:

For Each controlObject In Me.Controls
    Debug.Print VBA.TypeName(controlObject)
Next controlObject

Or as a parameter in function which expects not just only one type of control:

Private Sub PrinControlName(ByRef c As MSForms.Control)
    Debug.Print c.Name
End Sub

So using fully qualified names like MSForms.CommandButton is in general approprite i think. Using Control.CommandButton is wrong and won't compile until you reference some object library named 'Control' with class CommandButton in it.

EDIT:

Here an example of locally created class with same name like MSForm.CommandButton:

And how TypeName and TypeOf work in this case:

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

这篇关于VBA MSFORMS 与控件 - 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:54