Hi everyone,
I have an application where I am building a form in code. The controls to
be placed on the form are determined by data so I don't know what they are in
advance. In addition, the maximum number of input characters is determined
in data as well.
My question is, If I know the control to be instantiated (textbox, combobox,
listbox) and I know the maximum size of an entry in characters, and I know
the font, font size, and style, how do I calculate the width to make the
control on the form?
Thanks.
BBM
Herfried K. Wagner [MVP] - 20 Feb 2005 16:11 GMT
"BBM" <bbm@bbmcompany.com> schrieb:
> My question is, If I know the control to be instantiated (textbox,
> combobox,
> listbox) and I know the maximum size of an entry in characters, and I know
> the font, font size, and style, how do I calculate the width to make the
> control on the form?
Some controls provide an 'AutoSize' property (Label, LinkLabel). You can
measure a string using
'Graphics.MeasureString'/'Graphics.MeasureCharacterRanges'.

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
BBM - 20 Feb 2005 17:13 GMT
That was fast!
Thanks, MeasureString should work nicely. For initially setting the control
width, what character would you recommend I use to fill my "sizing" string.
I would think that "Z" (capital z) would probably be the widest character in
almost every font. So if my input is 6 characters, I could size my control
to "ZZZZZZ", and add offsets as the next response suggested.
Any suggestions?
BBM
> "BBM" <bbm@bbmcompany.com> schrieb:
> > My question is, If I know the control to be instantiated (textbox,
[quoted text clipped - 6 lines]
> measure a string using
> 'Graphics.MeasureString'/'Graphics.MeasureCharacterRanges'.
Bob Powell [MVP] - 21 Feb 2005 08:11 GMT
"M" is traditionally the widest character in proportional fonts.

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> That was fast!
>
[quoted text clipped - 24 lines]
>> measure a string using
>> 'Graphics.MeasureString'/'Graphics.MeasureCharacterRanges'.
BBM - 22 Feb 2005 13:33 GMT
Hi Bob,
Thanks for the tip, and the links.
BBM
> "M" is traditionally the widest character in proportional fonts.
>
[quoted text clipped - 26 lines]
> >> measure a string using
> >> 'Graphics.MeasureString'/'Graphics.MeasureCharacterRanges'.
Tim Wilson - 20 Feb 2005 16:18 GMT
If the control has an AutoSize property, or something similar in nature,
then you might consider using that. If that is not possible, then you will
need to measure the longest string, using either Graphics.MeasureString() or
Graphics.MeasureCharacterRanges(), and set the width based on the result.
Depending on the border of the control, and possible offset space, you will
need to throw some extra pixels into the result as well. You can get the
border size using the SystemInformation class and the offset is usual a
small number of pixels (4, 5, 6). I'm not sure if there is a way, through
the framework or through the Win32 API, to get the offset for a control.

Signature
Tim Wilson
.Net Compact Framework MVP
> Hi everyone,
>
[quoted text clipped - 11 lines]
>
> BBM
BBM - 20 Feb 2005 17:15 GMT
Thanks for the fast response.
> If the control has an AutoSize property, or something similar in nature,
> then you might consider using that. If that is not possible, then you will
[quoted text clipped - 24 lines]
> >
> > BBM