检查NumericUpDown是否为空

Ala*_*ray 6 c# numericupdown

如何检查用户是否将NumericUpDown控件留空,删除其中的值?所以我可以将它重新赋值为0.

chr*_*her 8

if(NumericUpDown1.Text == "")
{
     // If the value in the numeric updown is an empty string, replace with 0.
     NumericUpDown1.Text = "0";
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,Visual Studio 不会显示“Text”属性,但您无论如何都可以输入它,它会编译。 (2认同)

Ale*_*Río 6

使用经过验证的事件并请求text属性可能很有用

private void myNumericUpDown_Validated(object sender, EventArgs e)
{
    if (myNumericUpDown.Text == "")
    {
        myNumericUpDown.Text = "0";
    }
}
Run Code Online (Sandbox Code Playgroud)