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.
VulpesPosted Mar 24, 2011, 11:28 AM
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.
Charles MorissettePosted Mar 24, 2011, 11:20 AM
VulpesPosted Mar 24, 2011, 11:18 AM
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);