Search code examples
c#groupbox

C# iterate through the groupbox themselves


Iv been looking around and i havnt really found anything of meaning.

what i am trying to do, is change between groupboxes at the click of a button.

what i have is a windows form with 32 groupbox containers and there are 12 textboxes in each, some of the textboxes will receive a number the rest will remain empty; now where the following code works and i could copy and paste it 31 more times:

foreach (TextBox x in GROUPBOX1.Controls.OfType<TextBox>())
        {
            int A1 = 0;
            int.TryParse(x.Text, out A1);
            TOTAL += A1;   //defined outside of the method
        }

Where it checks all the textboxes in GROUPBOX1 and adds all the ones with values in together and ignores the empty textboxes.

what i would like to do, at the click of a button, is to increment int GP =1 by +1 and thus move to the next groupbox and its set of textboxes so that i can reuse the code in the foreach loop, to look something like this:

foreach (TextBox x in GROUPBOX+GP+.Controls.OfType<TextBox>())
        {
            int A1 = 0;
            int.TryParse(x.Text, out A1);
            TOTAL += A1;   //defined outside of the method
        }

where +GP+ would represent the int GP =1 and its subsequent increments.

now i got the idea from: this.Controls["GROUPBOX"+box_number].Controls["LABEL"+label_prefix+"_"+label_number+"_LBL"].Text = valueToDisplayInLabel.ToString();

which (with the appropriate assigned int) assigns values to certain labels in different groupboxes. can this be done this way? all the groupboxes and textboxes have already been created.

what i am trying to accomplish is, the text boxes that receive a number will all be added together and that value will then be stored in a database as well as added to all the other "totals" and too stored in a database.

 foreach(GroupBox gb in Controls.OfType<GroupBox>())
    {
        foreach(TextBox tb in gb.Controls.OfType<TextBox>())
        {
            int A1 = 0;
            int.TryParse(tb.Text, out A1);
            TOTAL += A1;   //defined outside of the method
        }
    }

this works great and all, but it doesnt allow me to get the totals of the combined text boxes per group box, like i want to store each groupbox total individually and add them together at a later stage, where as this takes ALL the textboxes that are in group boxes and dumps them in a list, iv added List<int> TOTAL = new List<int>(); and TOTAL.Insert(0, A1); to try and make this work for me, but now its dumping all the textbox values into the list.

essentially im looking for int GB1TOT, GB2TOT etc etc then int TOTAL = GB1TOT + GB2TOT


Solution

  • Is this what you want? Store the total per GroupBox into List GBtotal. Then you can get the total by summing them.

    int Total;
    List<int> GBTotal = new List<int>()
    foreach(GroupBox gb in Controls.OfType<GroupBox>())
    {
        int temp = 0;
        foreach(TextBox tb in gb.Controls.OfType<TextBox>())
        {
            int A1 = 0;
            if(int.TryParse(tb.Text, out A1))
            {
               temp += A1;
            }
        }
        GBTotal.Add(temp);
    }
    Total = GBTotal.Sum();
    

    You technically can use

    Total = GBTotal[0] + GBTotal[1] + ....
    

    Or use loop to get the total