Link to home
Start Free TrialLog in
Avatar of Lensolo
Lensolo

asked on

c# child mdi does not maximize with parent

I have a MDI form with a child form docked that works great until I maximize the window. When I do, the child form does not maximize to fill up the MDI Parent. I have been trying to catch & resize the object on Parent Resize, but I'm just not having luck.

Also, the child's Window state is set to maximize and autosize is set to True (I have tried it both ways). I have also set the parent form ISMDIContainer to True.

My code to load the client from a menu item click on the parent MDI form is:
            frmChild newCurt = new frmChild();
            newCurt.TopLevel = false;
            newCurt.AutoScroll = true;
            newCurt.FormBorderStyle = FormBorderStyle.None;
            this.Controls.Add(newCurt);
            newCurt.Show();

I am building this in VS2012 / c# .net 4.5.
Avatar of SerjTech
SerjTech

When you create the child is it set to FormWindowState.Normal; ?

Then when the parent is maximized set the child's window state, in the code, to maximized.

Does this work?

If not let me know and will try and build a quick demo to test it.
Avatar of Lensolo

ASKER

I don't see a way to set the child WindowState when the parent resizes. This is the only way I know to set the child properties after iot has been added to the parent controls.

 MdiClient client = null;
            foreach (Control contr in this.Controls)
            {
              if (contr.GetType() == typeof(MdiClient))
            {
                client = (MdiClient)contr;
                client.Size = new Size(this.Width,this.Height);
             }
            }
Avatar of Lensolo

ASKER

If you can create 2 new forms & get them to resize with eachother, I just need to know what settings makes this work. I am spending too much time on this and getting no results. I'm really surprised this is not doing it as expected.

Also, the suggestion you mention above does not work with the code I published.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lensolo

ASKER

Oh. Thanks! That's what I get for googling a solution. I'll try that.
Avatar of Lensolo

ASKER

That didn't fix the issue, but I dug up some code on Microsoft and I see why you said I wasn't using it correctly. I had to just set the parent of the MDI child. The following code works. I'm posting in case someone else find the same 10 or so links I found from Googling this that work, but aren't the right way to code this. Thanks for pointing that out.

            this.IsMdiContainer = true;
            fMDIChildTest newMDIChild = new fMDIChildTest();
            newMDIChild.MdiParent = this;
            newMDIChild.WindowState = FormWindowState.Maximized;
            newMDIChild.FormBorderStyle = FormBorderStyle.None;
            newMDIChild.Show();

This is from: http://msdn.microsoft.com/en-us/library/aa984329(v=vs.71).aspx
An MDI setup with no Form Border also don't seem to go together in my mind.  The whole point of MDI is that you can move the windows around inside the parent client area.  Not sure what your app looks like, or how it behaves, so I could be wrong.  Good luck!
Avatar of Lensolo

ASKER

I just wanted to contain most of the screens to a designated location on my parent form & fill the form. A menu strip will switch out the screens. Some screens will become popup screens, but very specific ones & I will handle these differently. Anyway, thank you.
"I just wanted to contain most of the screens to a designated location on my parent form & fill the form."

You can do that using your original approach setting TopLevel to false and adding the form to a SPECIFIC container.  For instance, you can add a Panel to your form, then add the Form to that Panel in code.  With Dock set to Fill on the Form, it will resize when the Panel resizes.  Just make sure your Panel is setup correctly to resize when the main form resizes.

So instead of:

    this.Controls.Add(newCurt);

You'd have something like:

    panel1.Controls.Add(newCurt);