【发布时间】:2018-11-27 09:00:06
【问题描述】:
我想检查是否有任何文本框在 btn 单击时为空,然后在其一侧显示相应的 SetError 消息
bool isIncomplete = false;
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox tb = control as TextBox;
if (string.IsNullOrWhiteSpace(tb.Text))
{
isIncomplete = true;
break;
}
}
} // I think this.controls does not work properly..
if (isIncomplete)
{
errorProvider1.SetError(firstname_txtbox, "First Name is required.");
errorProvider2.SetError(lastname_txtbox, "Last Name is required.");
MessageBox.Show("Please fill all the textbox correctly!");
return;
} else if(firstname_txtbox.Text.Length < 2)
{
errorProvider1.SetError(firstname_txtbox, "First Name need to be at least 2 characters"); //this error message does appear through...
}else if() { etc..
errorProvider 消息不会在点击时显示。 我的文本框在面板内...
【问题讨论】:
-
您需要遍历面板的控件而不是表单的控件。一种快捷方式:
this.panel1.Controls.OfType<TextBox>()将只返回文本框 -
谢谢,我认为它的工作原理
标签: c# winforms textbox errorprovider