Solved

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

Posted on 2012-04-07
10
320 Views
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
0
Comment
Question by:andreas_rafn
10 Comments
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
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).
0
 
LVL 14

Expert Comment

by:nishant joshi
Comment Utility
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
0
 

Author Comment

by:andreas_rafn
Comment Utility
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):
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
Do NOT dock.fill the usercontrol, just add it to the panel (and may-be dock.top)
0
 

Author Comment

by:andreas_rafn
Comment Utility
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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Just as a test, still same problem if you change Dock of content panel to none?
0
 

Author Comment

by:andreas_rafn
Comment Utility
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.
0
 

Author Comment

by:andreas_rafn
Comment Utility
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.
0
 

Accepted Solution

by:
andreas_rafn earned 0 total points
Comment Utility
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)
0
 

Author Closing Comment

by:andreas_rafn
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now