0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Yes, but I also need to account for the fact that I can calculate negative values, just not zero.
mm in that case i think the only solution will be to handle the 0 when the NumericUpDown value change or in your function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.