[RESOLVED] [VB.NET-VS2010] Limit the number of digits in a NumericUpDown
I have a numericupdown with a minimum set at 0 and a maximum at 100. However, at run-time a user may input an infinite number by skipping the arrows and inputting numbers with keys. I have tried to solve this myself but my error message won't popup until the user presses the enter key.
Code:
Private Sub spnMiscQty_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles spnMiscQty.ValueChanged
If spnMiscQty.Value = 100 Then
MessageBox.Show("Pick a Quantity between 1 and 100")
Else
End If
End Sub
What I would really like to do to by-pass this issue is to limit the number (cannot enter a value greater than '100') or to disallow a cursor to be placed in the NumericUpDown (user would only be allowed to use the arrows to select a number).
Thanks in advance for any help you may provide.
Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown
Why? Let the user choose, then change the query to:
If spnMiscQty.Value > 100 or spnMiscQty.Value < 0 Then ...
Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown
I posted the wrong code. That code was to check if the message box was working before I found out about the Enter key initiating the msgbox. I did have spnMiscQty.Value > 100 before that.
I do not want the user to be able to choose a quantity above 100 because this program needs to be airtight. No room for error with my bosses...even user error.
Thanks for your quick reply David.
Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown
Instead of using valuechanged event, use the Keypress event and trap them there....
Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown
Have a look at this thread, it explains why this happens :
http://www.vbdotnetforums.com/window...html#post20621
It is the default behaviour. If you want to change that, you will need to make a custom NumericUpDown ( your own ). Not sure if it will be a worthwhile effort
Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown
That's very interesting. I guess I never really took the time to play around with NumericUpDowns before this week. I guess I will leave it as is.
Thanks for the article Hannes!