【发布时间】:2016-11-08 21:38:37
【问题描述】:
我目前在一个 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");
}
}
}
【问题讨论】:
-
欢迎来到 StackOverflow,我认为您应该通过迄今为止尝试过的示例以及您期望的输出来澄清您的问题。见how to ask a question.
-
为什么不循环访问
panel.Controls? -
您仍然可以遍历控件...只需检查每次迭代中的
Control是否为TextBox。