【问题标题】:How to click out of Combobox?如何点击退出组合框?
【发布时间】:2021-10-21 09:51:39
【问题描述】:

我有一个包含MatchEntry 1-fmMatchEntryCompleteMatchRequired True属性的组合框。

我需要它为 true 以防止组合框中的任何无效条目。我不想将其设为Style 2-fmStyleDropDownList,而是将其设为Style 0-fmStyleDropDownCombo,因为我有大约 1000 个项目可供选择。

此设置有效,除非您不小心单击了组合框并尝试将其退出。你不断得到

无效的属性值

我是否可以对无效条目进行编码,这样我就不必将属性分配给True

【问题讨论】:

    标签: excel vba ms-access combobox userform


    【解决方案1】:

    如果将来有人遇到此问题,请弄清楚。我所做的只是保留上面的属性,并将此代码添加到我的用户表单中以用于组合框1。

    Private Sub ComboBox1_Change()
      If Me.ComboBox1.Value = "" Then
            'Match not required if zero lenght string
            Me.ComboBox1.MatchRequired = False
        Else
            'Match is required if other than zero length string
            Me.ComboBox1.MatchRequired = True
        End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      您可以使用组合 LostFocus 事件。它将检查该值是否与其中一个组合条目匹配,在不匹配的情况下发送消息,并删除该组合值。如果我的建议不够好,它也可以做其他事情:

      Private Sub ComboBox1_LostFocus()
          If ComboBox1.Value = "" Then Exit Sub
          Dim cbVal As Variant, boolFound As Boolean, i As Long
          cbVal = ComboBox1.Value
          For i = 0 To ComboBox1.ListCount - 1
              If cbVal = ComboBox1.list(i) Then boolFound = True: Exit For
          Next i
          If Not boolFound Then _
              MsgBox "The value """ & cbVal & """ does not exist between the combo items" & vbCrLf & _
                          "It will be deleted", vbInformation, "Illegal entry": ComboBox1.Value = ""
      End Sub
      

      MatchRequired 应保持为False(默认)...

      【讨论】:

      • OP 仅指用户窗体控件(afaik 没有 LostFocus 事件)?
      • @T.M.只看OP问题不容易理解...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多