Danilo Ivanovic
Danilo Ivanovic

Reputation: 1226

How to make an exception on hide all panels?

I just saw answer on another question for hiding all panels in form. So i wonder how to make exception to this code. This code is in C#.

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

I tried to add another if to check if c is my panel but that does not work.

if (c is MyPanel) continue;

MyPanl is name of my panel.

Error list say A constant value is expected

Can someone help?

Upvotes: 1

Views: 63

Answers (1)

D-Shih
D-Shih

Reputation: 46219

From your comment you can try to use c == MyPanel to be the condition instead of c is MyPanel because ... is ... checked the type instead of compare instance.

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

I would use linq where to set the condition to let the code clearer

var panels = this.Controls
    .Cast<Control>()
    .Where(c => c != MyPanel && c is Panel);

foreach (var c in panels)
{
    c.Visible = false;
}

Upvotes: 2

Related Questions