VB.NET NumericUpDown: Is there a way to keep the user from entering a number that is out of range?

I have to allow a user to select a whole number between 10 and 30.  I was planning on using a numericUpDown control.  As soon as the user clicks on my "OK" button, the value is stored in the My.Settings and the form is disposed.

As I tested my code, I tried entering a value that was out of range. I learned that if I enter a number such as 50, (too high), the value of the NumericUpDown simple goes to the maximum value (in this case 30).   If I enter a value of 5 (too low), the NumericUpDown simply goes to the minimum value (in this case 10).  Now that maximum number or minimum number is stored in the My.Settings.

Is there a way to disable the user from entering a value from the keyboard and limit him to only use the up and down arrows on the control.
Or, is there some other way for me to capture that he put in a value that is out of range and allow him to correct the mistake?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
a long time ago, I showed how to create your own numeric custom control and it includes a min/max values: http://emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi charlieb01;

You can use the Validating event to catch the errors in value and display a message to the user. The system will not allow the focus to leave the control until a valid value is entered
Private Sub NumericUpDown1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
    Dim inputVal As Decimal = Decimal.Parse(NumericUpDown1.[Text])
    If inputVal < NumericUpDown1.Minimum Or inputVal > NumericUpDown1.Maximum Then
        MessageBox.Show("The value you entered is outside the valid range. Please correct.")
        e.Cancel = True
    End If
End Sub

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.