C# Windows Forms Calling a Grandchild Form inside a panel in the Grandparent form from the Parent Form

Asked

Viewed 267 times

2

I have 3 forms, Main, product and details. In the main form I have a side menu and a panel called pnlContent. In this menu I have a button that fills the pnlContent with the product form. Inside the product form I have another button that fills the pnlContent with the form Details but does not display. They could help me?

(Main form)Call to the product form:

    private void btnProduto_Click(object sender, EventArgs e)
    {
        frmProduto frm = new frmProduto
        {
            TopLevel = false,
            AutoScroll = true,
            FormBorderStyle = FormBorderStyle.None,
            Dock = DockStyle.Fill
        };

        this.pnlContent.Controls.Clear();
        this.pnlContent.Controls.Add(frm);
        frm.Show();
    }

(Form Product)Call to form details:

    private void btnDetalhes_Click(object sender, EventArgs e)
    {          
        frmDetalhes Detalhes = new frmDetalhes
        {
            TopLevel = false,
            AutoScroll = true,
            FormBorderStyle = FormBorderStyle.None,
            Dock = DockStyle.Fill
        };

        frmPrincipal Principal = new frmPrincipal();
        Principal.pnlContent.Controls.Clear();
        Principal.pnlContent.Refresh();
        Principal.pnlContent.Controls.Add(Detalhes);

        Detalhes.Show();
        Principal.Refresh();   
        Principal.pnlContent.Refresh();
        this.Close();
    }
  • Let me see if I understand, you carry the product form on pnlContent from the main and to and from the product you want to replace the content of the pnlContent the details form of that product?

  • Managed to solve??

1 answer

0


Instead of instantiating frmPrincipal again, you can access it through Parent and locate the control panel.

var Principal = this.ParentForm;
var pnlContentPrincipal = Principal.Controls.Find("pnlContent", true).FirstOrDefault(); 
    pnlContentPrincipal.Controls.Clear();
    pnlContentPrincipal.Refresh();
    pnlContentPrincipal.Controls.Add(Detalhes);

    Detalhes.Show();

    Principal.Refresh();
    pnlContentPrincipal.Refresh();
    this.Close();
  • Thank you Leandro Angelo, it worked.

Browser other questions tagged

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