sujittalukde
Well-known Member
- Joined
- Jun 2, 2007
- Messages
- 520
I am using the following codes to scroll list items in a list box through arrow keys.
When I am using the Down arrow key, it is working well ie when the item "Delhi" is reached and then again Down arrow key is pressed, "Ahemdabad" is coming in the combobox.
But when I am using the Up arrow key, problem is coming. When the combobox is displaying the item "Ahemdabad" and Up key is pressed, "Calcutta" is coming instead of "Delhi".
How can I fix it?
Code:
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "Ahemdabad"
.AddItem "Bombay"
.AddItem "Calcutta"
.AddItem "Delhi"
.Style = fmStyleDropDownList
.ListIndex = 0
End With
End Sub
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case vbKeyDown
If ComboBox1.ListIndex <> ComboBox1.ListCount - 1 Then
'
Else
ComboBox1.ListIndex = -1
End If
Case vbKeyUp
If ComboBox1.ListIndex <> 0 Then
'
Else
ComboBox1.ListIndex = ComboBox1.ListCount - 1
End If
End Select
End Sub
When I am using the Down arrow key, it is working well ie when the item "Delhi" is reached and then again Down arrow key is pressed, "Ahemdabad" is coming in the combobox.
But when I am using the Up arrow key, problem is coming. When the combobox is displaying the item "Ahemdabad" and Up key is pressed, "Calcutta" is coming instead of "Delhi".
How can I fix it?