0
votes

I am trying to resize an Mdi parent form to accommodate the size of an Mdi child form. So, when the child form is opened, the parent form resizes based on the size of the child form, and the child form remains the same size as created in the designer.

This seems simple enough, and I've almost achieved it with the following:

frm_newWindow newWindow = new frm_newWindow();
this.ClientSize = newWindow.Size;
newWindow.MdiParent = this;
newWindow.Dock = DockStyle.Fill;
this.MinimumSize = this.Size;
newWindow.Show();

At first, I thought it worked great -- the parent form grew bigger and the child form filled the client area -- and I was going to move on. However, I looked closer and realized that the sizing is not correct. The parent form does re-size, but for some reason the child form size is being changed as well. As a result, the controls on the child form are incorrect sizes and in incorrect locations. They are fairly close, but not correct. For example, in the designer, a textbox that is "auto-positioned" below a listbox (shows a blue line between the textbox and listbox while dragging it into position) ends up being lower down on the form at runtime.

I also tried:

this.ClientSize = newWindow.ClientSize

since the child form does not have a border, and I thought that maybe the non-existent borders were still somehow in the calculation for the child form's size. This line behaved the same, though.

There is currently no code on the child form beyond the InitializeComponent() line that Visual Studio creates. The form's border style is set to "None." Besides its size and text, every other property remains the default.

How do I keep the Mdi child form from changing sizes when opening?

1
You're using the wrong tool for the job. MDI is an outdated paradigm, and MDI also requires the child forms to have the normal sizable borders in order to work correctly. You can make the parent form IsMDIContainer = false, change your child forms into TopLevel = false, and then do the same thing you are doing.LarsTech
@LarsTech thanks for looking at this with me. What does changing TopLevel do? When I tried this on one form, that form doesn't appear to show anywhereelmer007
You would have to add it to your parent container. this.Controls.Add(yourChild);. Don't forget to make it visible, too: yourChild.Visible = true;LarsTech
@LarsTech I think I understand now (I'm adding the child form to the parent form as if it's a control). I am able to see the form using those lines you mention, although the positioning is initially off (but not sized incorrectly as described in the question, so that's good). Thanks for the help- I'm going to work with this setup for a bit and see if I can get it to do what I need. I've (obviously) never gone this route beforeelmer007

1 Answers

0
votes

In the Child Form make the AutoScaleMode proprity to None. This will fix it.