gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:58:43 26 4
gpt4 key购买 nike

我的应用程序中有一个包含子控件的组框。(如附加图片所示)。我想枚举所有文本框以使用简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com