Search code examples
c#panel

Buttons don't always work


I am using a panel in my main form to open every other form in my program, I have a menu on the left side that has buttons for every form and sub menus for other stuff, and it works when I have nothing already loaded in the panel but when I do the buttons on the menu sometimes work and sometime don't...

Here are two screenshots of the menu

enter image description here

enter image description here

and this is the code i use to open forms inside the panel

private void abrirHijo(object formHijo)
    {

        panelContenedor.Controls.Clear();            

        Form fh = formHijo as Form;
        fh.TopLevel = false;
        fh.Dock = DockStyle.Fill;
        this.panelContenedor.Controls.Add(fh);
        this.panelContenedor.Tag = fh;
        fh.Show();
    }

Solution

  • So i ended up resolving this, so the buttons in the menu in the vertical menu for some reason are added as controls to the panel that i use to put forms in so when i use the "panelContenedor.Controls.Clear();" i end up removing all the buttons, so i ended up doing this and it works

    if (panelContenedor.Controls.Count > 6) //it is six because i have 5 controls and when i open a form it turns to six, so if i have a sixth control it means that i have a form open and so i must close it
            {
                panelContenedor.Controls.RemoveAt(6);                                    
            }
    
    
            Form fh = formHijo as Form;
            fh.TopLevel = false;
            fh.Dock = DockStyle.Fill;
            this.panelContenedor.Controls.Add(fh);
            this.panelContenedor.Tag = fh;
            fh.Show();