Click here to Skip to main content
15,867,568 members
Articles / .NET
Tip/Trick

Control Resizing on a UserControl in WinForms

Rate me:
Please Sign up or sign in to vote.
4.95/5 (7 votes)
21 May 2010CPOL 78.4K   7   7
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.



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
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.

Comments and Discussions

 
GeneralReason for my vote of 1 Not a very clear example and respons... Pin
digger6927-Sep-10 6:15
digger6927-Sep-10 6:15 
Reason for my vote of 1
Not a very clear example and responses to posts condescending.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.