从Combobox下拉 - 右键单击

时间:2018-04-30 16:28:23

标签: vba

我有一个ComboBox,我想显示一个msgbox,当用户从下拉列表中使用特定项目的RightClick时 - 不选择此项目。有可能吗?

这不符合我的意愿。此msgbox仅显示选择的项目,但我会从下拉列表级别为每个项目显示此msgbox,而不选择此项目而不折叠整个下拉列表。

我的代码:

Private Sub ComboBox1_Mousedown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = xlSecondaryButton Then
        Select Case ComboBox1.Value
            Case Is = "1"
                MsgBox "Description of item1"
                ComboBox1.Value = ""
            Case Is = "2"
                MsgBox "Description of item2"
                ComboBox1.Value = ""
            Case Is = "3"
                MsgBox "Description of item2"
                ComboBox1.Value = ""
            Case Else
        End Select
    End If
    End Sub

1 个答案:

答案 0 :(得分:1)

好的,我想我已经看到了你想要实现的目标。

不幸的是,不,组合框并不像那样工作。你可以想象使用一些非常复杂的Win32消息处理来破解它,但是在一天结束时,ActiveX控件只能像他们设计的那样可配置,并且"下拉"组合框的一部分并不会为你想要做的事情发动事件。

您的用户也不会期望组合框能够以这种方式运行 - 因此实施这将导致UX /功能可发现性问题。

这感觉非常像XY问题:真正的问题是您希望每个下拉项目都有相关的描述,以便用户知道他们将要选择的内容,在他们选择它之前。

事实证明,组合框本身可以做到这一点。

我不知道你如何填充你的组合框,但如果你可以将数据(包括描述)放在2D数组中(如果数据来自工作表,你可以免费获得一个Range),您可以将组合框配置为在2列上显示其项目,如果需要,您甚至可以隐藏无意义的用户数字代码:

Dim items
ReDim items(1 To 3, 1 To 2)
items(1, 1) = 1
items(1, 2) = "Description for Item 1"
items(2, 1) = 2
items(2, 2) = "Description for Item 2"
items(3, 1) = 3
items(3, 2) = "Description for Item 3"

ComboBox1.List = items
ComboBox1.ColumnCount = 2
'ComboBox1.ColumnWidths = "30,70"
ComboBox1.ColumnWidths = "0,70"

使用ColumnWidths = "0,70"即可获得此信息:

userform combobox showing only descriptions

使用ColumnWidths = "30,70",你得到了这个:

userform combobox showing codes and descriptions

组合框的.Value将是第一列包含的内容,即使它被隐藏了:

label showing selected value in the combobox