Log in

View Full Version : Userform: Make up/down arrow keypress do spinup/down



mark99k
11-17-2012, 08:54 AM
Hi. I'm trying to duplicate, in a userform, the common behavior of a multiple-approach box -- for example, the Left Indent box in the Paragraph dialog, where you can change the value by either (1) editing it directly, (2) clicking the adjacent spin buttons, or (3) clicking within the box and pressing the up or down arrow keys.

The spin buttons work fine, but my attempted code for the arrow keypresses (below, simplified) does nothing. Is that box truly a combo box, or is it some other type of control, married to internal spin buttons? Thanks for any clues.

Sub LI_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 38 Then
Call LISpin_SpinUp
ElseIf KeyAscii = 40 Then
Call LISpin_SpinDown
End If
End Sub

Sub LISpin_SpinUp
LI.Text = Str(Val(LI.Text) + 1)
End Sub

Sub LISpin_SpinDown
LI.Text = Str(Val(LI.Text) - 1)
End Sub

fumei
11-17-2012, 09:15 PM
This is for a textbox? What is its name?

fumei
11-17-2012, 09:26 PM
OK, it is LI, but what makes you think 38 and 40 are correct for up/down arrows? They are & and ( respectively.

Try various keys. Notice you get the ascii for various keys, BUT you get nothing with the arrow keys.
Sub LI_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
MsgBox KeyAscii
End Sub

fumei
11-17-2012, 09:34 PM
Oh, and why are you using Val...when you just make the final value into a string with Str?

mark99k
11-17-2012, 10:20 PM
Hi fumei. Sorry, my bad -- I should've been using KeyDown, not KeyPress. The 38 and 40 are what works to intercept arrow keys in the KeyDown event. (Kind of strange that they're different in KeyPress). For anyone else looking, this works:

Private Sub LI_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 38 'up arrow
Call LIS_SpinUp
Case 40 'down arrow
Call LIS_SpinDown
End Select
End Sub

About Val(): Apologies -- that's an artifact of the original source of the box content, which is a calculation.

fumei
11-17-2012, 10:40 PM
So everything is good?