Controls.Remove index change
Came across something now which was quite funny... I dynamically add controls to my form, and I also need the option to remove them again. So at first I did this, which seemed logic as it is exactly how I add them:
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
{
this.Controls.Remove(c);
}
}
But this only removed every second textbox! At first I was dumbstruck, but then found out that when you remove a Control from the ControlCollection, subsequent controls are moved up to fill that space. So had to change it to this to work for all textboxes.
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
{
forRemoval.Add((TextBox)c);
}
}
foreach (TextBox txt in forRemoval)
this.Controls.Remove(txt);