Charles Morissette

Charles Morissette

  • NA
  • 29
  • 42.8k

Foreach skips

Mar 24 2011 11:10 AM
I'm doing a foreach loop, it's looking through the controls in splitContainer1.Panel2 in one of my forms.

The purpose is to bring back all the controls from panel2, and then check if they are a groupbox. If they are, it then deletes it. My problem is it seems to only delete some of them, and leave others there.

I've added a list to check if it detects all the groupboxes, if I make the code to delete a note rather then code it will detect every groupbox. The moment I try to delete them again, it detects a different amount.

These groupboxes are dynamically created in run time, and need to be removed during the program's use.

                List<string> BoxxCheck = new List<string>();
                List<string> ActualBoxes = new List<string>();
                Parts = 0;
                foreach (Control Boxx in this.splitContainer1.Panel2.Controls)
                {
                    if (Boxx is GroupBox)
                    {
                        BoxxCheck.Add(Boxx.Name.ToString());
                        //splitContainer1.Panel2.Controls.Remove(Boxx);
                    }
                }
                lblTest.Text = string.Join(",", BoxxCheck);

Is my current code.

Answers (3)