Parent form for the panel

I have a tree structure on one side. Depending on what node is selected, I want to display different content on the right. To preserve the managed code and controls, my plan was to isolate the content in separate forms and display the form inside the panel.

In my TreeView AfterSelect event, I tried to instantiate the form and set it to the parent, but I get an exception. "The top-level control cannot be added to the control."

Form frmShow = new MyForm();
frmShow.Parent = this.pnlHost;

This is not an MDI setting, but I tried setting the MdiParent property for the form to the parent form, and then setting the Parent property to the form, but I get an exception. "The form that was specified as MdiParent for this form is not an MdiContainer. Parameter name: value":

Form frmShow = new MyForm();
frmShow.MdiParent = this;
frmShow.Parent = this.pnlConfigure;

I cannot set the form as an MDI container, because it is not a top-level form, it is actually a form that dock within the parent form (using the WeifenLuo dock library).

Is there a way to parent form in a panel in an environment without MDI?

+5
source share
5 answers

You will be better off creating each panel as UserControl. They look like shapes, but without window elements.

+3
source

. , TopLevel false. :

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
        switch (e.Node.Name) {
            case "Node0": embedForm(new Form2()); break;
            // etc..
        }
    }
    private void embedForm(Form frm) {
        // Remove any existing form
        while (panel1.Controls.Count > 0) panel1.Controls[0].Dispose();
        // Embed new one
        frm.TopLevel = false;
        frm.FormBorderStyle = FormBorderStyle.None;
        frm.Dock = DockStyle.Fill;
        frm.Visible = true;
        panel1.Controls.Add(frm);
    }

.

+18

, . UserControls . / UserControl , Form, ().

+3
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
    ucAdmin ucA = new ucAdmin(); //ucAdmin is a user control u had created.
    ucA.Visible = true;
    ucA.Dock = DockStyle.Fill;

    this.pnlMain.Controls.Clear(); // pnlMain is the location u are going to display this user control.
    this.pnlMain.Controls.Add(ucA);
}
+2

frmShow.TopLevel = false, - - UserControl.

+1

All Articles