VBA 用户窗体对象

发布于 2024-07-13 09:58:52 字数 882 浏览 9 评论 0原文

我真的在这里遇到了一些困难。 我有一个类模块,我们称之为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-07-20 09:58:52

这里的问题是 UserForm 和 frmTest 对象不是同一类型。 事实上,frmTest 是 UserForm 的子类型,它通过在其他成员中添加 Height 属性来扩展它。

您可能不得不将函数参数声明为对象。

Public Sub Addcontrols(iNum as integer, oForm as Object)

这应该按照您的意愿工作,尽管不幸的是您将牺牲类型安全性。 大多数面向对象的概念在 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.

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.

始于初秋 2024-07-20 09:58:52

关于 VBA 首先要记住的一点是,它是准 OO,而不是完全 OO。 我在这个问题上遇到了很多奇怪的问题,所以我现在倾向于避免完全依赖 VBA 的一致性。

话虽这么说,试试这个:

Dim fname = oForm.Name (or whatever the property is for this subobject)
With VBA.UserForms.Add(fname)
   .Height = x
   .Show
End With

是不是很奇怪?!?! 我没有准备好并等待的代码示例,所以我不能保证成功,但这感觉是正确的方法。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文