如何获取 MSForms.ComboBox 对象所在的 UserForm? [英] How does one get the UserForm where MSForms.ComboBox Object is located?

查看:23
本文介绍了如何获取 MSForms.ComboBox 对象所在的 UserForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个名为FilterLine"的类,它由 3 个组合框组成,这些组合框根据所有其他已选择的过滤器进行填充.下面是代码的简化:

Hi got a Class Called "FilterLine", It consist of 3 ComboBoxes which are filled depending on all other already selected filters. Here a simplification of the code:

Option Explicit

Public WithEvents Filter As MSForms.Combobox
Public WithEvents Operator As MSForms.Combobox
Public WithEvents Options As MSForms.Combobox
Public index As Integer

' This sub adds a new FilterLine and formats it

Public Sub Add()

' Do Stuff with form in which it is embedded

frmFilter.Height = frmFilter.Height + 50

End Sub

' Other subs

其中 frmFilter 是用户表单.这是相当不优雅的,因为我将用户表单称为名称,因此如果不更改另一个用户表单,就无法使用该类.用户表单应始终是默认过滤器所在的用户表单.但是如何从 ComboBox 获取用户表单?

Where frmFilter is the Userform. This is rather unelegant since I refer to the userform as a name and therefore the class could not be used without changes in another userform. The Userform should always be the userform where the default filter is located. But how does one get the userform from a ComboBox?

例如使用范围对象,您可以这样做:

For example with a range object you can do this:

dim rng as Range

set rng = 'Whatever you want

rng.Worksheet.activate

这将激活范围所在的工作表.但是如何从 MSForms.ComboBox 获取 UserForm 实例呢?

This will activate the worksheet where the range is located. But how does one get the UserForm instance from a MSForms.ComboBox?

为了澄清我使用以下代码调用用户表单:

To clarify I call the Userform using this code:

Sub testFilter()
Dim Filterm As FilterModel

Set Filterm = New FilterModel

With New frmFilter
    Set .Model = Filterm
    .SetDefaultFilter' This is the interesting part
    .Show
End With
End Sub 

然后 UserForm Instance.SetDefaultFilter 设置默认过滤器:

Then the UserForm Instance.SetDefaultFilter sets the default filter with:

Public Sub SetDefaultFilterLine()

Dim DefaultFilterLine As New FilterLine
Set DefaultFilterLine.Filter = frmFilter.DefaultFilter
Set DefaultFilterLine.Operator = frmFilter.DefaultOperator
Set DefaultFilterLine.Options = frmFilter.DefaultOptions
DefaultFilterLine.index = 1

Me.Model.FilterCol.Add DefaultFilterLine

' This doesn't work because I cannot refer to the instance I newed up in the testFilter Sub. 
DefaultFilterLine.Add
End Sub

DefaultFilterLine.Add 不起作用,因为代码使用了 frmFilter(这是它的默认实例并且没有分配模型).因此问题应该是:

DefaultFilterLine.Add doesn't work because the code uses frmFilter (which is it's default instance and has no model assigned to it). Therefore the question should probably be:

如何引用另一个模块(在本例中为类模块)中的用户窗体实例?

编辑 2:

这是frmFilter的相关代码:

This is the relevant code of frmFilter:

Public DisableEvents As Boolean

Private Type TView
    Model As FilterModel
    IsCancelled As Boolean
    IsBack As Boolean
End Type

Private this As TView

Public Property Get Model() As FilterModel
    Set Model = this.Model
End Property

Public Property Set Model(ByVal value As FilterModel)
    Set this.Model = value
    'Validate
End Property

Model 是 Type FilterModel 的 UserForm 的一个属性.FilterModel(目前)仅包含一个整数 N 和一个集合,其中存储了所有 FilterLine.

Model is a property of the UserForm of the Type FilterModel. FilterModel (currently) only consists of a Integer N and a collection where all the FilterLine are stored.

推荐答案

你会使用 Filter.Parent.

举个例子:

创建一个名为 Class1 的类.
添加此代码:

Create a class named Class1.
Add this code:

Public WithEvents Filter As MSForms.ComboBox

Private Sub Filter_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox Filter.Parent.Name
End Sub

UserForm1 包含一个名为 ComboBox1 的组合框.
将此代码添加到表单中:

UserForm1 contains a single combo-box named ComboBox1.
Add this code to the form:

Private colEvents As Collection

Private Sub UserForm_Initialize()

    Dim MyEvents As Class1

    Set colEvents = New Collection
    Set MyEvents = New Class1

    Set MyEvents.Filter = Me.ComboBox1
    colEvents.Add MyEvents

End Sub

打开表单并双击组合框,它会告诉您表单的名称.

Open the form and double-click the combobox and it will tell you the name of the form.

这篇关于如何获取 MSForms.ComboBox 对象所在的 UserForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆