473,395 Members | 1,870 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,395 software developers and data experts.

Width of Text

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
Nov 22 '05 #1
8 3250
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
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 22 '05 #2
Charles Law wrote:
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
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Thanks, I suspect programming in machine code was easier than trying to
find my way around the .net maze!
Nov 22 '05 #3
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
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell <ja***@norubbish.tpg.com.au> wrote:
Charles Law wrote:
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
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


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


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 22 '05 #4
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
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sp8yofxaklbvpo@stone...
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
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
<ja***@norubbish.tpg.com.au> wrote:
Charles Law wrote:
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
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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

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


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 22 '05 #5
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.
On Wed, 04 May 2005 13:24:55 +0200, Charles Law <bl***@nowhere.com> wrote:
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
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sp8yofxaklbvpo@stone...
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
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
<ja***@norubbish.tpg.com.au> wrote:
Charles Law wrote:
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
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

> 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

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


--
Happy coding!
Morten Wennevik [C# MVP]



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 22 '05 #6
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sp8yofxaklbvpo@stone...
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
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
<ja***@norubbish.tpg.com.au> wrote:
Charles Law wrote:
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
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

> 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

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


--
Happy coding!
Morten Wennevik [C# MVP]


Nov 22 '05 #7
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" <rallen@_nospam_src-us.com> wrote in message
news:ue*************@tk2msftngp13.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sp8yofxaklbvpo@stone...
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
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
<ja***@norubbish.tpg.com.au> wrote:

Charles Law wrote:
> 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
>
>
> "Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
> news:%2****************@TK2MSFTNGP12.phx.gbl...
>
>> 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
>
>
>
Thanks, I suspect programming in machine code was easier than trying to
find my way around the .net maze!


--
Happy coding!
Morten Wennevik [C# MVP]



Nov 22 '05 #8
Charles Law wrote:
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" <rallen@_nospam_src-us.com> wrote in message
news:ue*************@tk2msftngp13.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl.. .
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
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sp8yofxaklbvpo@stone...

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
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
<ja***@norubbish.tpg.com.au> wrote:
>Charles Law wrote:
>
>>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
>>
>>
>>"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
>>news:%2****************@TK2MSFTNGP12.phx.gbl ...
>>
>>
>>>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
>>
>>
>>
>Thanks, I suspect programming in machine code was easier than trying to
>find my way around the .net maze!
>

--
Happy coding!
Morten Wennevik [C# MVP]


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.
Nov 22 '05 #9

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

Similar topics

2
by: Alex Shi | last post by:
In php, is there a way obtain the width of a charactor of a certain font? Alex -- ================================================== Cell Phone Batteries at 30-50%+ off retail prices!...
2
by: Kai Grossjohann | last post by:
I would like to put a text input field (in the sense of <input type="text">) and an image next to each other, where I know the size in pixels of the image, and I know the total width in em. I...
8
by: BiNZGi | last post by:
Hi I have reduced the problem to this code: <form> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td><input type="text" style="width: 100%;" value="Lorem ipsum dolor...
17
by: Piers Lawson | last post by:
If the following is displayed in IE6 or FireFox, the text box forces the second DIV to expand beyond its 15em width. Is there a way to have the text box fit within the DIV (preferably without...
1
by: AndrewF | last post by:
Hi there, I have racked my brains for days on this and can't seem to find a solution. I have a form which is dynamically creating some text boxes on it. The text boxes are single line ones. ...
1
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
31
by: Sarita | last post by:
Hello, this might sound stupid, but I got a really nice homepage template which unfortunately is a 3-Column Fixed Width CSS format. Now I don't have any content for the right column and would...
2
by: xtian | last post by:
Hi, I have been having some trouble with getting a column to be a fixed width. I have a table with 11 columns, so it doesn't all fit in a normal browser window. One of the cells has a "?" and then...
1
pradeepjain
by: pradeepjain | last post by:
I have a 3 column layout left<->content<->right<->.I made a css and html in such a manner that when there is no left sidebar the content region starts from left side and takes whole width of left on...
10
by: magnesium | last post by:
I have this under-construction page, If you try and select the link coming from the bottom with your mouse, you'll notice, that before the actual link is selected, the td background will change. you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.