1

我想检查 btn 单击时是否有任何文本框为空,然后在其一侧显示相应的 SetError msg

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 消息。我的文本框在面板内...

4

1 回答 1

0

使用String.IsNullOrEmpty(String) 检查控件是否为空。

private void button1_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(textBox1.Text))
        {
            MessageBox.Show("Please fill all the textbox correctly!");
        }
        else
        {
            MessageBox.Show("Not empty");
        }
    }
于 2018-11-27T13:02:43.487 回答