Search code examples
c#winformsmdiparent

Tips on setting the window state of a Windows Forms window


I have a Windows Forms application that opens MDI child forms. When I select those forms, I need to set or render its windowstate to Maximized. The problem is, when I navigate between the open forms, it reverts back to the normal window state, and when I set the window state to maximized again, it shows the transition from normal to maximized state and it doesn't look nice.

How can a Windows application be created that have an MDI parent form that opens many MDI childs in maximized window state?


Solution

  • Here's an answer based on using the MDI "Parent Form and Child Form paradigm," with the following assumptions :

    1. you have a MenuStrip control 'Dock = 'Top on your MDIParentForm, and you've implemented the automatic MDI &Window menu handler as described in : How to: Create an MDI Window List with MenuStrip

    2. you are creating new child forms that :

      a. do not have a MaximizeBox, MinimizeBox, etc., but may have ControlBox (for closing them)

      b. these child forms may be resizable or not : we won't consider the implications of that here.

    3. You want these MDIChildForms to display maximized in the MDIParent Form, but not to obscure the MDIParentForm's menu.

    Okay : assuming you have all your child Forms fully designed, "waiting in the wings" : we might see some code like this in your MDIParentForm code :

        // create instances of your child forms
        Form2 f2 = new Form2();
        Form3 f3 = new Form3();
        Form4 f4 = new Form4();
        Form5 f5 = new Form5();
    
        private void MDIParentForm1_Load(object sender, EventArgs e)
        {
            f2.Text = "subForm1";
            f3.Text = "subForm2";
            f4.Text = "subForm3";
            f5.Text = "subForm4";
    
            f2.MdiParent = this;
            f3.MdiParent = this;
            f4.MdiParent = this;
            f5.MdiParent = this;
    
            f2.Dock = DockStyle.Fill;
            f3.Dock = DockStyle.Fill;
            f4.Dock = DockStyle.Fill;
            f5.Dock = DockStyle.Fill;
    
            f2.Show();
            f3.Show();
            f4.Show();
            f5.Show();
        }
    

    At this point, the dock style 'Fill applied to the child forms will make them full-screen, and keep them from obscuring the MDIParentForm menu : and the menu will allow you to auto-select which one is frontmost.

    Now, if you want to do fancier stuff : like resizing the child Forms, tiling them, cascading them. You are going to have to change the 'Dock property of these child windows : and then you can make use of the built-in MDI paradigm window arranging facilities as described here : How to: Arrange MDI Child Forms

    And if you want to create multiple instances of one type of pre-defined child form : How to Create MDI Child Forms ... see the example on how to use a 'New menu entry : may prove useful.