If you have only the labels mentionned, you can use the @Stefan solution.
If you want to set all controls of the same type, you can use that:
private void ButtonAction_Click(object sender, EventArgs e)
{
SetLabelsVisibility(this, false, true);
}
private void SetLabelsVisibility(Control control, bool state, bool recurse = false)
{
if ( !recurse )
Controls.OfType<Label>().ToList().ForEach(c => c.Visible = state);
else
foreach ( Control item in control.Controls )
{
if ( item is Label )
item.Visible = state;
else
if ( item.Controls.Count > 0 )
ToggleLabelsVisibility(item, state, recurse);
}
}
Using recursivity on the form or on any container will change the visibility of all inner labels (or any other type of control you want) as well as all in inner containers and so on.
To toggle the visibility you can use a conditional variable such as:
private bool IsSomePanelLabelsVisible = true;
// To initialize at startup if needed
SetLabelsVisibility(SomePanel, IsSomePanelLabelsVisible);
// To toggle labels
IsSomePanelLabelsVisible = !IsSomePanelLabelsVisible;
SetLabelsVisibility(SomePanel, IsSomePanelLabelsVisible);
You can simplify and take the code you need from what is above.