【问题标题】:Checking if all textboxes in a panel a filled检查面板中的所有文本框是否已填充
【发布时间】: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

标签: c# .net winforms


【解决方案1】:

大概是这样的:

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

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    相关资源
    最近更新 更多