Grammarian
Grammarian

Reputation: 6882

How can I find the content area of a MDI parent form?

I want to know the bounds of the grey scrollable area in an MDI parent -- the area in which MDI children are placed/arranged. I don't want it to include any menu, scroll bars, or status areas -- just the grey area.

this.mdiForm.ClientRectangle gives the whole interior of the Form, including scroll bars et al, which is not what I want.

Upvotes: 1

Views: 2046

Answers (2)

Grammarian
Grammarian

Reputation: 6882

As always, just after posting, I figure it out.

Form has an internal property MdiClient. So, you can get the rectangle like this:

PropertyInfo pi = typeof(Form).GetProperty("MdiClient", 
    BindingFlags.Instance | BindingFlags.NonPublic);
MdiClient mdiClient = (MdiClient)pi.GetValue(this.form1, null);
Rectangle scrollableRect = mdiClient.ClientRectangle;

A production version would, of course, check for null in the appropriate places.

Upvotes: 0

Josh
Josh

Reputation: 69272

The control is called MdiClient and it's automatically added when the IsMdiContainer property is set to true. You should be able to access it by doing:

// traditional way
MdiClient client = null;
foreach (Control c in this.mdiForm.Controls) {
    client = c as MdiClient;
    if (client != null) {
        break;
    }
}

// linq
MdiClient client = this.mdiForm.Controls
                       .OfType<MdiClient>()
                       .FirstOrDefault();

Upvotes: 1

Related Questions