Question

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?

Was it helpful?

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.

OTHER TIPS

If you want the window state to always be maximized, I'd recommend switching away from an MDI Form. A TabControl may work better, in that case.

MDI forms have quite a few usability issues, which is why they are not commonly used anymore, and tend to be replaced with other controls/options.

After reading Reeds answer and especially your comment:

problem with tabcontrol is, i have a lot of controls used per child form

Maybe this will help: Don't put your controls into a Winform. Instead encapsulate them into a UserControl (maybe it already works by changing your inheritance from Form to UserControl).

Now put every UserControl on it's own TabPage and set its Dock property to Fill. Now you are able to change each UserControl on it's own, without any interference to another control on another TabPage (as far as you don't built in any connection).

If you intend to give up on MDI, you could have a look at docking frameworks like WeifenLuo or DigitalRune. These are free, for other options you can have a look here: http://windowsclient.net/downloads/folders/controlgallery/tags/Windows+Forms+Docking+Windows/default.aspx


EDIT:

If I remember well, DigitalRune allows the usage of windows forms as containers for docked content so the migration effort would be smaller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top