Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the list of Child controls inside a groupbox

Tags:

c#

linq

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

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

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.?

like image 321
this-Me Avatar asked May 18 '15 04:05

this-Me


People also ask

How do I add controls to GroupBox?

To create a group of controls If you have existing controls that you want to enclose in a group box, you can select all the controls, cut them to the Clipboard, select the GroupBox control, and then paste them into the group box. You can also drag them into the group box.

What is GroupBox control?

Windows Forms GroupBox controls are used to provide an identifiable grouping for other controls. Typically, you use group boxes to subdivide a form by function. For example, you may have an order form that specifies mailing options such as which overnight carrier to use.

What is GroupBox control in VB net?

The GroupBox displays a frame around a group of controls with or without a caption. Use a GroupBox to logically group a collection of controls on a form. The group box is a container control that can be used to define groups of controls.

What is a GroupBox in C#?

A GroupBox control is a container control that is used to place Windows Forms child controls in a group. The purpose of a GroupBox is to define user interfaces where we can categories related controls in a group. A GroupBox control is a container control that is used to place Windows Forms child controls in a group.


2 Answers

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

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.

like image 114
Grant Winney Avatar answered Sep 28 '22 05:09

Grant Winney


A one liner slution,

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

or

You can try following generic method,

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

then enumerate over the controls as follows,

foreach (TextBox tb in FindVisualChildren<TextBox>(grpBxTargetSensitivity)) //assuming you are looking for TextBox
{
    // do something
}
like image 35
Abhishek Avatar answered Sep 28 '22 07:09

Abhishek