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.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
user1391337
  • 35
  • 1
  • 4

2 Answers2

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.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 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. – user1391337 Apr 15 '13 at 19:55
  • Also, I changed the "Controls.Clear();" line as per your suggestion. Thanx. – user1391337 Apr 15 '13 at 19:56
  • @user1391337 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. – LarsTech Apr 15 '13 at 20:02
  • 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. – user1391337 Apr 16 '13 at 15:48
  • @user1391337 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... – LarsTech Apr 16 '13 at 15:52
  • When I removed the TableLayoutPanel I was able to get the desired behavior with the following property setup on the UserContrl: – user1391337 Apr 16 '13 at 18:43
0

You should set AutoSizeMode to GrowAndShrink too.

Sam
  • 1