Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControl does not auto resize with the Form

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.

like image 321
user1391337 Avatar asked Dec 06 '25 00:12

user1391337


1 Answers

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.

like image 158
LarsTech Avatar answered Dec 09 '25 19:12

LarsTech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!