Solved

c# child mdi does not maximize with parent

Posted on 2014-11-05
9
1,840 Views
Last Modified: 2014-11-07
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.
0
Comment
Question by:Lensolo
  • 5
  • 3
9 Comments
 
LVL 5

Expert Comment

by:SerjTech
ID: 40425217
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.
0
 

Author Comment

by:Lensolo
ID: 40426043
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);
             }
            }
0
 

Author Comment

by:Lensolo
ID: 40426048
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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40427455
You are not really using MDI Forms in the proper manner.  Your child form is not really an MDI child, and your main form does not need IsMdiContainer() set to true.

To "fix" your problem, simply set Dock to Fill for the child form:
newCurt.Dock = DockStyle.Fill;

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Lensolo
ID: 40428236
Oh. Thanks! That's what I get for googling a solution. I'll try that.
0
 

Author Comment

by:Lensolo
ID: 40428282
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40428692
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!
0
 

Author Comment

by:Lensolo
ID: 40428707
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40428787
"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);
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now