C# Windows Forms foreach controls, with Textbox and Button in Form or Panel and select all Checkboxe

Lionsure 2020-02-29 Original by the website

A form is like a large container that can hold a variety of controls, including the Panel control in Windows Forms development. If the Form is the largest container in Winforms, then the Panel control can be considered the second largest. It is specifically used for layout of software interfaces, which is equivalent to the div in Web development. Almost all controls can be included in the Panel, including Form.

Including the control in the Panel, on the one hand, it is beautiful for the interface layout(filling with different background colors or pictures), and on the other hand, it is convenient to perform related operations on the sub-controls. You can foreach all the child controls contained in the control to achieve their operations. Let's see how to foreach the child controls in the form and in the Panel.

 

I, C# Windows forms foreach controls in Form

Mainly foreach the controls that belong to the Form. If there are Panel, Button and TextBox controls in the Form, the foreach code is as follows:

/// <summary>
       /// Foreach controls in Form
       /// </summary>
       /// <param name="contName">Name of control</param>

       public void ForeachControlsInForm(string contName)
       {
              foreach (Control contr in this.Controls)
              {
                     if (contr is Panel)
                     {
                            // Related operation code
                            contr.BackColor = Color.Aquamarine;
                     }
                     else if (contr is Button)
                     {
                            contr.ForeColor = Color.RoyalBlue;
                     }
                     else if (contr is TextBox)
                     {
                            contr.Text = null;
                     }

              // Find a control by its name
                     if(contr.Name.Equals(contName))
                     {
                            contr.Name = string.Empty;
                     }
              }
       }

The above code just performs some random operations according to the type of the control and the name of the control passed. You can change it to the desired operation according to the specific needs.

 

 

II, C# Windows forms foreach controls in Panel

The foreach method is the same as foreach the controls in the form, except that this should be changed to the name of Panel. If there are only Button and TextBox controls in the Panel control, the code is as follows:

/// <summary>
       /// Foreach controls in Panel
       /// </summary>
       /// <param name="contName">Name of control</param>

       public void ForeachPanelControls(string contName)
       {
              foreach (Control contr in panel1.Controls)
              {
                     if (contr is Button)
                     {
                            if (contr.Name.Equals(contName))
                                   contr.ForeColor = Color.RoyalBlue;
                            else
                                     ctrl.ForeColor = Color.SkyBlue;
                     }
                     else if (contr is TextBox)
                     {
                            if (contr.Name.Equals(contName))
                                   contr.Name = "The current value";
                            else
                                   contr.Text = null;
                     }
              }
       }

 

III, C# Windows forms foreach CheckBox Controls(Select all)

If you are asked to click "Select All", check all the CheckBoxes; click "Select All" again to uncheck all CheckBoxes;

Method 1:

private void ForeachCheckBox(Control ctrls, bool currVal)
       {
              CheckBox cbox;
              foreach (Control contr in ctrls.Controls)
              {
                     if (contr is CheckBox)
                     {
                            cbox = (CheckBox)contr;
                            cbox.Checked = currVal;
                     }
              }
       }

Call(Double-click the "Select All CheckBox control" to automatically switch to the code page and automatically add the CbAll_CheckedChanged event, called in this event):

private void CbAll_CheckedChanged(object sender, EventArgs e)
       {
              ForeachCheckBox(cbAll.Parent, cbAll.Checked);
       }

cbAll is the name of "Select All CheckBox control". The effect is shown in Figure 1:

C# Windows forms foreach CheckBox Controls(Select all)

Figure 1

 

Method 2:

private void ForeachCheckBoxes(Control ctrls, bool currVal)
       {
              CheckBox cbox;
              foreach (Control contr in ctrls.Controls.OfType<CheckBox>())
              {
                     cbox = (CheckBox)contr;
                     cbox.Checked = currVal;
              }
       }

The calling method and effect are the same as "Method 1".