获取组框内的子控件列表 [英] Get the list of Child controls inside a groupbox

查看:38
本文介绍了获取组框内的子控件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a groupbox in my application which contains child controls.(As seen in the attchached pic). I want to enumerate through all the textboxes for performing some validation using a simple foreach loop.

此文档大纲将为控件的外壳提供一个清晰的概念

This document outline would give a fair idea of the housing of the controls

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)来获取所有控件,包括容纳在面板内的文本框?

My question is, Is there a better way (possibly through LINQ) to get all the controls including the textboxes housed inside the panels than the above method.?

推荐答案

我不知道这会更好..是否更容易阅读是一个观点问题:

I don't know that this is any better.. whether it's easier to read is a matter of opinion:

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的所有FlowLayoutPanels的所有面板内获取所有TextBox,如果其中的 all 中有值,则返回 true .

This'll grab all TextBoxes inside of all Panels in all FlowLayoutPanels in your GroupBox, and return true if all of those TextBoxes have a value in them.

这篇关于获取组框内的子控件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆