C#忽略空文本框

C#忽略空文本框

问题描述:





我一直在寻找一种方法来检查表单中的所有文本框是否为空,如果没有显示消息。其中一些是不可见的。

我有下面的代码,但它不能以某种方式工作,当我点击确定我得到一个错误,因为文本框是空的。



有人可以帮忙吗?



提前付款



我尝试过:



Hi,

i´ve been searching for a way to check if all textboxes from a form a empty and if not show a message. Some of them are not visible.
I have the code below but it´s not working somehow, when i click ok i get an error because a textbox is empty.

Can anyone help please ?

Thanks in advance

What I have tried:

foreach (Control c in Controls)
{
    if (c is TextBox && c.Visible && string.IsNullOrEmpty(c.Text))
    {
        MessageBox.Show($"TextBox {c.Name} is empty");
    }
}

TextBox {c.Name}为空);
}
}
"TextBox {c.Name} is empty"); } }


希望我理解正确,但为什么不使用控件可见状态来确定是否测试盒子;



Hope I understand correctly but why not use the controls visible state to determine whether to test the box or not;

if (larg.Text == string.Empty)
{
    if (larg.Visible == False)
    {
      MessageBox.Show("info missing", "Warning");
      return;
    }
}





Etcetera。



Etcetera.