How do you allow arrow keys in a combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Adams
    New Member
    • Jul 2010
    • 55

    How do you allow arrow keys in a combo box

    I have multiple combo boxes which I have a tab order on. The problem that I am having right now is is that when you tab to a combo box and try to go down through the data and use the down arrow on the keyboard the cusor goes to the next tab count item.

    What do I do to allow the use of arrow keys in the combo box when it has the focus?

    Thanks in Advance.
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Replacing Combo with the actual name of your combobox:
    Code:
    Private Sub Combo_KeyDown(KeyCode As Integer, Shift As Integer)
      
      Select Case KeyCode
        Case vbKeyDown
          If Combo.ListIndex <> Combo.ListCount   1 Then
            Combo.ListIndex = Combo.ListIndex + 1
          Else
            Combo.ListIndex = 0
          End If
       Case vbKeyUp
          If Combo.ListIndex <> 0 Then
            Combo.ListIndex = Combo.ListIndex   1
          Else
            Combo.ListIndex = Combo.ListCount   1
          End If
    End Select
    
    End Sub
    In order to keep focus from leaving the combobox when you get to the beginning or the end of the selections, I have it "wrapping." If you get to the last selection and hit the Down Arrow again, it wraps back to the first selection ; if you're on the first selection and hit the Up Arrow it wraps around to the last selection.

    Remember, if for some reason you use this code elsewhere, without the combobox having the focus, you need to set focus on it before executing the code.

    Linq ;0)>

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by Michael Adams
      I have multiple combo boxes which I have a tab order on. The problem that I am having right now is is that when you tab to a combo box and try to go down through the data and use the down arrow on the keyboard the cusor goes to the next tab count item.

      What do I do to allow the use of arrow keys in the combo box when it has the focus?

      Thanks in Advance.
      On the GotFocus() Event of your Combo Boxes, Drop Down the Combo Box. Now, the Arrow Keys should behave as you expect. For a Combo Box named Combo1:
      Code:
      Private Sub Combo1_GotFocus()
        Me![Combo1].Dropdown
      End Sub

      Comment

      Working...