Search code examples
c#.netinstallationpanel

Hiding and showing panels


EDIT
I've found and posted the solution.

I am trying to make an installer for my application and I am trying to do that with panels (I don't know if this is a good way of doing it, but this gives me more customization options instead of using the install shield program). What would be the best way to do this?
This is the code I have right know:

C# Code

foreach (var c in Controls)
{
    if (!(c is Panel)) continue;
    if (c.Name == "pnlBottom") continue;
        
    c.Visible = c.Name.Contains(_currentPanel.ToString());
    
    if (c.Visible) return;
}

Solution

  • Try this, it changes the Visibility of a single Panel:

    private void PanelVisible(string panelName, bool visible)
    {
        var panel = this.Controls.OfType<Panel>().FirstOrDefault(p => p.Name == panelName);
        if (panel != default(Panel)) panel.Visible = visible;
    }
    

    If you want to make all Invisible, but one:

    private void PanelVisible(string panelName)
    {
        foreach(var panel in this.Controls.OfType<Panel>().Where(p=>p.Name!="pnlBottom"))
        {
            panel.Visible = panel.Name == panelName;
        }
    }