Search code examples
c#labelvisible

how to make multiple labels visible 0 c# wfapp


it works but what if I have 100 labels? I can't type it 100 times, is there anything to hide all labels

    private void xd()
    {
        foreach(var c in new Panel[] {panel1, panel2, panel3, })
        {
            c.Visible = false;
        }


    }

Solution

  • You can use Filter the Labels by Type. For example,

    foreach (var label in Controls.OfType<Label>())
    {
        label.Visible = false;
    } 
    

    This can be further developed if you need to filter based on particular naming pattern,

    Controls.OfType<Label>().Where(x=>x.Name.StartsWith("la"))
    

    Similarly you can use additional filters if required to further filter your controls.

    PS: btw, your example shows Panel. If you meant Panel, replace the type with Panel.