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

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
andreas_rafnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nepaluzCommented:
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).
nishant joshiTechnology Development ConsultantCommented:
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.

Regards,
nishant
andreas_rafnAuthor Commented:
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):
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

nepaluzCommented:
Do NOT dock.fill the usercontrol, just add it to the panel (and may-be dock.top)
andreas_rafnAuthor Commented:
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.
CodeCruiserCommented:
Just as a test, still same problem if you change Dock of content panel to none?
andreas_rafnAuthor Commented:
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.
andreas_rafnAuthor Commented:
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.
andreas_rafnAuthor Commented:
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)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andreas_rafnAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.