Frage

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();
    }
}
War es hilfreich?

Lösung

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

Andere Tipps

It is automatic when you Dock other controls to the edges of the MDI parent form. The dark-gray MDI client window shrinks to fit the remaining space. Using the Dock property is the essential part.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top