Search code examples
c#winformspropertiesnumericupdown

C# WindowsForms NumericUpDown "cannot equal x value" property?


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...