Private Sub MyCombo_DblClick(Cancel As Integer)
With MyCombo
.Value = .ItemData((.ListIndex + 1) Mod .ListCount)
End With
End Sub
This sets the combo to the next item
(list index plus one), wrapping to zero after the last item
(modulo list count). The event can also be cancelled, but that is rarely useful.
Function ComboNextItem(cbo As ComboBox, Optional OrNull As Boolean)
' Selects the next item in the passed combo box.
' Set 'OrNull' to True (or 1) to select Null after the last item.
If OrNull Then
' let Null happen by overflow of list index
With cbo
.Value = .ItemData(.ListIndex + 1)
End With
Else
' wrap list index to list count
With cbo
.Value = .ItemData((.ListIndex + 1) Mod .ListCount)
End With
End If
End Function
Each combo box in my application then gets the following “On Dbl Click” property:
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (5)
Author
Commented:(^v°)
Open in new window
Commented:
Well, that may be, but ... that is ... standard Windows operation ... double click in a 'text' box to highlight and select, and I have found that users 'expect' that to happen.
So, I recommend that you incorporate Shift (or Control) + DoubleClick to Increment ... AND maybe ... Alt + DoubleClick to Decrement ... which would be handy if the list has more than a few items, say 10 or more ... and you happen to go one past the intended selection. Just Alt+Double Click to backup one.
mx
Author
Commented:Another idea would be to restrict the feature to the label of the combo box. This requires to examine the mouse position, exposed in the low-level mouse events. If you need to trap two events, it's probably best to do so via a class module, much heavier than a simple function call.
Anyway, I just took the habit to double-click, most of the time on the label. If the combo box displays a long text, long enough that a user might want to copy-paste portions of it elsewhere, then the double-click should not be activated, I suppose. Have you ever encountered a situation like this?
(°v°)
Commented:
Well, not specifically. Usually ... it would be a Text box. But I was mainly ... just saying ....
"A WinAPI call (GetKeyState) would do the trick, though."
That's what I used for years. I have that function in my mda library. Pretty simple really.
'Return the state of keyboard key requested....
Declare Function adiSWA_GetKeyState Lib "user32.dll" Alias "GetKeyState" (ByVal nKey%) As Integer
Public Function SWA_GetKeyState(argKeyName
'Actions:Determine via Windows API call if specified key was depressed.
' The argKeyName passed must be one on the Global keycode Constants
'-------------------------
If adiSWA_GetKeyState(argKeyN
SWA_GetKeyState = True
Else
SWA_GetKeyState = False
End If
End Function
Example usage:
If SWA_GetKeyState(vbKeyShift
So, the GetKeyState could also be in the module that contains your ComboNextItem() function ... all self contained.
mx
Author
Commented:Your comments are a good addition to the article. This makes is simple to hook the “select next item” function to Ctrl+double-click (or Alt+click for that matter) and also a “select previous item” function to another combination, as you suggested.
Select previous, change line 8:
Open in new window
and line 13:Open in new window
This is with wrapping, which somehow seems less intuitive in this case, but would still be correct for simple Yes/No combos or similar.(°v°)