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?
this.Controls.Add(yourChild);
. Don't forget to make it visible, too:yourChild.Visible = true;
– LarsTech