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
List
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.

VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 7:28 AM EDT
If you're removing multiple elements from a collection, it's therefore best to always iterate backwards through the collection using an ordinary for loop so that it doesn't mess up the indexing of the elements still to be examined.
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 7:18 AM EDT
List
List
Parts = 0;
for (int i = this.splitContainer1.Panel2.Controls.Count - 1; i >= 0; i--)
{
Control Boxx = this.splitContainer1.Panel2.Controls[i];
if (Boxx is GroupBox)
{
BoxxCheck.Add(Boxx.Name.ToString());
splitContainer1.Panel2.Controls.RemoveAt(i);
}
}
lblTest.Text = string.Join(",", BoxxCheck);
Charles Morissette29 reputationPosted Mar 24, 2011, 7:20 AM EDT