Make panel invisible when opening Form

Asked

Viewed 38 times

-1

My program is opening a form2 inside a panel in Form1, I want when I close this form2 my panel is invisible

private void button1_Click(object sender, EventArgs e)
        {
            panelPrinc.Visible = true;

            Form2 frm2 = new Form2();
            
            frm2.TopLevel = false;
            frm2.AutoScroll = true;

            panelPrinc.Controls.Add(frm2);

            frm2.Show();

            if ()
            {
                panelPrinc.Visible = false;
            }
        }

1 answer

0


You just need to create an event FormClosed to the Form2.

Following the same code idea you did, I added the call to the event.

private void button1_Click(object sender, EventArgs e)
{
    panelPrinc.Visible = true;
    Form2 frm2 = new Form2();
    frm2.TopLevel = false;
    frm2.AutoScroll = true;
    panelPrinc.Controls.Add(frm2);
    
    //Esse evento será disparado no momento que o form2 for fechado
    frm2.FormClosed += new FormClosedEventHandler(Form2Fechado);
    frm2.Show();
}

Then you just need to create the method that receives the event within your main form

private void Form2Fechado(object sender, FormClosedEventArgs e)
{
   panelPrinc.Visible = false;
}
  • Thank you very much!!!

Browser other questions tagged

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