Click here to Skip to main content
Click here to Skip to main content

Tagged as

Control Resizing on a UserControl in WinForms

, 21 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
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

 
GeneralMy vote of 5 Pinmemberanon19319-May-13 17:54 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 21 May 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid