Control Resizing on a UserControl in WinForms
Control resizing is a bit more mysterious than it might appear at first glance.
Let's say you have a
UserControl
, and on that user control, you have placed a control (any control), setting the Dock
property to Fill
. From hereon, we will refer to this control as the "filling control".
Let's further assume that you've placed a Panel
(or other type of appropriate container) onto a Form
, and set the Anchor
property of the container to grow/shrink when you resize the form.
Finally, you would probably programatically add the user control (that contains the filling control) to the panel.
If you perform the steps outlined above, you will observe that the filling control will NOT resize itself to the container control, even though the container control will happily obey the Anchor
property setting.
The problem is that the designer doesn't allow you to set either the Dock
or the Anchor
properties. You MUST manually set the appropriate property in the UserControl
Load
event handler (or someplace else equally appropriate).
I struggled with this for two hours (trying to capture Size events, and other things) before stumbling on the solution. I had completely missed the fact that there were no Dock
/Anchor
property settings in the designer for UserControl
objects.