Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / September 2005

Tip: Looking for answers? Try searching our database.

Automatic resizing of controls in group box

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MB - 27 Sep 2005 10:21 GMT
Hi!

I am trying to make a dynamic GroupBox that will contain a variable
number of child controls of a single type, e.g. RadioButtons or
CheckBoxes. I want to read the neccesarry information about the
controls (such as the text) from an xml-file, create controls, and then
place them on top of another in the group box. The problem is how to
give the controls and the group box the correct size. The group box
(and its child controls) should have a set width, but the height should
resize based on the child controls. Also, I need the child controls to
adjust their height based on the Text property.

Any ideas on how to do this? What I need is some way of making e.g. a
RadioButton adjust it height (but have constant width) to fit the text
it displays. I have tried with the Label control, and have set the
AutoSize property to true, but it will only resize the width. I managed
to get the Label to adjust correctly by splitting the text in words,
adding word by word, and adding a "\n" when the PreferredWidth property
was larger than the width I wanted, and at the end reading the
PreferredHeight, but the RadioButton and CheckBox doesn't seem to have
this property. Any other suggestions?

MB
Dave - 27 Sep 2005 17:28 GMT
Get a Graphics object for the control that needs to be resized.  (This is provided in the paint method, but you may need to access
it using CreateGraphics in the TextChanged event: (sender as Control).CreateGraphics())

The Graphics object has a method called MeasureString which can be used to obtain the height of the text that will be painted on the
control.  Specify the text from the Text property of the control and a StringFormat instance that is appropriate.

Set the height of the control using the result from the MeasureString object.

FYI, in my experience it seems this is never a perfect result.  Usually, I have to pad the result from MeasureString with a constant
number of pixels.  If the font style changes, this "constant" value may need to change also.  I think this occurs since Text is
rendered in a device-independant manner in the framework.  Just pick a value that will suit most, if not all, font styles such as 5
px.  The height may not be as tight as you want it, but it won't clip any text at the bottom.

Also, don't forget to account for the non-client area of the control.  For instance, if you have a two pixel border around the
control add another four pixels to the height returned by MeasureString.

GL

Signature

Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------

> Hi!
>
[quoted text clipped - 19 lines]
>
> MB

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.