【问题标题】:Combobox Event listener for dynamic user interface用于动态用户界面的组合框事件侦听器
【发布时间】: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个新标签?
  • 这只是一个错误,每个标签都应该附加到一个文本框,因此会随之出现和消失。 @罗里
  • 那就是问题所在。您可以隐藏每次添加的控件,但不能隐藏之前添加的控件。您应该将它们添加到用户表单代码中并将它们分配给类的属性,然后您可以根据需要简单地隐藏/显示。

标签: vba excel combobox


【解决方案1】:

设置 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

请注意该帖子中关于组合框的评论。

【讨论】:

  • 所以你没有得到任何错误,它只是没有隐藏 txtbox?您确定代码正在运行该部分吗?
  • 你确定已经到达代码行 txtB4.Visible = False 吗?
  • 是的,我刚刚通过在其后添加 MsgBox "Reached" 对其进行了测试,我认为 Rory 可能发现了问题。
猜你喜欢
  • 2012-10-10
  • 1970-01-01
  • 2015-11-01
  • 2018-12-12
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
相关资源
最近更新 更多