UserControl does not auto resize with the Form
Asked Answered
E

2

3

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.

Endosteum answered 15/4, 2013 at 15:14 Comment(0)
I
3

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.

Introduce answered 15/4, 2013 at 16:23 Comment(6)
Thanks for getting back. Unfortunately, setting AutoSize to False did not resolve this issue for me. The UserControl maintains its original size that I assigned in the designer.Endosteum
Also, I changed the "Controls.Clear();" line as per your suggestion. Thanx.Endosteum
@Endosteum The code you posted does not reproduce the same result. You must have some other container control interfering with the panelUserCtrl that is preventing it from resizing correctly.Introduce
Thanks for the suggestions LarsTech. I have two UserControls that produce the same issue. The UserControl contains a TableLayoutPanel and each cell of the TableLayoutPanel contains a separate Panel.Endosteum
@Endosteum Are you sure the UserControl is the control that is having the resizing issues or is the TableLayoutPanel control inside it the one with the resize issues? You are beginning to have a lot of panels inside of panels inside of panels...Introduce
When I removed the TableLayoutPanel I was able to get the desired behavior with the following property setup on the UserContrl:Endosteum
Z
0

You should set AutoSizeMode to GrowAndShrink too.

Zaragoza answered 21/11, 2013 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.