Maximize MDI Child Form

I am working on a legacy WinForms MDI application and am experiencing certain issues with the fact that child forms behave the way I want. My goal is to ensure that the shape of the child is always maximized (docked).

The problem is that even if I set MaximizeBox to false , the maximize / resize button appears in the MDI toolbar and allows the user to resize (unpin) the child form. The only way to avoid this is to set the ControlBox to false , but then the close button will disappear (this is not what I want).

I already tried using a fixed FormBorderStyle and maximizing the child form when the resize event was FormBorderStyle , but none of my approaches worked.

Is there any top-secret property that I missed, or is it just not possible?

Best regards and thanks in advance

Update

I wrote a messy method (thanks to @rfresia) to handle my child form, this may help others who are facing the same problem:

 //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; } 
+7
source share
5 answers

You can override OnResize for each child form that you want to make sure that it is not minimized. Or create a BaseForm and inherit all child forms from it.

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

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

  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 X, Y from here: http://bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing

+12
source

The problem was not easy to solve, but I accidentally found the answer, and it is quite simple; by default, set the window state of the Normal child form. 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; } 
+13
source

This is how I overcame the same problem, I can’t 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 } 
+2
source
 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; 
+2
source

In my application, I found that if I put only these two lines in the loaded form event, it worked. Thanks to sarvjeet for the basic idea. +1 for you

 this.WindowState = FormWindowState.Minimized; this.WindowState = FormWindowState.Maximized; 
0
source

All Articles