VBA 用户窗体对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/543243/
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 UserForm Object
提问by user51498
I'm really struggling with something here. I have a class module, let's call it FormMan which has a bunch of methods relating to the large number of userforms I have in my project. One particular method is to be called from a lot of different places and is pretty simple - it simply adds a user defined number of controls to a form and extends the forms height to accommodate these new controls.
我真的在这里挣扎。我有一个类模块,我们称之为 FormMan,它有一堆与我在我的项目中拥有的大量用户表单相关的方法。一种特殊的方法是从许多不同的地方调用,而且非常简单——它只是将用户定义的控件数量添加到窗体并扩展窗体高度以容纳这些新控件。
The user passes the number of controls and the userform.
用户传递控件的数量和用户表单。
oF.AddControlsToForm iNumberOfControls,frmTest
In FormMan class module:
在 FormMan 类模块中:
Public Sub Addcontrols(iNum as integer, oForm as userform)
//stuff happens here, oForm is used extensively
oForm.Height = i //object does not support this property or method
frmTest.Height = i //works
oForm.Show //object does not...
frmTest.show //works
end sub
In the Locals window, oForm does not have a height property, so fair enough. But oForm has been defined as frmTest. I can say oForm.BackColor = vbred
, and I can set ctl = oform.TextBox1
for example
在 Locals 窗口中,oForm 没有 height 属性,所以很公平。但是oForm已经被定义为frmTest。我可以说oForm.BackColor = vbred
,我可以设置ctl = oform.TextBox1
例如
This is meant to be a generic procedure that can add a bunch of controls to whatever form. I've tried loading and showing the form before assigning it to oForm.
这是一个通用过程,可以向任何表单添加一堆控件。在将表单分配给 oForm 之前,我尝试加载并显示该表单。
Why are height and show properties and methods of userforms but not of objects declared as userforms? What am I doing wrong?
为什么用户窗体的 height 和 show 属性和方法而不是声明为用户窗体的对象?我究竟做错了什么?
Really appreciate any help.
真的很感激任何帮助。
回答by Adam Ralph
The problem here is that the UserForm and frmTest objects are not of the same type. In fact, frmTest is a subtype of UserForm that extends it by adding the Height property, amongst other members.
这里的问题是 UserForm 和 frmTest 对象的类型不同。事实上,frmTest 是 UserForm 的一个子类型,它通过在其他成员中添加 Height 属性来扩展它。
You'll probably have to resort to declaring your function parameter as Object.
您可能不得不求助于将函数参数声明为 Object。
Public Sub Addcontrols(iNum as integer, oForm as Object)
This should work as you desire, although unfortunately you'll be sacrificing type safety. Most OO concepts tend to fall apart in the context of VBA.
这应该可以按您的意愿工作,但不幸的是,您将牺牲类型安全性。大多数面向对象的概念在 VBA 的上下文中往往会分崩离析。
回答by notnot
The thing first and foremost to remember about VBA is that it's quasi-OO, not full-OO. I've run into a lot of wacky problems around this issue and so I now tend to avoid relying entirely on VBA's consistency.
关于 VBA,首先要记住的是它是准面向对象的,而不是完全面向对象的。我在这个问题上遇到了很多古怪的问题,所以我现在倾向于避免完全依赖 VBA 的一致性。
That being said, try this out:
话虽如此,试试这个:
Dim fname = oForm.Name (or whatever the property is for this subobject)
With VBA.UserForms.Add(fname)
.Height = x
.Show
End With
Isn't it so pleasantly bizarre?!?! I don't have a code example ready and waiting, so I can't guarantee success, but this feels like the right approach.
是不是很令人愉快的奇怪?!?!我没有准备好等待的代码示例,所以我不能保证成功,但这感觉是正确的方法。