检查文本框是“0”还是空或空

时间:2015-05-13 13:34:43

标签: c#

我正在尝试使用以下代码查看文本框是"0"还是空。

我希望我可以在!上使用IsNulorEmpty运算符,但似乎它没有按照我的意愿行事。有没有一种简单的方法可以在一条线上完成这项工作?

if (!String.IsNullOrEmpty(MyTextbox.Text) || MyTextbox.Text != "0")
{ 
    //string is either null, empty or 0
}
else
{ 
    //string has a value
}

2 个答案:

答案 0 :(得分:12)

代码正在执行您所写的内容,但您对操作员评估感到困惑:

if (string.IsNullOrEmpty(MyTextbox.Text) || MyTextbox.Text == "0")
{ 
     //string is either null, empty or 0
}
else
{ 
     //string has a value
}

答案 1 :(得分:0)

if (!(String.IsNullOrEmpty(MyTextbox.Text) || MyTextbox.Text == "0"))
{ 
   //string has a value
}
else
{ 
   //string is either null, empty or 0
}