C # - Hide or show panels within a SplitPanel

1

In a form I have a SplitContainer: in the left panel I have a series of buttons and in the right several panels COUPLED to the right panel of the SplitContainer.

The idea is: by pressing the different buttons on the left panel, the different panels on the right are shown (one at a time depending on the button pressed, since the panels are attached).

The problem I have is that pressing the second button, for example, shows me the panel on the right blank (as if all the panels contained in it were hidden) but when I press the first one to go back to the main section I shows the panel corresponding to the second button.

Code:

    //Segundo boton
    private void btnDatosGenerales_Click(object sender, EventArgs e)
    {
        OcultarOtrosPaneles(panDatosGen);
    }

    // Primer boton
    private void btnDatosPersonales_Click(object sender, EventArgs e)
    {
        OcultarOtrosPaneles(panDatosPer);
    }

    private void OcultarOtrosPaneles(Panel panelActivo)
    {
        foreach (Panel panel in this.splPrincipal.Panel2.Controls.OfType<Panel>())
        {
            panel.Visible = false;
        }
        panelActivo.Visible = true;
    }

Things I tried:

  • Cool the panels (with Refresh ()).
  • Pass the "panelActive" parameter as a reference.
  • Use the BringToFront () or SendToBack () method with the panels as appropriate.

Any ideas or suggestions?

    
asked by Willy616 18.04.2017 в 18:02
source

1 answer

2

I would say that your problem may be here: foreach (Control ctrl in this.Controls) .

this.Controls probably refers to the form, not the SplitContainer . To go through all the Panels within SplitContainer , I would do something like this:

foreach(Panel panel in this.splitContainer1.Panel2.Controls.OfType<Panel>())
{
    panel.Visible = false;
}
    
answered by 18.04.2017 / 18:24
source