Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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):
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

LVL 17

Expert Comment

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb message driven bean mdb creation steps 2 16
Syntax Issue with SSIS module 26 101
how to just get time from a date 6 32
AvlTree-Node Data type 4 10
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

808 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