C# WindowsForms NumericUpDown “cannot equal x value” property?
-
29-09-2019 - |
Question
Due to my application being a quadratic root solver, and receiving input from a NumericUpDown in the form of 0
will throw a divide by zero
error, I was wondering if it was possible to be able to specify that particular NumericUpDown control, not able to be set to 0
at all. Or, is it just easier to catch that with a conditional and resolve it?
Solution
You can create a check in the Validating-event
private void numericUpDown1_Validating(object sender, CancelEventArgs e)
{
if ((sender as NumericUpDown).Value == 0)
{
e.Cancel = true;
}
}
But you also need the check in your code, always validate input...
OTHER TIPS
NumericUpDown has two properties calld Minimum and Maximum which set the min and max value for your up/down control. You can just set its Minimum to 1 so the user won't be able to select 0.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow