Is it possible to iterate using foreach loop on multiple groupBox.

I have 6 GroupBox named as gBox1,gBox2,gBox3,gBox4,gBox5,gBox6 AND each gBox contain 21 ComboBox named as slot1,slot2,slot3,slot4,slot5.... so on out of which I want to select only 7 comboBoxes for operation.
For eaxmple if user enter 2 in textbox then from gBox1->cb15,slot1,slot2,slot3,slot4,slot5,slot6,slot7 and from gBox2->slot8,slot9,slot10,slot11,slot12,slot13 text of all comboBox should be stored in a string array.

Now problem is I am able to Iterate on ComboBox like this

    if (no_of_period == 2)
    {
      if (duration == 1)
      {
        foreach (var comboBox in gBox2.Controls.OfType<ComboBox>().OrderBy(m => m.Name))
        {
          if (comboBox.Name.Substring(0, 4).Equals("slot"))
          {
             comboBox.Text = i.ToString("HH:mm");
          }
        }
        i = i.AddMinutes(60);
        foreach (var comboBox in gBox3.Controls.OfType<ComboBox>().OrderBy(m => m.Name))
        {
          if (comboBox.Name.Substring(0, 4).Equals("slot"))
          {
              comboBox.Text = i.ToString("HH:mm");
          }
        }
     }

Now I have to write this code for all 6 gBox is there any way to automatically gBox should change... Something like this- NOT CORRECT CODE

     foreach (var sgrpBox in batch_creation.ActiveForm.Controls.OfType<GroupBox>().OrderBy(g => g.Name))
     {
         string gname = null;
         if (sgrpBox.Name.Substring(0, 4).Equals("gBox"))
         {
              gname = sgrpBox.Name.Substring(0, 4) + G;
             foreach (var comboBox in ganme.Controls.OfType<ComboBox>().OrderBy(m => m.Name))
             {.......

Could you explain a bit more please?
What are your intentions with this eleborate scheme of yours?

Sure
I have 6 Group Box and each group box have 21 comboboxs that means 6 GB x 21 CB = Total 126 CB.
Each groupbox represent period say Period1, Period2, Period3...
Period and Shift option are coming from other ShiftcomboBox->Morning and Evening and PeriodcomboBox->1,2,3,4,5,6.
Now if user select Eve shift and 2 periods then 7 CBs which is representing Time Slot for Seven Day of GB1 and GB2 should automatically change its time to 16:00.
Now as shown in my code I have to write code for each group box i.e. for gBox2 and gBox3 and if user select 6 period then I have to write same code for all SIX group Box.
What I want is to automatically the groupbox name sholud change on each iteration as I am doing for combobox inside group. So don't have to write same code for all groupbox separately.
I hope I am clear now

Still don't really get it what you are trying to realize, but I guess this can perhaps help.

Your code is close to correct, you were trying to access the groupbox just by it's name instead of as an object:

     foreach (GroupBox sgrpBox in batch_creation.ActiveForm.Controls.OfType<GroupBox>())
    {
        if (sgrpBox.Name.Substring(0, 4).Equals("gBox"))
        {
            foreach (ComboBox comboBox in sgrBox.Controls.OfType<ComboBox>())
            {.......

When you cast the objects in the foreach loop as the specific type you're accessing, then you get Intellisense support for all the properties and methods for that object.

Also unless you specifically need the objects sorted in the foreach loop, it just uses more resources and is kind of redundant, since you're iterating through all of them anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.