本文介绍了如何检查文本框是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个站点上,我找到了TryParse
方法(如何检查C#中是否有空文本框),但是我不知道如何使用它.
On a site I found the TryParse
method (how to check if there is a empty textbox in C#) but I don't know how to use it.
int outputValue=0;
bool isNumber=false;
isNumber=int.TryParse(textBox1.Text, out outputValue);
if(!isNumber)
{
MessageBox.Show("Type numbers in the textboxes");
}
else
{
// some code
}
以及如何解决1个以上的文本框
and how can i solve this for 1+ number of textboxes
推荐答案
如果您要为页面中的所有文本框控件选中空,请尝试 IsNullOrWhiteSpace
If you want to check empty for all text box control in your page .Try IsNullOrWhiteSpace
foreach (Control child in this.Controls)
{
TextBox textBox = child as TextBox;
if (textBox != null)
{
if (!string.IsNullOrWhiteSpace(textBox.Text))
{
MessageBox.Show("Text box can't be empty");
}
}
}
这篇关于如何检查文本框是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!