【发布时间】:2015-08-03 13:57:08
【问题描述】:
我基本上是在尝试根据组合框采用的值在用户窗体上显示不同的文本框集。我创建了一个名为 CControlEvents 的类模块,在其中我描述了当我更改组合框的值时应该发生的事件:
Private WithEvents mclsCbx As MSForms.ComboBox
Private mMyProperty As Integer
Public Property Set Cbx(ByVal clsCbx As MSForms.ComboBox): Set mclsCbx = clsCbx: End Property
Public Property Get Cbx() As MSForms.ComboBox: Set Cbx = mclsCbx: End Property
Public Property Get MyProperty() As Integer
MyProperty = mMyProperty
End Property
Public Property Let Transition(Value As Integer)
mMyProperty = Value
End Property
Private Sub mclsCbx_Change()
'Options NUM
Set Lbl4 = UserForm1.Frame1.Controls.Add("Forms.Label.1", "lbl3")
Set txtB4 = UserForm1.Frame1.Add("Forms.TextBox.1")
With txtB4
.name = "Unit" & mMyProperty
.Height = 15
.Width = 50
.Left = 500
.Top = 10 * mMyProperty * 3
.Value = "txtB4NUM"
End With
Lbl4.Caption = "Unité : "
Lbl4.Top = txtB4.Top
Lbl4.Left = 360
'Options LIST
Set Lbl3 = UserForm1.Frame1.Controls.Add("Forms.Label.1", "lbl3")
Set txtB3 = UserForm1.Frame1.Add("Forms.TextBox.1")
With txtB3
.name = "specMin" & mMyProperty
.Height = 15
.Width = 200
.Left = 410
.Top = 10 * mMyProperty * 3
.Value = "txtB3LIST"
End With
Lbl3.Caption = "Eléments : "
Lbl3.Top = txtB3.Top
Lbl3.Left = 360
If (Me.Cbx.Value = "NUM") Then
txtB3.Visible = False
txtB4.Visible = True
Else
If (Me.Cbx.Value = "LIST") Then
txtB4.Visible = False
txtB3.Visible = True
End If
End If
End Sub
在用户窗体的代码中,我动态添加了这样的组合框:
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
Set gclsControlEvents = New CControlEvents
Set gclsControlEvents.Cbx = oleCbx
Let gclsControlEvents.Transition = i
问题是,当我更改组合框的值时,它会显示相应的文本框,但不会删除其他文本框,而
If (Me.Cbx.Value = "LIST") Then
txtB4.Visible = False
txtB3.Visible = True
如果组合框的值为“LIST”,则应该将其中一个文本框设置为可见,而将另一个设置为不可见。
编辑:@Rory 在评论中给出了正确的解决方案。
【问题讨论】:
-
为什么每次更改组合框中的值时都要添加2个新标签?
-
这只是一个错误,每个标签都应该附加到一个文本框,因此会随之出现和消失。 @罗里
-
那就是问题所在。您可以隐藏每次添加的控件,但不能隐藏之前添加的控件。您应该将它们添加到用户表单代码中并将它们分配给类的属性,然后您可以根据需要简单地隐藏/显示。