我基本上试图在用户表单上显示不同的文本框集,具体取决于组合框所采用的值。我创建了一个类模块调用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

在userform的代码中,我动态添加了这样的组合框:
'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在评论中提供了此问题的正确解决方案。
答案 0 :(得分:0)
设置txtB4.Visible =" False"应该这样做。也许尝试重绘表格。在visible =" false"。
之后尝试这些UserForm1.Repaint
或让doevents让Windows完成任何更新。
DoEvents
如果是失败的值检查,请尝试.Text属性。
If (Me.Cbx.Text = "LIST") Then
.Text为您提供显示的内容。
.Value为您提供列表项的指定值。
Distinction between using .text and .value in VBA Access
请注意该帖子中关于组合框的评论。