User Control AutoSizeMode in Visual Studio 2008

Hello,

I am building a Windows Application using Visual Studio 2008 in conjunction with VB.NET.

My application makes use of a custom User Control.  The User Control has several label controls across the top.  I have set the AutoSizeMode of the User Control to GrowAndShrink.  My questions are:

(1) What operation(s) do I have to perform on the labels in order to remove them for the purposes of auto-shrinking the size of the rendered control?  E.g., would labelname.Dispose() do it, or is something else required?

(2) After removing the labels, is it necessary to issue any explicit command to get the User Control to shrink, or does the User Control do this automatically as embedded base controls (the labels) are removed?

Thanks, Stu Engelman
stuengelmanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Corey ScheichConnect With a Mentor DeveloperCommented:
Sorry it has been a few days I have attached an example of an autosize control

The control has a listbox that fills with the labels that are listed on the control and a button that will remove the selected label in the listbox. For visibility I added a border to the control so you can visibly see the control shrink when it is on a parent form.  I also included a parent form with it already on it. Just create a new windows forms application and replace Form1 with the form attached and add the control to the project and run it to test the example.  I wrote it in vs2008 so let me know if you would like the solution.
AutoSizeExample.zip
0
 
james-ct16Commented:
Howdy
I believe this is the solution you are looking for.
On the user control add a flow layout panel and set
AutoSize = true
AutoSizeMode = GrowAndShrink
Add your labels to the auto panel
On the User Control set
AutoSize = true
AutoSizeMode = GrowAndShrink

Then to remove the labels from the display simply do [labelname].Visible = false / true When the visibility is toggled the control will resize itself without intervention

Hope that helps

James
0
 
stuengelmanAuthor Commented:
Hi James,

Thank you.  Will test it out.

Stu
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
stuengelmanAuthor Commented:
Hi James,

A few comments and questions.

The UserControl is set up at design time with AutoSize = True and AutoSizeMode = GrowAndShrink.

I'm confused about why the FlowLayoutPanel is needed.  The labels are already on the UserControl, so it would seem logical that removing them would cause the UserControl to resize automatically as long as the labels are properly removed.  I've tried the Hide and Dispose methods without success.  Maybe as you suggest setting the Visible propery to False would do the trick, although I thought that's what the Hide method does.

Another question is whether any method needs to be run on the UserControl after performing the required operations on the labels.  I'm not sure what it might be, but possibilities include Refresh, PerformAutoScale, and Update.

Will await your comments.

Thanks, Stu
0
 
james-ct16Commented:
Howdy,
sorry for the delay in getting back to you, I am using the flow layout so when the visibility is toggled the controls that are visible shift to the left automatically. I got the impression this was what you were trying to do. If you just toggle the visibility in a container that has absolute positioning then the positioning of all visible controls will not change.
If you are toggling visibility then you should not have to perform any additional actions.
You may be interesting this article while it does not deal with toggling visibility of controls it is a nice intro to managing form layouts http://www.nezperce.com/~joe/matt/program/vb/Tutorials/pflvb7/index.html
Hope that helps
James
0
 
stuengelmanAuthor Commented:
Hi James,

Thanks.  Will check it out.

Stu
0
 
stuengelmanAuthor Commented:
Hi James,

Please excuse my long absence.  I got pulled into another project, and then the holidays came up.

The link doesn't really address my issue.

The core issue is: how do I sufficiently dispose of the labels so that my UserControl will automatically resize when I set AutoSize = True and AutoSizeMode = GrowAndShrink?

Either I'm doing something wrong, or this functionality does not work in VS 2008 for UserControls.

Thanks, Stu
0
 
james-ct16Commented:
0
 
james-ct16Commented:
Sorry i didnt mean to post just that

Part of the problem is that Panel on its own does perform any action on the setting of auto size, if you look at the msdn doco for it you will see a note that says 'This property is not relevant for this class.' The AutoSizeMode informs controls like the flow layout how to size controls.

The flow layout panel (http://msdn.microsoft.com/en-us/library/f9e8s203.aspx) is one of the controls that performs the autosize you are looking for. Or at least i think you are looking for.

In the demo posted above I have created to user controls contained in group boxes. the button beside the group box will toggle the visibility for one of the controls. Each control has a blue background so you can see how the resize works. You can equally change the ToggleButtonVisibility method in each class to change the text and you will note that as the text gets longer the flow layout changes size, the whole control gets wider which the panel starts clipping the control.

A note on some of the other things you have tried
.Dispose() will not work, it is there to close or release unmanaged resources and is simply not applicable, think of files, streams
.PerformAutoScale, this call may work under the following scenario, say you have been manipulating the controls within the flow layout and suppressed the raising of layout events then you may want to call it.

I hope that helps a little bit more

0
 
Corey ScheichDeveloperCommented:
To properly remove a control from a parent panel all you should need to do is call ParentPanelControl.Controls.Remove(ControlObjToRemove) or ParentPanelControl.Controls.RemoveAt(ControlObjToRemoveIndex)
0
 
stuengelmanAuthor Commented:
James - are you saying that making labels invisible within a UserControl, and then setting AutoSize = True and AutoSizeMode = GrowAndShrink, will NOT cause the UserControl to resize?  I.e., the resizing must be done to a FlowLayoutPanel instead?  I.e., remove the labels from the UserControl, and place them along with the UserControl(s) in the FlowLayoutPanel, and then toggle the visibility of the labels?

Corey - Is "ParentPanelControl" the correct object to use when the parent control for the labels is a UserControl?
0
 
Corey ScheichDeveloperCommented:
Not exactly.

Substitute the name of your user control or if the code is within your user control class you can use Me.Controls.Remove instead.
0
 
stuengelmanAuthor Commented:
Hi Corey,

Thanks, will try it.

Stu
0
 
Corey ScheichDeveloperCommented:
Stu,

You can't delete just because the last suggestion didn't work when your last response was I will try it and you posted nothing about the results.  I am going to create a simple example for you.

Corey
0
 
stuengelmanAuthor Commented:
OK, thank you Corey.
0
 
stuengelmanAuthor Commented:
Corey2 - Thank you, I'll test it out.

Thermoduric - Please simply leave the question open until I close it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.