设置组合框的一些属性(滚动、编辑、跳转)

发布于 2024-12-10 12:07:51 字数 262 浏览 1 评论 0原文

我想为一些 VBA 表单组合框设置 3 个属性,但我不知道是否可行。 我不想让组合框可编辑。现在,如果用户在其中输入内容并提交表单,它将发送该值...我想让他仅从我在组合框中添加的值中进行选择。 我想让组合框中的项目列表可滚动。现在,如果使用滚动条,我可以滚动列表,但我不知道为什么不能使用鼠标滚动来滚动。 如果我开始打字,我想跳到某个项目。假设我在一个组合框中有一年中的月份...如果我开始输入 mar 我希望它跳到三月。我知道对于 html 表单来说,这个属性是默认的,但我不知道 VBA 表单... 多谢

There are 3 properties that I want to set for some VBA form comboboxes and I don't know if it's possible.
I don't want to let the combobox editable. Right now if the user types something in it that it submits the form it will send that value... I want to let him choose only from the values I added in the Combobox.
I want to make the list of items in the combobox scroll-able. Right now I'm able to scroll through the list if I use the scroll-bar but I don't know why I can't scroll with the mouse scroll.
And I want to jump to some item if I start typing. Let's say I have the months of the year in one combobox... if I start to type mar I want it to jump to march. I know that for the html forms this properties is by default but I don't know about VBA forms...
Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始于初秋 2024-12-17 12:07:51

在您想要的行为中,有些可以通过组合上的设置实现,其他则需要对

  1. 月份列表进行编码:将条目列表放在(隐藏)工作表上并命名范围。将 .RowSource 设置为该范围
  2. 键入时匹配:设置属性 .MatchEntry = fmMatchEntryComplete.MatchRequired = True
  3. 拒绝非列表条目:组合使用这些设置将允许您键入无效条目,但在提交时会拒绝它并弹出错误消息。如果您想在键入时默默地拒绝无效数据,则需要对其进行编码。
  4. 如果您希望将所选值返回到工作表,请将 .ControlSource 设置为单元格地址(最好是命名范围)
  5. 通过“...用鼠标滚动滚动...” 我假设你指的是鼠标滚轮。不幸的是,表单不支持鼠标滚轮滚动。您必须自己编写代码。有一个针对此问题的 Microsoft 补丁 此处(我自己还没有尝试过)

静默拒绝无效条目的示例代码

Private Sub cmbMonth_Change()
    Static idx As Long
    Dim Match As Boolean
    Dim i As Long

    If cmbMonth.Value = "" Then Exit Sub
    If idx = 0 Then idx = 1
    i = idx
    Match = False
    For i = 0 To cmbMonth.ListCount
        If cmbMonth.List((i + idx - 1) Mod cmbMonth.ListCount) Like cmbMonth.Value & "*" Then
           cmbMonth.ListIndex = (i + idx - 1) Mod cmbMonth.ListCount
           Match = True
           Exit For
        End If
    Next
    If Not Match Then
        cmbMonth.Value = Left(cmbMonth.Value, Len(cmbMonth.Value) - 1)
    End If
End Sub

Of the behaviours you want, some are possible with settings on the Combo, others you will need to code

  1. List of Months: Put a list of entries on a (hidden) sheet and name the range. Set .RowSource to that range
  2. Match as you type: Set properties .MatchEntry = fmMatchEntryComplete and .MatchRequired = True
  3. Reject non list entries: A Combo with these settings will allow you to type an invalid entry, but will reject it with an error message popup when you commit. If you want to silently reject invalid data as you type, you will need to code it.
  4. If you want the selected value returned to a sheet, set .ControlSource to a cell address (preferable a named range)
  5. By "...scroll with the mouse scroll..." I assume you mean the mouse wheel. Unfortunatley Forms don't support mouse wheel scroll. You will have to code it yourself. There is a Microsoft patch for this at here (not tried it myself yet)

Sample code to silently reject invalid entries

Private Sub cmbMonth_Change()
    Static idx As Long
    Dim Match As Boolean
    Dim i As Long

    If cmbMonth.Value = "" Then Exit Sub
    If idx = 0 Then idx = 1
    i = idx
    Match = False
    For i = 0 To cmbMonth.ListCount
        If cmbMonth.List((i + idx - 1) Mod cmbMonth.ListCount) Like cmbMonth.Value & "*" Then
           cmbMonth.ListIndex = (i + idx - 1) Mod cmbMonth.ListCount
           Match = True
           Exit For
        End If
    Next
    If Not Match Then
        cmbMonth.Value = Left(cmbMonth.Value, Len(cmbMonth.Value) - 1)
    End If
End Sub
南汐寒笙箫 2024-12-17 12:07:51

将组合框的属性 MatchEntry 设置为 1 (fmMatchEntryComplete),并将 MatchRequired 设置为 true,例如

combobox1.MatchEntry=1
combobox1.MatchRequired=True

[] 的

Set the propertie MatchEntry of combobox to 1 (fmMatchEntryComplete) and MatchRequired to true for example

combobox1.MatchEntry=1
combobox1.MatchRequired=True

[]'s

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文