【问题标题】:Get the list of Child controls inside a groupbox获取组框内的子控件列表
【发布时间】:2015-07-29 11:42:22
【问题描述】:

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

本文档大纲将给出控件外壳的合理概念

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)来获取所有控件,包括位于面板内的文本框。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    不知道这样好不好..是否更容易阅读见仁见智:

    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 中所有 Panels 内的所有 TextBox,如果 所有 这些 TextBox 中有值,则返回 true

    【讨论】:

    • 完美!万分感谢。在 LINQ 中有没有办法可以在上面的查询中添加一个条件来过滤掉一些文本框?
    【解决方案2】:

    单线解决方案,

    IEnumerable<TextBox> collection = grpBxTargetSensitivity.Children.OfType<TextBox>(); //assuming you are looking for TextBox
    

    您可以尝试以下通用方法,

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }
    
                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }
    

    然后枚举控件如下,

    foreach (TextBox tb in FindVisualChildren<TextBox>(grpBxTargetSensitivity)) //assuming you are looking for TextBox
    {
        // do something
    }
    

    【讨论】:

      【解决方案3】:

      我通过为您找到任何控件(或 T 类型)和任何从 Control(或 T 类型)继承的对象的能力来创建一种方法:

      public static List<T> GetSubControlsOf<T>(Control parent) where T : Control
      {
             var myCtrls = new List<T>();
      
             foreach (Control ctrl in parent.Controls)
             {
                 // if ctrl is type of T
                 if (ctrl.GetType() == typeof(T) || 
                     ctrl.GetType().IsInstanceOfType(typeof(T)))  
                 {
                      myCtrls.Add(ctrl as T);
                 }
                 else if (ctrl.HasChildren)
                 {
                      var childs = GetSubControlsOf<T>(ctrl);    
                      if (childs.Any()) 
                         myCtrls.AddRange(childs);
                 }
             }
      
             return myCtrls;
       }
      

      并使用这种形式,例如:

      foreach (var textbox in GetSubControlsOf<TextBox>(this))
      {
             validData &= !string.IsNullOrWhiteSpace(textbox.Text);
      }
      

      【讨论】:

        【解决方案4】:

        我在 StackOverflow 上找到了以下链接:

        How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

        Loop through all the user controls on a page

        试一试。应该适用于您的情况。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-05
          • 2019-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多