简体   繁体   English

打开时如何防止Mdi子窗体大小发生变化

[英]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.我正在尝试调整 Mdi 父窗体的大小以适应 Mdi 子窗体的大小。 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.目前,除了 Visual Studio 创建的 InitializeComponent() 行之外,子窗体上没有任何代码。 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?打开时如何防止 Mdi 子窗体改变大小?

In the Child Form make the AutoScaleMode proprity to None . 在子窗体中,将AutoScaleMode属性设置为None This will fix it. 这将解决它。

Try setting of the child before attaching it to the parent will fix weird behaviors such as auto resize.在将其附加到父级之前尝试设置子级将修复奇怪的行为,例如自动调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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