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
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
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):
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

627 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