VBA MSFORMS 与控件 - 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15457262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA MSFORMS vs Controls - whats the difference
提问by TYale
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
回答by dee
Add UserForm first. Then in VBA IDE press F2, the object browserappears. 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.
首先添加用户窗体。然后在VBA IDE 中按 F2,出现对象浏览器。在左上角是组合框,选择MSForms。在类列表中,您可以看到属于 MSForms 对象库的类。
You can see CommandButtonand Controlin that list:
您可以在该列表中看到CommandButton和Control:
To declare a variable of type CommandButton in your code:
要在代码中声明类型为 CommandButton 的变量:
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 librarysay 'MSFoo' which will contain class CommandButton as well you will have to declare them fully qualifiedlike this:
变量 button1 肯定是 MSForms 中的 CommandButton 类型。该BUTTON2可以在您的本地VBA项目定义自己的类...但如果当地的VBA项目不包含这样的名字就会从MSForms被视为以及任何类。但是,如果您引用另一个对象库,例如“MSFoo”,它将包含类 CommandButton,您将必须像这样声明它们是完全限定的:
Dim button1 As MSForms.CommandButton
Dim button2 As MSFoo.CommandButton
To declare a variable of type Control in your code:
要在代码中声明 Control 类型的变量:
Dim controlObject As MSForms.Control
Use variable of type Control like kind of a base class for controls. E.g. to enumarate Controls collection:
使用 Control 类型的变量,就像控件的基类一样。例如枚举控件集合:
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.
因此,我认为使用像 MSForms.CommandButton 这样的完全限定名称通常是合适的。使用 Control.CommandButton 是错误的,除非您引用名为“Control”的对象库,其中包含类 CommandButton,否则不会编译。
EDIT:
编辑:
Here an example of locally created class with same namelike MSForm.CommandButton:
这里有一个本地创建的类的例子,同名,如 MSForm.CommandButton:
And how TypeNameand TypeOfwork in this case:
以及TypeName和TypeOf在这种情况下如何工作:
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
输出:
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
回答by Doug Glancy
Numbers 1 and 3 create the same type of control. The first statement is just fully qualified - equivalent to using Dim ws as WorkSheet or Dim ws as Excel.WorkSheet.
数字 1 和 3 创建相同类型的控件。第一个语句是完全限定的 - 相当于使用 Dim ws 作为 WorkSheet 或 Dim ws 作为 Excel.WorkSheet。
I'm not familiar with the 2nd type - "Control.CommandButton" - and it doesn't compile for me inside a userform in Excel 2010.
我不熟悉第二种类型 - “Control.CommandButton” - 它不会在 Excel 2010 的用户窗体中为我编译。