Click here to Skip to main content
15,861,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

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 (help I got from code project for this)
C#
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
C#
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))
        {.......


Need guidance
Regards to all
Posted

1 solution

Try a simple loop like this -
foreach (Control c in this.Controls)
{
   if(c is GroupBox)
   {
     // Do stuff here ;
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900