NumericUpDown allows the user to enter a number greater than the maximum

I have a NumericUpDown variable in my class. Minimum and maximum values ​​are set as follows:

myNumericUpDown.Maximum = 9999; myNumericUpDown.Minimum = 0; 

This will prevent the rotation range from exceeding 9999 or below 0.

The problem I am facing is when the user types inside the text box, they can enter any arbitrary number greater than the Maximum or less than the minimum.

Is there a property in the NumericUpDown class that controls the minimum and maximum values ​​for a text field? Or do I need to write a callback procedure that checks this condition?

+4
source share
1 answer

If you set a property in the property pane in the form design view, it will handle this for you. If the user had to enter 12000, and you would have a maximum of 9999, as soon as the control loses focus, the control will drop to a maximum value of 9999. It also goes with a negative value. It will automatically switch to 0 as soon as the control loses focus.

If you do not want the user to enter more than 4 digits, you can just watch the KeyDown event.

  /// <summary> /// Checks for only up to 4 digits and no negatives /// in a Numeric Up/Down box /// </summary> private void numericUpDown1_KeyDown(object sender, KeyEventArgs e) { if (!(e.KeyData == Keys.Back || e.KeyData == Keys.Delete)) if (numericUpDown1.Text.Length >= 4 || e.KeyValue == 109) { e.SuppressKeyPress = true; e.Handled = true; } } 
+2
source

All Articles