c# - 获取组框内的子控件列表

标签 c# linq

我的应用程序中有一个包含子控件的组框。(如附加图片所示)。我想枚举所有文本框以使用简单的 foreach 循环执行一些验证。

这份文件大纲将给出控件外壳的合理概念

enter image description here

foreach (Control control in grpBxTargetSensitivity.Controls)
            {
                if (control is FlowLayoutPanel && control.HasChildren)
                {
                    foreach (Control ctrl in control.Controls)
                    {
                        if (ctrl is Panel && ctrl.HasChildren)
                        {
                            foreach (Control tbox in ctrl.Controls)
                            {
                                if (tbox is TextBox)
                                {
                                    TextBox textbox = tbox as TextBox;
                                    validData &= !string.IsNullOrWhiteSpace(textbox.Text);
                                }
                            }
                        }
                    }
                }
            }

我的问题是,是否有比上述方法更好的方法(可能通过 LINQ)来获取面板内的所有控件,包括文本框。?

最佳答案

我不知道这样更好..是否更容易阅读是一个见仁见智的问题:

var validData
    = grpBxTargetSensitivity.Controls.OfType<FlowLayoutPanel>()
                            .SelectMany(c => c.Controls.OfType<Panel>())
                            .SelectMany(c => c.Controls.OfType<TextBox>())
                            .All(textbox => !string.IsNullOrWhiteSpace(textbox.Text));

这将抓取您的 GroupBox 中所有 FlowLayoutPanel 中所有面板内的所有 TextBox,如果 所有 这些 TextBox 中都有值,则返回 true。 p>

关于c# - 获取组框内的子控件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30295242/

相关文章:

c# - .net 邮件类与 SQL sp_send_dbmail

C# WPF - 应用程序图标 + ShowInTaskbar = False

c# - 在类型上强制执行静态方法

c# - 秒倒计时器

c# - 使用 Azure Blob 存储上传文件后 URL 过期吗?

c# - 在 LINQ-to-NHibernate 或 LINQ 中比较枚举时有什么考虑吗?

c# - 在所有类 List<Object> 中搜索匹配值

c# - 多个数组计数比较 C#

c# - LINQ 成员表达式获取列名

c# - LINQ 和可为 null 的参数