UserControl 不会随窗体自动调整大小 [英] UserControl does not auto resize with the Form

查看:45
本文介绍了UserControl 不会随窗体自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将用户控件设置为使用创建它的面板自动调整大小时遇到​​问题.当用户调整包含用户控件的主窗体的大小时,该用户控件的大小根本不会改变,从而导致一些糟糕的用户体验.

I am having troubles with setting my user control to automatically resize with the panel where it is created. When user is resizing a main form that contains the user control, the size of this user control does not change at all making for some poor user experience.

到目前为止,我尝试了以下操作:

So far I tried following:

  1. 确保用户控件上的 MinimumSizeMaximumSize 属性设置为 0.
  2. 将 (1) 用户控件和 (2) 其所在面板的 AutoSize 属性设置为 True
  3. 将面板上的属性设置为顶部底部强>
  4. 将用户控件的 Dock 属性设置为 Fill(我使用以下代码执行此操作)
  1. Ensure MinimumSize and MaximumSize properties on the user control are set to 0.
  2. Set AutoSize property on both (1) the user control and (2) the panel where it resides to True
  3. Set Anchor property on the panel to Top, Bottom, Left, Right
  4. Set Dock property to Fill for the user control (I did this with the following code)

这些尝试对我的用户控件的行为没有影响:

These attempts had no effect on the behavior of my user control:

CalcUserControl calcControl = new CalcUserControl(CountryId);
calcControl.Dock = DockStyle.Fill;
panelUserCtrl.Controls.Clear();
panelUserCtrl.Controls.Add(calcControl);

任何建议将不胜感激.

推荐答案

尝试将 AutoSize 属性设置为 False.

Try setting the AutoSize properties to False.

此外,不要调用 Controls.Clear();,而是尝试处理其中的控件,例如:

Also, instead of calling Controls.Clear();, try disposing of the controls inside it, something like:

while (panelUserCtrl.Controls.Count > 0) {
  panelUserCtrl.Controls[0].Dispose();
}

否则,你会泄漏内存,因为那些被移除的控件仍然存在.

Otherwise, you are leaking memory since those removed controls would still exist.

这篇关于UserControl 不会随窗体自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆