How to avoid automatic (and illogical) resize of UserControls when added to a panel in VB.NET?

Posted on 2012-04-07
Last Modified: 2012-06-27
I made some layout changes for my application in the Visual Studio designer and all of a sudden the automatic resizing of controls acts kinda weird - or at least I'm no experienced enough to figure out why it happens.

This is the case: I have a content panel to which I add content in the form of fixed size UserControl objects at run time. By fixed size I mean that MinimumSize = MaximumSize = Size of these controls. This has worked out fine before, but all of a sudden content is resized (even though its fixed size) in a way that so that it becomes larger than the content container making some of it hidden. I can't figure out what I have done to mak this happen.

Please see the screenshot of a debug session in which the sizing is illustrated in through the watch variables to see what I mean. Note that the ContentForm has Dock = DockStyle.Fill set before being passed to the AddContentForm Sub.

I have been fiddling with the AutoSize, Locked, Dock, Size(s) etc. properties, but nothing seems to solve this very illogical behavior. Does any one have the slightest idea what is causing this?

Debug session showing the weird size change
Question by:andreas_rafn
LVL 17

Expert Comment

ID: 37819157
If you are expressly setting the size of the usercontrol, then you HAVE to set the Autosize to False. In addition (and depending on what control hosts your usercontrol) you have to set a maximum size (and be sure to set this AFTER parenting).
LVL 14

Expert Comment

by:nishant joshi
ID: 37819167
whenever you add content to form,the size of form always increases.
it not possible to add content runtime and not to increase form size.
try to set intialtial size as max that you want to add content.
or try to set size of form as in your beforeSize variable.


Author Comment

ID: 37819400
I just dont understand why the content UserControl changes size when it has a fixed size (MinimumSize = MaximumSize = Size), AutoSize=False and Dock=Fill, and is added to a Panel with the exact same size with no padding. Btw only its width changes for some reason, never its height.

As to your answers, the content has MinimumSize = MaximumSize = Size set to a fixed size before being added, and should not be able to resize, but yet it is.

I can reset the size of the content usercontrol after adding it to the parent panel, but then all the child controls of the content UserControl are stretched and some are out of the bounds (not visible). So what I want to obtain is that the UserControl stays fixed at all times. All sizes are fine until its added to the parent panel.

I have added some more attachments that might enable you to help:

Layout of application. the "Content Panel" is the Panel in which the panel to which content usercontrols are added:

Layout of application. the "Content Panel" is the Panel in which the panel to which content usercontrols are added
the properties of the "Content Panel" panel (its name: ContentContainer):

the properties of the "Content Panel" panel (its name: ContentContainer):
finally an example of the properties of a content userform (they are all the same fixed size):

an example of the properties of a content userform (they are all the same fixed size):
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

LVL 17

Expert Comment

ID: 37819436
Do NOT dock.fill the usercontrol, just add it to the panel (and may-be

Author Comment

ID: 37819484
DockStyle Top, None, Fill, its the same weird outcome.

Note that the container to which I add the content is exactly the same size as the content (if the padding is added) before the content is added (I've checked with console.write's at runtime). Still the size of the content (the child) and not the panel (the parent) is increased after being added making part of the child hidden and even worse all the controls of the content is stretched.

In theory should adding say a size 500,500 usercontrol to a size 500,500 panel produce the same result regardless of dockstyle? And why does the added control then increase in size? I can't make any sense of this behavior.
LVL 83

Expert Comment

ID: 37819672
Just as a test, still same problem if you change Dock of content panel to none?

Author Comment

ID: 37819821
Content panel dockstyle none is same same but different:
The width of the content added is increased as before, so nothing is changed there, however now the content panel doesnt resize with the application form if it is for instance maximized by the user.

Author Comment

ID: 37820126
I just tried to fiddle around with parts of the forms

First I tried to remove all content containers and simply add the content usercontrol directly to the panel2 of the main splitcontainer, but the problem persisted; the width of the added usercontrol was increased and all its child controls where stretched too.

Next I tried to create a brand new Form and add the content usercontrol directly to that, and in that case there was NO problem, no size increase whatsoever, the code behaved as one would expect it to.

So I'm wondering if part of my project has become corrupt if thats feasible? Is there a way to completely regenerate the designer code files in Visual Studio to see if thats the case?

Hope someone can help me out with this one, its really getting on my nerves.

Accepted Solution

andreas_rafn earned 0 total points
ID: 37820966
With designer file corruption as my part of departure, I was able to solve the problem by:

1. Making a backup copy of the Main Form containing the content container panel
2. Deleting the main form
3. Reconstructing it from scratch in the designer (copying over individual controls from the backed up form)
4. Voila! problem solved

I guess it was a corruption issue of some kind (happened all the time in my VBA days)

Feel free to comment, and if possible provide solutions to manage this kind of problem in an easier way (if it is possible)

Author Closing Comment

ID: 37875097
I solved the probem myself, as it was caused by corruption and thus basically  impossible to solve for others having not experienced the same kind of corruption failure.

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question