How to open a Form( or hide a Form) inside a Panel by another Form

Asked

Viewed 73 times

2

I have an application in C# and I wonder if it would be like when I press the add button (whose arrow is pointing in the image) this form that is in light blue inside a Panel if it closes and opens another form that in case would be the frmCadastro.

I was using this code to see if it opened, but without success:

frmPaginaInicial pg = new frmPaginaInicial();
pg.painelMenu.Controls.Clear();

Leitor.frmAddLeitor ad = new Leitor.frmAddLeitor();
ad.TopLevel = false;
ad.Dock = DockStyle.Fill;
ad.FormBorderStyle = FormBorderStyle.None;

pg.painelMenu.Controls.Add(ad);
ad.Show();

inserir a descrição da imagem aqui

  • Does it give any error? Put more details in your question pf.

  • A question, you said in the question that you would like to open the form frmCadastro, but in your code is creating the form frmAddLeitor. After all, which form would you like to open?

  • I already managed to find the solution, but thanks for the attention John

1 answer

1


The problem is that you need to take your current instance (which is created and saved in memory) from the class frmPaginaInicial, so the rest of your code will work normally:

// Aqui pegamos a instância atual da classe "frmPaginaInicial"
frmPaginaInicial pg = Application.OpenForms["frmPaginaInicial"] as frmPaginaInicial;
// Removemos todos os controles do painel menu
pg.painelMenu.Controls.Clear();

// Criamos uma nova instância do frmAddLeitor
Leitor.frmAddLeitor ad = new Leitor.frmAddLeitor();

// Dizemos que não é o elemento principal/elemento pai
ad.TopLevel = false;
// Preenchemos o painel por completo
ad.Dock = DockStyle.Fill;
// Retiramos a borda
ad.FormBorderStyle = FormBorderStyle.None;
// Adiciona o formulário dentro do painel menu
pg.painelMenu.Controls.Add(ad);
// Mostramos o formulário
ad.Show();
  • It worked, thank you very much Pedro Paulo

  • If the answer helped you and can help others, please mark it as the accepted answer. Thank you! :)

Browser other questions tagged

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