Excel ActiveXcomboboxonClick事件

我正在尝试在Excel中使用ActiveXcombobox。 一切工作正常,从下拉buttonclick_event填充点。 但是,当它设置点击事件,我发现它甚至触发,如箭头键。 这是正常的行为,如果是这样,我怎么能绕过这个?

我正在使用Excel 2007 VBA

这是我用来允许在combobox中使用键导航的方法,我将等待看看是否有更好的解决scheme..:lastkey是一个公共variables

 Private Sub ComboBox1_KeyDown(ByVal KeyCode As _ MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 38 Then If ComboBox1.ListIndex <> 0 Then lastkey = KeyCode ComboBox1.ListIndex = ComboBox1.ListIndex - 1 KeyCode = 0 End If ElseIf KeyCode = 40 Then If ComboBox1.ListIndex <> ComboBox1.ListCount - 1 Then lastkey = KeyCode ComboBox1.ListIndex = ComboBox1.ListIndex + 1 KeyCode = 0 End If End If End Sub Private Sub ComboBox1_Click() If lastkey = 38 Or lastkey = 40 Then Exit Sub Else MsgBox "click" End If End Sub 

是的,这是正常的行为。 使用箭头键导航组合中的项目,因此点击事件被触发。

绕过那插入这个代码。 这将捕获所有4个箭头键,按下时不执行任何操作。 这种方法唯一的缺点是你将无法使用箭头键再次导航。

 Private Sub ComboBox1_KeyDown(ByVal KeyCode As _ MSForms.ReturnInteger, ByVal Shift As Integer) Select Case KeyCode Case 37 To 40: KeyCode = 0 End Select End Sub 

跟进

 Dim ArKeysPressed As Boolean Private Sub ComboBox1_Click() If ArKeysPressed = False Then MsgBox "Arrow key was not pressed" '~~> Rest of Code Else ArKeysPressed = False End If End Sub Private Sub ComboBox1_KeyDown(ByVal KeyCode As _ MSForms.ReturnInteger, ByVal Shift As Integer) Select Case KeyCode Case 37 To 40: ArKeysPressed = True End Select End Sub