Problem when displaying one form within another

Asked

Viewed 186 times

1

I’m having trouble displaying one form inside another through a panel. The point is that by modifying the state of the main form of WindowState == FormWindowState.Normal for WindowState == FormWindowState.Maximized, the form opened in the panel does not follow the size of the main form, so:

Normal form:

formulário normal

Maximized form:

formulário maximizado

Another problem is that by passing the main form from normal to maximized with some form open in the panel, the main form does not fill the entire screen, thus:

problema no form

Code used to open Forms on the panel:

public void AbrirFormulario(Type formType)
    {
        pnFormulários.Enabled = true;
        pnFormulários.Visible = true;
        this.SuspendLayout();

        if (btVoltarInicio.Enabled == false)
        {
            try
            {
                for (int i = 0; i < lstFormulario.Count; i++)
                {
                    lstFormulario[i].Close();
                    lstFormulario[i].Dispose();
                    lstFormulario[i] = null;
                }

                lstFormulario.Clear();

                Form form = (Form)Activator.CreateInstance(formType);
                form.WindowState = FormWindowState.Maximized;
                form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                form.TopLevel = false;

                pnFormulários.Controls.Add(form);
                form.Show();

                lstFormulario.Add(form);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            this.ResumeLayout(true);
            btVoltarInicio.Enabled = true;
            btVoltarInicio.Visible = true;
        }
        else
        {

        }
    }

Button to open some form:

private void btCadCategorias_Click(object sender, EventArgs e)
    {
        AbrirFormulario(typeof(cadastro_categoria));
    }
  • 1

    Have you tried changing the Anchor property of the panel? In theory, if you leave with "Top, Bottom, Left, Right', the panel will follow the size of the main form.

  • @Marcelomonteiro - I’ve done it, it didn’t work, I think the problem is in the event: Openformulario

1 answer

2


Browser other questions tagged

You are not signed in. Login or sign up in order to post.