vba Excel ActiveX ComboBox onClick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11792352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel ActiveX ComboBox onClick event
提问by Deepak
I am trying to use the ActiveX ComboBox in excel. Everything works fine to the point of being populated from the drop down button click_event
. But when it set the click event I find it is triggered even from keystrokes like the Arrow keys. Is this normal behavior, and if so how can I bypass this?
我正在尝试在 Excel 中使用 ActiveX ComboBox。一切正常,可以从下拉按钮填充click_event
。但是当它设置点击事件时,我发现它甚至是从像箭头键这样的击键中触发的。这是正常行为吗,如果是,我该如何绕过?
I am working on Excel 2007 VBA
我正在使用 Excel 2007 VBA
This is the method i used to allow navigating in the combo box using keys , i will wait to see if there is a better solution.. : lastkey is a public variable
这是我用来允许使用键在组合框中导航的方法,我将等待看看是否有更好的解决方案..:lastkey 是一个公共变量
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
采纳答案by Siddharth Rout
Yes this is normal behavior. Using the arrow keys navigates the items in the combo and hence the click event is fired.
是的,这是正常行为。使用箭头键导航组合中的项目,因此触发 click 事件。
To bypass that insert this code. This captures all the 4 arrow keys and does nothing when it is pressed. The only drawback of this method is that you will not be able to navigate using the Arrow keys anymore.
要绕过插入此代码。这会捕获所有 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
FOLLOWUP
跟进
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