【问题标题】:How to check if all Textbox is empty in on click and input thier errorProvider msg?如何在单击并输入他们的errorProvider msg时检查所有文本框是否为空?
【发布时间】: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 消息不会在点击时显示。 我的文本框在面板内...

【问题讨论】:

标签: c# winforms textbox errorprovider


【解决方案1】:

使用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");
        }
    }

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多