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; } }
source share