473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Automatic resizing of controls in group box

MB
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

Sep 27 '05 #1
1 4496
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

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"MB" <ma******@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
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

Sep 27 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Weng kin | last post by:
Hi everyone, I have a problem trying to load an image without the image resizing itself to fit into the window. My simple javascript is as follows: function viewImage(filename) { features =...
11
by: Jozef | last post by:
I have some old code that I use from the Access 95 Developers handbook. The code works very well, with the exception that it doesn't seem to recognize wide screens, and sizes tab controls so that...
2
by: Vaughn | last post by:
Where can I get information on automatic resizing of controls when the form changes size (eg. if I dynamically increase the size of my form, the listview inside should automatically increase in...
8
by: Chris | last post by:
Hi, In design mode I built some windows with some controls (e.g. listboxes, labels, chgeck boxes etc.) in it, and I did set the property for the window size is set to normal. Now, when I run...
12
by: Søren Reinke | last post by:
Hi there I have a little problem. How do i make sure that a graph is not redrawn while the form with the graph is being resized ? I have tried to add a mouse up/down event handler on the...
3
by: tangokilo | last post by:
Hi, I believe my questions is a beginners questions and I appreciate your help. I create controls (mostly in a group box). When user maximizes the form I want to adjust the sizes of these...
1
by: MB | last post by:
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...
4
by: Rob | last post by:
If you allow the user to Maximize a form, is there a way to automatically enlarge each control on the form in the same proportion ?
2
by: necro1000 | last post by:
Hi all, I am using an MDI Container for the creation of my app but I need to automatically resize my controls according to the size of the child form. If I set the Anchor properties of all the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.