Why did one method work but the other did not and produce weird results?
I was trying to remove all controls of type button from a form. I tried two methods:
This worked:
public void ClearButtons()
{
for (int i = 0; i < this.Controls.Count; i++)
{
if (Controls[i] is Button)
{
Controls.RemoveAt(i);
i--;
}
}
}
Didn't work (I got weird results, some buttons would stay on the form):
foreach(Control control in Controls)
{
if (control is Button)
Controls.Remove(control);
}

Francis SusaimichaelRank #466 · 5.4K reputationPosted Jan 31, 2015, 7:32 AM EST
Swatto23 reputationPosted Jan 31, 2015, 7:33 AM EST