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.C reateGraphics. If you use the latter,
remember to Dispose it when you have finished.
HTH
Charles
"Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.phx.gbl. ..[color=blue]
> 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[/color]
Charles Law wrote:[color=blue]
> 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.C reateGraphics. If you use the latter,
> remember to Dispose it when you have finished.
>
> HTH
>
> Charles
>
>
> "Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
> news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.phx.gbl. ..
>[color=green]
>>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[/color]
>
>
>[/color]
Thanks, I suspect programming in machine code was easier than trying to
find my way around the .net maze!
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.Measur eCharacterRange s
On Wed, 04 May 2005 10:48:37 +0200, Jack Russell <jackr@norubbis h.tpg.com.au> wrote:
[color=blue]
> Charles Law wrote:[color=green]
>> 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.C reateGraphics. If you use the latter,
>> remember to Dispose it when you have finished.
>>
>> HTH
>>
>> Charles
>>
>>
>> "Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>> news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.phx.gbl. ..
>>[color=darkred]
>>> 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[/color]
>>
>>
>>[/color]
> Thanks, I suspect programming in machine code was easier than trying to
> find my way around the .net maze!
>[/color]
Funnily enough, I had spotted that too, but assumed that
Graphics.Measur eCharacterRange s would give similar results. I will try it
myself. Is there any logic behind why MeasureString over estimates?
Charles
"Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
news:op.sp8yofx aklbvpo@stone.. .[color=blue]
> 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.Measur eCharacterRange s
>
>
> On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
> <jackr@norubbis h.tpg.com.au> wrote:
>[color=green]
>> Charles Law wrote:[color=darkred]
>>> 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.C reateGraphics. If you use the
>>> latter,
>>> remember to Dispose it when you have finished.
>>>
>>> HTH
>>>
>>> Charles
>>>
>>>
>>> "Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>>> news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.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
>>>
>>>
>>>[/color]
>> Thanks, I suspect programming in machine code was easier than trying to
>> find my way around the .net maze!
>>[/color]
>
>
>
> --
> Happy coding!
> Morten Wennevik [C# MVP][/color]
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 <blank@nowhere. com> wrote:
[color=blue]
> Hi Morten
>
> Funnily enough, I had spotted that too, but assumed that
> Graphics.Measur eCharacterRange s would give similar results. I will try it
> myself. Is there any logic behind why MeasureString over estimates?
>
> Charles
>
>
> "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
> news:op.sp8yofx aklbvpo@stone.. .[color=green]
>> 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.Measur eCharacterRange s
>>
>>
>> On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
>> <jackr@norubbis h.tpg.com.au> wrote:
>>[color=darkred]
>>> 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.C reateGraphics. If you use the
>>>> latter,
>>>> remember to Dispose it when you have finished.
>>>>
>>>> HTH
>>>>
>>>> Charles
>>>>
>>>>
>>>> "Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>>>> news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.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!
>>>[/color]
>>
>>
>>
>> --
>> Happy coding!
>> Morten Wennevik [C# MVP][/color]
>
>
>[/color]
Charles,
MeasureString depends heavily on the StringFormat being used. Any of
the overloads where you specify this and use an instance of
StringFormat.Ge nericTypographi c will work much better than the default
StringFormat. MeasureCharacte rRanges is more accurate though so use this if
you need to get exceedingly precise results. My reporting output works fine
with the GenericTypograp hic 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.publi c.dotnet.framew ork.drawing that you can find in a search.
Ron Allen
"Charles Law" <blank@nowhere. com> wrote in message
news:%235bYpvJU FHA.2444@TK2MSF TNGP10.phx.gbl. ..[color=blue]
> Hi Morten
>
> Funnily enough, I had spotted that too, but assumed that
> Graphics.Measur eCharacterRange s would give similar results. I will try it
> myself. Is there any logic behind why MeasureString over estimates?
>
> Charles
>
>
> "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
> news:op.sp8yofx aklbvpo@stone.. .[color=green]
>> 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.Measur eCharacterRange s
>>
>>
>> On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
>> <jackr@norubbis h.tpg.com.au> wrote:
>>[color=darkred]
>>> 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.C reateGraphics. If you use the
>>>> latter,
>>>> remember to Dispose it when you have finished.
>>>>
>>>> HTH
>>>>
>>>> Charles
>>>>
>>>>
>>>> "Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>>>> news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.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!
>>>[/color]
>>
>>
>>
>> --
>> Happy coding!
>> Morten Wennevik [C# MVP][/color]
>
>[/color]
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 MeasureCharacte rRanges, and put something together in VB.NET that
now does a pretty good job. I might have a play with GenericTypograp hic for
comparison.
Cheers
Charles
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:ueXD4uKUFH A.628@tk2msftng p13.phx.gbl...[color=blue]
> Charles,
> MeasureString depends heavily on the StringFormat being used. Any of
> the overloads where you specify this and use an instance of
> StringFormat.Ge nericTypographi c will work much better than the default
> StringFormat. MeasureCharacte rRanges is more accurate though so use this
> if you need to get exceedingly precise results. My reporting output works
> fine with the GenericTypograp hic 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.publi c.dotnet.framew ork.drawing that you can find in a search.
>
> Ron Allen
> "Charles Law" <blank@nowhere. com> wrote in message
> news:%235bYpvJU FHA.2444@TK2MSF TNGP10.phx.gbl. ..[color=green]
>> Hi Morten
>>
>> Funnily enough, I had spotted that too, but assumed that
>> Graphics.Measur eCharacterRange s would give similar results. I will try it
>> myself. Is there any logic behind why MeasureString over estimates?
>>
>> Charles
>>
>>
>> "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
>> news:op.sp8yofx aklbvpo@stone.. .[color=darkred]
>>> 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.Measur eCharacterRange s
>>>
>>>
>>> On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
>>> <jackr@norubbis h.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.C reateGraphics. If you use the
>>>>> latter,
>>>>> remember to Dispose it when you have finished.
>>>>>
>>>>> HTH
>>>>>
>>>>> Charles
>>>>>
>>>>>
>>>>> "Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>>>>> news:%23FZhUoHU FHA.3544@TK2MSF TNGP12.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][/color]
>>
>>[/color]
>
>[/color]
Charles Law wrote:[color=blue]
> 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 MeasureCharacte rRanges, and put something together in VB.NET that
> now does a pretty good job. I might have a play with GenericTypograp hic for
> comparison.
>
> Cheers
>
> Charles
>
>
> "Ron Allen" <rallen@_nospam _src-us.com> wrote in message
> news:ueXD4uKUFH A.628@tk2msftng p13.phx.gbl...
>[color=green]
>>Charles,
>> MeasureString depends heavily on the StringFormat being used. Any of
>>the overloads where you specify this and use an instance of
>>StringFormat. GenericTypograp hic will work much better than the default
>>StringForma t. MeasureCharacte rRanges is more accurate though so use this
>>if you need to get exceedingly precise results. My reporting output works
>>fine with the GenericTypograp hic 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.pub lic.dotnet.fram ework.drawing that you can find in a search.
>>
>>Ron Allen
>>"Charles Law" <blank@nowhere. com> wrote in message
>>news:%235bYpv JUFHA.2444@TK2M SFTNGP10.phx.gb l...
>>[color=darkred]
>>>Hi Morten
>>>
>>>Funnily enough, I had spotted that too, but assumed that
>>>Graphics.Mea sureCharacterRa nges would give similar results. I will try it
>>>myself. Is there any logic behind why MeasureString over estimates?
>>>
>>>Charles
>>>
>>>
>>>"Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
>>>news:op.sp8y ofxaklbvpo@ston e...
>>>
>>>>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.Measur eCharacterRange s
>>>>
>>>>
>>>>On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
>>>><jackr@noru bbish.tpg.com.a u> 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
>>>>>>graphic s
>>>>>>object. Otherwise use controlObject.C reateGraphics. If you use the
>>>>>>latter,
>>>>>>remembe r to Dispose it when you have finished.
>>>>>>
>>>>>>HTH
>>>>>>
>>>>>>Charles
>>>>>>
>>>>>>
>>>>>>"Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>>>>>>news:%23F ZhUoHUFHA.3544@ TK2MSFTNGP12.ph x.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]
>>>
>>>[/color]
>>[/color]
>
>[/color]
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.
Charles Law wrote:[color=blue]
> 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 MeasureCharacte rRanges, and put something together in VB.NET that
> now does a pretty good job. I might have a play with GenericTypograp hic for
> comparison.
>
> Cheers
>
> Charles
>
>
> "Ron Allen" <rallen@_nospam _src-us.com> wrote in message
> news:ueXD4uKUFH A.628@tk2msftng p13.phx.gbl...
>[color=green]
>>Charles,
>> MeasureString depends heavily on the StringFormat being used. Any of
>>the overloads where you specify this and use an instance of
>>StringFormat. GenericTypograp hic will work much better than the default
>>StringForma t. MeasureCharacte rRanges is more accurate though so use this
>>if you need to get exceedingly precise results. My reporting output works
>>fine with the GenericTypograp hic 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.pub lic.dotnet.fram ework.drawing that you can find in a search.
>>
>>Ron Allen
>>"Charles Law" <blank@nowhere. com> wrote in message
>>news:%235bYpv JUFHA.2444@TK2M SFTNGP10.phx.gb l...
>>[color=darkred]
>>>Hi Morten
>>>
>>>Funnily enough, I had spotted that too, but assumed that
>>>Graphics.Mea sureCharacterRa nges would give similar results. I will try it
>>>myself. Is there any logic behind why MeasureString over estimates?
>>>
>>>Charles
>>>
>>>
>>>"Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
>>>news:op.sp8y ofxaklbvpo@ston e...
>>>
>>>>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.Measur eCharacterRange s
>>>>
>>>>
>>>>On Wed, 04 May 2005 10:48:37 +0200, Jack Russell
>>>><jackr@noru bbish.tpg.com.a u> 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
>>>>>>graphic s
>>>>>>object. Otherwise use controlObject.C reateGraphics. If you use the
>>>>>>latter,
>>>>>>remembe r to Dispose it when you have finished.
>>>>>>
>>>>>>HTH
>>>>>>
>>>>>>Charles
>>>>>>
>>>>>>
>>>>>>"Jack Russell" <jackr@norubbis h.tpg.com.au> wrote in message
>>>>>>news:%23F ZhUoHUFHA.3544@ TK2MSFTNGP12.ph x.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]
>>>
>>>[/color]
>>[/color]
>
>[/color]
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.
Comment