tags:

views:

4247

answers:

4

In C# i want to create a panel that has the properties of a MDI container ie. isMdiContainer = true.

I tried something like this

form.MDIParent = this.panel1;

But that dont work. Any suggestions?

+1  A: 

You could create custom form, remove all borders, and toolbars to make it look as closely to a panel as possible. Then make that new custom form a MdiContainer.

Basically, you can only set the IsMDIContainer property on a Form. This means that only a form can be a MdiContainer.

Jon
I no what your getting at but you cannot have nested MDI forms :(
Ozzy
I finally get what you ment about the custom form thing. Make it a user control :D
Ozzy
+2  A: 

It is possible to create an MDI-panel and show forms in that panel, something like the code below will do the job

Mdi-Panel definiton:

public class MdiClientPanel : Panel
{
    private Form mdiForm;
    private MdiClient ctlClient = new MdiClient();

    public MdiClientPanel()
    {
        base.Controls.Add(this.ctlClient);
    }

    public Form MdiForm
    {
        get
        {
            if (this.mdiForm == null)
            {
                this.mdiForm = new Form();
                /// set the hidden ctlClient field which is used to determine if the form is an MDI form
                System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                field.SetValue(this.mdiForm, this.ctlClient);
            }
            return this.mdiForm;
        }
    }
}

Usage:

/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
MathiasJ
A: 

There's an issue with the MaskedTextbox using this solution. Somehow you can't manual edit the value of the control. It's not receiving the focus after a mouse click. It becomes completely useless.

A: 
GiannisKhan