Incrementing form values

  • Thread starter Thread starter caveman.savant
  • Start date Start date
C

caveman.savant

Is there a way to create a userform with textboxes to increment to
value by clicking on it?
Could you also deincrement by shift-clicking?
 
You might consider using a spin button control. It's make just for the
purpose of incrementing so users will know instantly how to do it.
 
I bet you could, but why wouldn't you use scrollbar or spinbutton (and a
label???) instead. As a user, that would be more natural to me.

But maybe...

Option Explicit
Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
With Me.TextBox1
If IsNumeric(.Value) Then
If Shift = 0 Then
.Value = .Value + 1
Else
.Value = .Value - 1
End If
End If
End With
End Sub
 
If you set the initial Text value of the TextBox to 0 (or 1 or whatever
would be the appropriate starting value) and set the Locked property to True
(which I would think would required for the stated use for this TextBox),
then you can eliminate the IsNumeric test completely which would allow your
code to be reduced to this...

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
With Me.TextBox1
If Shift = 0 Then
.Value = .Value + 1
Else
.Value = .Value - 1
End If
End With
End Sub

And, while admittedly cryptic, this one-liner MouseDown event performs
identically to the above code...

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Me.TextBox1.Value = Me.TextBox1.Value - Sgn(Shift - 0.5)
End Sub
 
I still wouldn't use a textbox <vbg>.

Rick said:
If you set the initial Text value of the TextBox to 0 (or 1 or whatever
would be the appropriate starting value) and set the Locked property to True
(which I would think would required for the stated use for this TextBox),
then you can eliminate the IsNumeric test completely which would allow your
code to be reduced to this...

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
With Me.TextBox1
If Shift = 0 Then
.Value = .Value + 1
Else
.Value = .Value - 1
End If
End With
End Sub

And, while admittedly cryptic, this one-liner MouseDown event performs
identically to the above code...

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Me.TextBox1.Value = Me.TextBox1.Value - Sgn(Shift - 0.5)
End Sub
 
LOL... I had meant to agree with you on that point in my reply, but
apparently forgot to include a statement to that effect in it. Yes, I would
definitely use a SpinButton if I were implementing this for myself; but, as
you know, sometimes posters get an idea on how they want to do something and
will not be dissuaded no matter what. I presumed that is why you posted your
code in the first place; and it is pretty much the reason why I offered my
comments to it.
 
Hi,

I support Dave and Rick, us the Spinner control. Users know what a spinner
is but you will need to teach them how to use your non-standard text box.
It's always good to consider following the Microsoft paradym when creating a
Microsoft Excel application.
 
Thanks for everyone's help. The input, I agree is non-standard.
Imagine if this was implemented on a touch-screen. Spinner controls
and "fat fingers" do not marry well.
Most of the input is 1 (sometimes 2), which require a single tap on
the screen.
 
Agreed... and I would probably lay them out with one CommandButton on the
left of the TextBox (with a minus sign in a large, bold font size as its
caption) and the other CommandButton on the right of the TextBox (with a
plus sing in the same large, bold font size as the other CommandButton as
its caption).
 
Back
Top