Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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;
}
like image 429
Jay Avatar asked Jan 06 '12 22:01

Jay


People also ask

How do you set the child form in parent form with full screen size?

Solution 2 You can make a child fill teh parent client area very easily: just set the WindowState property to FormWindowState. Maximized but you cannot make it fill the screen, or appear outside the bound of the parent. To get the child outside, you have to just not set the MDIParent property before you show the form.

What is an MDI parent form?

The foundation of a Multiple-Document Interface (MDI) application is the MDI parent form. This is the form that contains the MDI child windows, which are the sub-windows wherein the user interacts with the MDI application. Creating an MDI parent form is easy, both in the Windows Forms Designer and programmatically.

What are MDI forms?

Multiple-document interface (MDI) applications enable you to display multiple documents at the same time, with each document displayed in its own window. MDI applications often have a Window menu item with submenus for switching between windows or documents.


1 Answers

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;
}
like image 86
MDe Avatar answered Oct 05 '22 03:10

MDe