Maximize MDI child form
Asked Answered
O

5

12

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 MaximizeBox to false 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 ControlBox to false but then the close button disappears to (thats not what I want).

I've already tried to use a fixed FormBorderStyle 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

Update

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.Focus();
        f.WindowState = FormWindowState.Maximized;
    }
    else
    {
        //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;
        Forms.Add(form);
        form.Forms = Forms;
        form.Show();
        form.Focus();
        //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;
}
Oysterman answered 6/1, 2012 at 22:5 Comment(2)
There's no point in using MDI when you want the child forms always to be maximized. MDI fights you tooth and nail anyway with lots of flicker when you switch windows. Just use UserControls that you swap into a form.Lodi
You're probably right, but as I said it's legacy code and I don't want to touch it to much.Oysterman
P
16

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

Purl answered 6/1, 2012 at 22:36 Comment(0)
H
22

The problem wasn't easy to solve, but I accidentally found the answer and it's quite simple; set the windowstate of the child form to Normal by default. Then make sure that you reset the windowstate of the child window AFTER you call the Show() method.

Example:

private void ShowNewForm(object sender, EventArgs e)
{
  Form childForm = new Form();
  childForm.MdiParent = this;
  childForm.Text = "Window " + childFormNumber++;
  childForm.Show();
  childForm.WindowState = FormWindowState.Maximized;
}
Hiss answered 2/10, 2012 at 14:15 Comment(0)
P
16

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

Purl answered 6/1, 2012 at 22:36 Comment(0)
J
5
form1 obj = new form1 ();
obj.MdiParent = MDIGracular.ActiveForm;
obj.StartPosition = FormStartPosition.CenterParent;
obj.WindowState = FormWindowState.Minimized;
obj.Dock = DockStyle.Fill;
obj.Show();
obj.WindowState = FormWindowState.Maximized;
Justice answered 14/10, 2016 at 13:19 Comment(0)
E
3

This is how I overcame the same promblem, cannot remember where I found the code.

private const int WM_SYSCOMMAND = 0x112;
private const int SC_MINIMIZE = 0xF020;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_CLOSE = 0xF060;
private const int SC_RESTORE = 0xF120;

protected override void WndProc(ref Message msg)
{
  if ((msg.Msg == WM_SYSCOMMAND) && 
    (((int)msg.WParam == SC_MINIMIZE) || ((int)msg.WParam == SC_MAXIMIZE) ||
    ((int)msg.WParam == SC_CLOSE)) || ((int)msg.WParam == SC_RESTORE))
    {
      //do nothing
    } // end if
    else
    {
      base.WndProc(ref msg);
     } // end else
}
Exuberance answered 24/9, 2012 at 13:55 Comment(0)
C
1

In my app I found if I put just these two lines in the form loaded event it worked. Thanks sarvjeet for the basic idea. +1 for you

this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
Coeternal answered 1/9, 2017 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.