On Mar 12, 8:21 am, Olan <O...@discussions.microsoft.comwrote:
Hi,
I'm trying to dynamically remove labels from a windows form in csharp. I
have a foreach loop similar to :
foreach(Control c in this.Controls)
{
c.Dispose();
}
The code does not find any labels but works fine for othet controls. I'm
clearing the entire form (excluding menus) so there may be a better way to do
this. I do not know the name(s) of the lables as they are created dynamically.
Ignoring for a moment that I have no idea why anyone would want to do
what you want to do... and therefore suspect that your original
problem (whatever it is) is probably best solved in some other way....
List<ControlcontrolsToRemove = new List<Control>();
foreach (Control c in this.Controls)
{
if (c is Label)
{
controlsToRemove.Add(c);
}
}
foreach (Control remove in controlsToRemove)
{
this.Controls.Remove(remove);
remove.Dispose();
}