问题描述
我在将用户控件设置为使用创建它的面板自动调整大小时遇到问题.当用户调整包含用户控件的主窗体的大小时,该用户控件的大小根本不会改变,从而导致一些糟糕的用户体验.
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:
- 确保用户控件上的 MinimumSize 和 MaximumSize 属性设置为 0.
- 将 (1) 用户控件和 (2) 其所在面板的 AutoSize 属性设置为 True
- 将面板上的锚属性设置为顶部、底部、左、右强>
- 将用户控件的 Dock 属性设置为 Fill(我使用以下代码执行此操作)
- Ensure MinimumSize and MaximumSize properties on the user control are set to 0.
- Set AutoSize property on both (1) the user control and (2) the panel where it resides to True
- Set Anchor property on the panel to Top, Bottom, Left, Right
- 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 不会随窗体自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!