检查是否在面板中的所有文本框填充
问题描述:
我目前在Windows工作窗体应用程序,我有2个小组,在他们的文本框,我需要单独检查面板的文本框,如果他们是不是空的,所以它不是一个选项循环遍历表单中的所有控件。检查是否在面板中的所有文本框填充
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");
}
}
}
答
也许是这样的:
foreach(Panel pnl in Controls.OfType<Panel>())
{
foreach(TextBox tb in pnl.Controls.OfType<TextBox>())
{
if(string.IsNullOrEmpty(tb.Text.Trim()))
{
MessageBox.Show("Text box can't be empty");
}
}
}
+1
谢谢你,完美的工作 –
欢迎StackOverflow的,我想你应该澄清你的问题有点用的,到目前为止,你已经尝试过什么例子,你期待什么样的输出。请参阅[如何提出问题。](http://stackoverflow.com/help/how-to-ask) – Marusyk
为什么不通过'panel.Controls'循环? – Dmitry
您仍然可以循环控制...只需检查每次迭代中的“控制”是否为“文本框”。 –