473,508 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4508
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
8085
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
18805
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
3934
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
15034
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
6693
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
2260
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
309
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
4823
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
2973
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...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.