Solved

User Control AutoSizeMode in Visual Studio 2008

Posted on 2010-11-28
20
1,190 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:stuengelman
  • 8
  • 4
  • 4
20 Comments
 
LVL 10

Expert Comment

by:james-ct16
ID: 34229583
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
 

Author Comment

by:stuengelman
ID: 34234385
Hi James,

Thank you.  Will test it out.

Stu
0
 

Author Comment

by:stuengelman
ID: 34251965
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
 
LVL 10

Expert Comment

by:james-ct16
ID: 34289137
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
 

Author Comment

by:stuengelman
ID: 34297276
Hi James,

Thanks.  Will check it out.

Stu
0
 

Author Comment

by:stuengelman
ID: 34489330
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
 
LVL 10

Expert Comment

by:james-ct16
ID: 34508842
0
 
LVL 10

Expert Comment

by:james-ct16
ID: 34508882
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 13

Expert Comment

by:Corey2
ID: 34512664
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
 

Author Comment

by:stuengelman
ID: 34513216
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
 
LVL 13

Expert Comment

by:Corey2
ID: 34513421
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
 

Author Comment

by:stuengelman
ID: 34513453
Hi Corey,

Thanks, will try it.

Stu
0
 
LVL 13

Expert Comment

by:Corey2
ID: 35194995
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
 

Author Comment

by:stuengelman
ID: 35195581
OK, thank you Corey.
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 35223453
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
 

Author Comment

by:stuengelman
ID: 35229008
Corey2 - Thank you, I'll test it out.

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

12 Experts available now in Live!

Get 1:1 Help Now