Width of Text

  • Thread starter Thread starter Jack Russell
  • Start date Start date
J

Jack Russell

How can I find the width of some text (in pixels?) so that I can size
the width of the control to fit it as one line?

Thanks

Jack Russell
 
Hi Jack

The Graphics property has a MeasureString method that will do this. If you
are doing this in an event, such as Paint, you will be passed a graphics
object. Otherwise use controlObject.CreateGraphics. If you use the latter,
remember to Dispose it when you have finished.

HTH

Charles
 
Charles said:
Hi Jack

The Graphics property has a MeasureString method that will do this. If you
are doing this in an event, such as Paint, you will be passed a graphics
object. Otherwise use controlObject.CreateGraphics. If you use the latter,
remember to Dispose it when you have finished.

HTH

Charles
Thanks, I suspect programming in machine code was easier than trying to
find my way around the .net maze!
 
Jack,

Just so you know, MeasureString is simple, but will return a size slightly larger than needed for the text. In most cases this is ok. If you want exact measures you need to use Graphics.MeasureCharacterRanges
 
Hi Morten

Funnily enough, I had spotted that too, but assumed that
Graphics.MeasureCharacterRanges would give similar results. I will try it
myself. Is there any logic behind why MeasureString over estimates?

Charles
 
I don't remember exactly why MeasureString returns a size larger than the actual text, but I believe it has something to do with regular characters being able to increase/decrease in size depending on appendices like N vs Ñ, C vs Ç etc. I believe MeasureString assumes every character is displaying its largest possible version or something.


Hi Morten

Funnily enough, I had spotted that too, but assumed that
Graphics.MeasureCharacterRanges would give similar results. I will try it
myself. Is there any logic behind why MeasureString over estimates?

Charles
 
Charles,
MeasureString depends heavily on the StringFormat being used. Any of
the overloads where you specify this and use an instance of
StringFormat.GenericTypographic will work much better than the default
StringFormat. MeasureCharacterRanges is more accurate though so use this if
you need to get exceedingly precise results. My reporting output works fine
with the GenericTypographic StringFormat though if you are looking at a
print preview scaled down the alignment may look off. When scaled to over
100% these look just fine on the screen and look fine on the printer.

There have been a lot of long discussions on measuring strings in
microsoft.public.dotnet.framework.drawing that you can find in a search.

Ron Allen
Charles Law said:
Hi Morten

Funnily enough, I had spotted that too, but assumed that
Graphics.MeasureCharacterRanges would give similar results. I will try it
myself. Is there any logic behind why MeasureString over estimates?

Charles
 
Hi Ron

I wasn't using a StringFormat in my calls to MeasureString, so that could
indeed account for the effect that I was seeing. I have found some articles
on using MeasureCharacterRanges, and put something together in VB.NET that
now does a pretty good job. I might have a play with GenericTypographic for
comparison.

Cheers

Charles


Ron Allen said:
Charles,
MeasureString depends heavily on the StringFormat being used. Any of
the overloads where you specify this and use an instance of
StringFormat.GenericTypographic will work much better than the default
StringFormat. MeasureCharacterRanges is more accurate though so use this
if you need to get exceedingly precise results. My reporting output works
fine with the GenericTypographic StringFormat though if you are looking at
a print preview scaled down the alignment may look off. When scaled to
over 100% these look just fine on the screen and look fine on the printer.

There have been a lot of long discussions on measuring strings in
microsoft.public.dotnet.framework.drawing that you can find in a search.

Ron Allen
 
Charles said:
Hi Ron

I wasn't using a StringFormat in my calls to MeasureString, so that could
indeed account for the effect that I was seeing. I have found some articles
on using MeasureCharacterRanges, and put something together in VB.NET that
now does a pretty good job. I might have a play with GenericTypographic for
comparison.

Cheers

Charles
Thanks for all the replies.
Measure string worked for me. The only complication was that the control
I am using does not support that method so I have to set up a text box
measure that and then apply it to the target control. Much to my
amazement it seems to work although I suspect it may trip me up later.
 
Back
Top