Abhinav Garg Abhinav Garg - 2 months ago 18
C# Question

how to hide all panels in windows form?

In my windows application which uses menu strip and multiple panel containers, a panel is displayed depending on menu option

hiding all panels by manually passing names is very time consuming, is there any way to hide all panels or any way to get names of all panels in a form??

Answer
foreach (Control c in this.Controls)
{
    if (c is Panel) c.Visible = false;
}

And you could even make that recursive, and pass in the ControlCollection instead of using this.Controls:

HidePanels(this.Controls);

...

private void HidePanels(ControlCollection controls)
{
    foreach (Control c in controls)
    {
        if (c is Panel)
        {
            c.Visible = false;
        }

        // hide any panels this control may have
        HidePanels(c.Controls);
    }
}