Search code examples
c#winformsmdi

C# - How to set up a small Mdi "area" for MdiChildren?


I have a Mdi parent form and some Mdi child forms. It works good so far but I want to define a smaller area on the parent form where the Mdi children can be moved around. It looks like the property MdiParent is of type Form. Now I wonder how can I make the children move inside a specific area but not inside the whole parent window.

class MdiParentForm: Form
{
    public MdiParentForm()
    {
        this.IsMdiContainer = true;
        Form form = new Form();
        this.AddOwnedForm(form);
        form.MdiParent = this;
        form.Show();
    }
}

Solution

  • You can just set the Padding of your Mdi form. The padding is the distance between the control (as a container) and its child controls. There are 4 sides: left, top, right, bottom. This code just makes all the sides the same:

    Padding = new Padding(50);
    

    You can notice that the BackColor around the MdiClient is not affected. To affect the BackColor around the MdiClient, we have to override the OnPaint so that the default behavior is not processed:

    BackColor = Color.Green;//try setting the BackColor of the Mdi form to Color.Green
    protected override void OnPaint(PaintEventArgs e){
       RaisePaintEvent(this, e); //remove the base.OnPaint(e)
    }
    

    If you want to take full control over the MdiClient, just declare a variable to hold the MdiClient:

    MdiClient client = Controls.OfType<MdiClient>().First();
    

    Then you can use its Properties and methods like as you do on a form, such as client.Dock = DockStyle.Left, client.Width = 400;, ...

    enter image description here