Hudson Atwell
Hudson Atwell

Reputation: 192

C#, Select by tag

I'm new with c# and am wanting to set all panels to visible=false that share the same tag. This will prevent me from calling each panel name individually and setting it to false when activating a new panel.

Any help?

This is how I would do it the old way:

private void button3click (object sender, EventArgs e)
{
 Panel1.Visible = false;
 Panel2.Visible = false
 Panel3.Visible = true;
}

Upvotes: 2

Views: 422

Answers (1)

John Saunders
John Saunders

Reputation: 161801

If you have sets of controls that you frequently refer to as a group, then you can try to place those controls into a list:

List<Control> typeAControls = new List<Control>(){control1, control2};
List<Control> typeBControls = new List<Control>(){control3, control4};

foreach (var toHide in typeAControls)
{
    toHide.Visible = false;
}

foreach (var toHide in typeBControls)
{
    toHide.Visible = true;
}

Upvotes: 1

Related Questions