Link to home
Start Free TrialLog in
Avatar of stuengelman
stuengelmanFlag for United States of America

asked on

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
Avatar of james-ct16
james-ct16
Flag of Australia image

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
Avatar of stuengelman

ASKER

Hi James,

Thank you.  Will test it out.

Stu
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
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
Hi James,

Thanks.  Will check it out.

Stu
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
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

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)
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?
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.
Hi Corey,

Thanks, will try it.

Stu
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
OK, thank you Corey.
ASKER CERTIFIED SOLUTION
Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Corey2 - Thank you, I'll test it out.

Thermoduric - Please simply leave the question open until I close it.