Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if NumericUpDown is empty

How do I check whether the user left a NumericUpDown control empty, removing the value on it? So I can reassign it a value of 0.

like image 622
Alan Mcgilvray Avatar asked Feb 25 '13 18:02

Alan Mcgilvray


3 Answers

if(NumericUpDown1.Text == "")
{
     // If the value in the numeric updown is an empty string, replace with 0.
     NumericUpDown1.Text = "0";
}
like image 164
christopher Avatar answered Nov 02 '22 00:11

christopher


It might be useful to use the validated event and ask for the text property

private void myNumericUpDown_Validated(object sender, EventArgs e)
{
    if (myNumericUpDown.Text == "")
    {
        myNumericUpDown.Text = "0";
    }
}
like image 7
Alejandro del Río Avatar answered Nov 02 '22 01:11

Alejandro del Río


Even if the user deleted the content of the numericUpDown control, its value still remains.
upDown.Text will be "", but upDown.Value will be the previous valid value entered.
So my way to 'prevent' the user to leave the control empty, on the onLeave event, I set:

upDown.Text = upDown.Value.ToString();
like image 2
userMA Avatar answered Nov 02 '22 02:11

userMA