UserControl does not auto resize with the Form

断了今生、忘了曾经 提交于 2019-12-09 19:25:48

问题


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. 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);

Any suggestions would be much appreciated.


回答1:


Try setting the AutoSize properties to False.

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.




回答2:


You should set AutoSizeMode to GrowAndShrink too.



来源:https://stackoverflow.com/questions/16018784/usercontrol-does-not-auto-resize-with-the-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!