Q:

Loop through all Controls and Clear desired

Hello I'm trying to find the desired label in controls of my panel1. All my labels are named like "lbl0", "lbl1" and so on.
So I tried to loop through all of these labels until I find the right one:
Control DelCon(string Name)
    {
        foreach (Control c in panel1.Controls)
            if (c.Name == Name)
            {
               c.Controls.Clear(); // this should delete the control
            }

        return null;
    }

But at the end even when the c.Name == Name the control is not deleted.
Might someone help me solve this out?
Thanks in advance. 

A:

A fast fix to your  code above 
Control DelCon(string Name)
{
    Control toRemove;
    foreach (Control c in panel1.Controls)
    {
         if (c.Name == Name)
         {
            toRemove = c;
            break;
         }
    }
    if(toRemove != null)
        panel1.Controls.Remove(toRemove); 
    return null;
}

Your original code remove ALL the child controls (if any) from the control that you found inside the panel collection of child controls. 
As a side note, you cant remove an element from a collection while you are looping over that collection. So a fast fix is to copy the reference of the control to remove and exit from the loop and remove the control (if found) after exiting from the loop
The code above could be shortened using Linq 
Control toRemove = panel1.Controls.Where(x => x.Name == Name).SingleOrDefault();
if(toRemove != null)
    panel1.Controls.Remove(toRemove);

