简体   繁体   中英

How to prevent Mdi child form size from changing when opening

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?

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

Try setting of the child before attaching it to the parent will fix weird behaviors such as auto resize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM