Removing components from controls
-
Hello, in runtime I create compoments (labels, checkboxs) later I want to clear those, but method that i`m using remove only half of them. Here is that method: foreach (Control ctrl in Controls) { if (ctrl is Label) { Controls.Remove(ctrl); } }
-
Hello, in runtime I create compoments (labels, checkboxs) later I want to clear those, but method that i`m using remove only half of them. Here is that method: foreach (Control ctrl in Controls) { if (ctrl is Label) { Controls.Remove(ctrl); } }
You are not allowed to remove elements of a list within a foreach on it. You should do it this way:
for (int i = Controls.Count - 1; i >= 0; i--) {
if (Controls[i] is Label)
Controls.Remove(Controls[i]);
} -
You are not allowed to remove elements of a list within a foreach on it. You should do it this way:
for (int i = Controls.Count - 1; i >= 0; i--) {
if (Controls[i] is Label)
Controls.Remove(Controls[i]);
}now it`s working, thank you very much.