AddItem不填充组合框中的选项 [英] AddItem not populating options in combo box
问题描述
我有以下表格.我已经按照 .AddItem"
添加了每个项目,但是它们没有填充在用户窗体的 combobox
中.
I have the following form. I've added each item as per .AddItem ""
, however they are not populating in the combobox
in the userform.
(这是在单独的模块中,引用用户表单)
(This is in a separate module referring to the userform)
代码
With frmForm
.txtFirst.Value = ""
.txtLast.Value = ""
.txtYear.Value = ""
.cmbSchool.Clear
.cmbSchool.AddItem "Harvard"
.cmbSchool.AddItem "Northwestern"
.cmbSchool.AddItem "UCBerkley"
.cmbSchool.AddItem "Stanford"
.cmbSchool.AddItem "NYU"
.cmbSchool.AddItem "UoT"
.cmbSchool.AddItem "UBC"
.cmbSchool.AddItem "RMC"
End With
谢谢!
推荐答案
您的问题是,在UserForm代码模块中,您不应引用表单的默认名称(将其视为蓝图,此 class 的其他实例),但实际上正在运行的当前对象实例-例如通过在控件前面加上 Me.
限定符.
Your issue is that within the UserForm code module you shouldn't reference the form's default name (think it as a blue print for further instances of this class), but the current object instance it is actually run - e.g. by prefixing controls with the Me.
qualifier.
这确实假定代码已移至表单自己的代码模块中- Initialize
处理程序将是实现此目的的好地方.
This does assume the code is moved into the form's own code module - the Initialize
handler would be a good place for this.
如何填充用户窗体组合框
a)通过 .AddItem
方法
a) Populating via .AddItem
method
With Me.cmbSchool ' don't refer to the form's default instance
.Clear
.AddItem "Harvard"
.AddItem "Northwestern"
.AddItem "UCBerkley"
.AddItem "Stanford"
.AddItem "NYU"
.AddItem "UoT"
.AddItem "UBC"
.AddItem "RMC"
End With
b)通过数组填充
通过直接将一个数组分配给框的 .List
属性来选择此方法,您可以将代码缩短为:
Choosing this approach by assigning an array directly to the box'es .List
property, you can shorten code to:
Dim SchoolList As Variant
SchoolList = "Harvard,Northwestern,UCBerkley,Stanford,NYU,UoT,UBC,RMC"
Me.cmbSchool.List = Split(SchoolList, ",")
根据评论进行编辑/2020-03-30
您可以定义实例的含义吗?"
"Can you define what you mean by instance?"
一个 class -一个UserForm仅代表一种特殊类型的类模块-可以视为对象模板的一种.
A class - and a UserForm only represents a special type of class modules - can be regarded as sort of object template.
类对象的所谓实例(基于UserForm |类提供的所有属性,方法和事件)将在运行时作为具有访问权限的► New
对象创建.属性,即(可能是重复地)更新" 到您已声明并设置为内存的当前对象.
A so called instance of the class object (based on all properties, methods and events provided by the UserForm|class)
will be created at runtime as a ►New
object with access to the "." properties , i.e. it's getting 'newed' (possibly repeatedly) to the current object you have declared and set to memory.
如果您希望从单独的模块(而不是表单的 UserForm_Initialize()
处理程序)中调用该过程,则可以保留对象/实例(只要您需要它)" 通过如下所示的正式对象设置(或者: Dim myFrm As New frmForm
).
If you prefer to call the procedure from a separate module (instead in the form's UserForm_Initialize()
handler), you could "hold on to the object/instance (as long as you need it)"
by formal object setting as shown below (alternatively: Dim myFrm As New frmForm
).
标准模块中的示例调用
Sub ShowFormExample()
Dim myFrm As frmForm ' declare myFrm as object type belonging to your form's class
Set myFrm = New frmForm ' set myFrm as new object instance to memory
FillItems myFrm ' << call procedure FillItems
' or integrate code here: Dim .../ SchoolList = ... / myFrm.cmbSchool.List = ...
myfrm.Show ' form's .Show-method; argument equals vbModal by default
End Sub
过程 FillItems
Procedure FillItems
Sub FillItems(myFrm As UserForm)
With myFrm
Dim SchoolList As Variant
SchoolList = "Harvard,Northwestern,UCBerkley,Stanford,NYU,UoT,UBC,RMC"
.cmbSchool.List = Split(SchoolList, ",")
End With
End Sub
推荐读物
- UserForm1.Show?
- 有关无模式表单的信息,请参见正确销毁无模式Userform实例
这篇关于AddItem不填充组合框中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!