Jay Jay - 1 year ago 124
C# Question

Maximize MDI child form

I'm working on a legacy WinForms MDI application and have some trouble making the child forms behave as I want.
My objective is to have the child form always maximized (docked).

The problem is, that even if I set

the maximize/resize button appears in the MDIs toolstrip and let the user resize (undock) the child form.
The only way to avoid this is to set
but then the close button disappears to (thats not what I want).

I've already tried to use a fixed
and to maximize the child form when the resize event is fired but none of my approaches worked.

Is there any super secret property I have missed or is it just impossible?

Best Regards & Thanks in advance


I wrote a sleazy method (thanks to @rfresia) for handling my child form, it may help others who run into the same issue:

//All child forms derive from ChildForm
//Parent MDI Form implementation
private void ShowForm(ChildForm form)
//Check if an instance of the form already exists
if (Forms.Any(x => x.GetType() == form.GetType()))
var f = Forms.First(x => x.GetType() == form.GetType());
f.WindowState = FormWindowState.Maximized;
//Set the necessary properties (any other properties are set to default values)
form.MdiParent = this;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.WindowState = FormWindowState.Maximized;
form.Forms = Forms;
//Lets make it nasty (some forms aren't rendered properly otherwise)
form.WindowState = FormWindowState.Normal;
form.WindowState = FormWindowState.Maximized;

//ChildForm implementation
public List<Form> Forms { get; set; }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
Forms.RemoveAll(x => x.GetType() == GetType());

protected override void OnResize(EventArgs e)
WindowState = FormWindowState.Maximized;

Answer Source

You can override the OnResize of each child form you want to make sure does not minimize. Or create a BaseForm and inherit all children forms from it.

protected override void OnResize(EventArgs e)
        this.WindowState = FormWindowState.Maximized;

Also, you can use X,y coordinates, but OnResize should be enough. Put this in the child form constructor:

   this.WindowState = FormWindowState.Maximized;

        Point NewLoc = Screen.FromControl(this).WorkingArea.Location;
        //Modifiy the location so any toolbars & taskbar can be easily accessed.
        NewLoc.X += 1;
        NewLoc.Y += 1;
        this.Location = NewLoc;

        Size NewSize = Screen.FromControl(this).WorkingArea.Size;
        //Modifiy the size so any toolbars & taskbar can be easily accessed.
        NewSize.Height -= 1;
        NewSize.Width -= 1;
        this.Size = NewSize;

        this.MinimumSize = this.Size;
        this.MaximumSize = this.MinimumSize;

I got the code for the X,Y from here: http://bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download