VBA 用户窗体对象
我真的在这里遇到了一些困难。 我有一个类模块,我们称之为 FormMan,它有一堆与我的项目中的大量用户表单相关的方法。 一种特殊的方法是从许多不同的地方调用,并且非常简单 - 它只是将用户定义数量的控件添加到表单中,并扩展表单高度以容纳这些新控件。
用户传递控件的数量和用户窗体。
oF.AddControlsToForm iNumberOfControls,frmTest
在 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
在 Locals 窗口中,oForm 没有 height 属性,所以很公平。 但oForm已被定义为frmTest。 我可以说 oForm.BackColor = vbred
,并且我可以设置 ctl = oform.TextBox1
例如,
这意味着这是一个通用过程,可以将一堆控件添加到无论什么形式。 我尝试在将表单分配给 oForm 之前加载并显示该表单。
为什么 height 和 show 属性和方法是用户窗体的属性和方法,而不是声明为用户窗体的对象的属性和方法? 我究竟做错了什么?
非常感谢任何帮助。
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.
The user passes the number of controls and the userform.
oF.AddControlsToForm iNumberOfControls,frmTest
In FormMan class module:
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
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.
Why are height and show properties and methods of userforms but not of objects declared as userforms? What am I doing wrong?
Really appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是 UserForm 和 frmTest 对象不是同一类型。 事实上,frmTest 是 UserForm 的子类型,它通过在其他成员中添加 Height 属性来扩展它。
您可能不得不将函数参数声明为对象。
这应该按照您的意愿工作,尽管不幸的是您将牺牲类型安全性。 大多数面向对象的概念在 VBA 环境中往往会崩溃。
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.
You'll probably have to resort to declaring your function parameter 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 首先要记住的一点是,它是准 OO,而不是完全 OO。 我在这个问题上遇到了很多奇怪的问题,所以我现在倾向于避免完全依赖 VBA 的一致性。
话虽这么说,试试这个:
是不是很奇怪?!?! 我没有准备好并等待的代码示例,所以我不能保证成功,但这感觉是正确的方法。
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.
That being said, try this out:
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.