Question

I have a form which is a mdicontainer and has a menu strip at the top. I add a child form to my mdi container and when I maximize the child it maximizes over the menustrip. I want to know how to limit the child to maximize below the menustrip. Any help would be appreciated.

Was it helpful?

Solution

Your child form is being maximized in the way that child forms are supposed to be maximized in MDI. It's not really covering the menu strip of the parent form - it's actually merging its own menu strip with that of the parent form.

To make the child form take up only the available child area in the MDI parent (and not merge its menu with the parent's menu), put something like this code in the child form's Resize event:

if (this.WindowState == FormWindowState.Maximized)
{
    this.WindowState = FormWindowState.Normal;
    this.Size = this.MdiParent.ClientSize;
    this.Location = new Point(0, 0);
}

which will prevent the child window from being actually maximized.

I say "something like this code" because this snippet doesn't work exactly right. The ClientSize property of the parent form gives the overall size of the form, whereas you want to use the size of just the MDI client area. I don't know how to get that, and apparently it's not super-easy. See this question:

Size/Location of Winforms MDI Client Area

OTHER TIPS

You could set the MaximumSize property so that it doesn't fill up the entire container.

I know this an old question, but I just ran into this on an old project I'm working on, so here's an answer for anyone seeing this. Setting the Dock to DockStyle.Fill will give you the behaviour you want.

Just be aware that the window will act/look strange if you try to reposition or resize the window while it has that DockStyle.

To accomplish this, I subscribed to the MDI client window's resize event and if the window had just been maximized, I set its DockStyle to Fill, set the FormBorderStlye to FixedDialog(to prevent resizing), and set the window state to normal to prevent the maximization from occurring.

To prevent the user from moving the window while it is in this "maximized state" I simply overrode the WndProc method and handled when the window was being moved(SC_MOVE) and placed a return to prevent the action from taking effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top