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 UserControlLoad 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.

Share

About the Author

I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Of course the user control won't resize because even though the PANEL resizes due to its anchor settings, there has been nothing set to ask the UC to resize. Wouldn't you just want to set the UC to dock Fill too?

My point was that there is no way to do this *in the designer* and that you have to remember to go to the actual source code to establish the desired behavior.

.45 ACP - because shooting twice is just silly-----"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997-----"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

You can set this in the designer. Let's see if I can post enough to show (I wish I could upload a ZIP with the solution...)

I have a UserControl (MyUserCtrl) with a FULL docked TextBox (multiline so it fills the control). The UserControl has a padding value so the TextBox doesn't go all the way to the edges, plus a blue background so you can see the UserControl when displayed on the main form. The main form (Form1) contains an anchored Panel control. The Panel control contains an instance of MyUserCtrl docked FULL. When you resize the form, the panel resizes due to the anchoring, the UserControl resizes due to it being docked, and the TextBox resizes due to it being docked too. I didn't have to do anything in the source -- everything is in the designer.

I think this is what you're trying to do. If I missed the boat, my apologies...

.45 ACP - because shooting twice is just silly-----"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997-----"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001