467,077 Members | 1,021 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Finding the width of a text

Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Width according to the actual width of the text. It can also be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa
Nov 21 '05 #1
  • viewed: 1206
Share:
8 Replies
me.CreateGraphics.MeasureString(txtMyTextBox.Text, me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no*************@rislaa.no> wrote in message
news:Ua*******************@news2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Width according to the actual width of the text. It can also
be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa

Nov 21 '05 #2
Thank you, works OK!

TIRislaa

"Eric Dreksler" <ericd AT accessoneinc DOT com> skrev i melding
news:uG**************@TK2MSFTNGP12.phx.gbl...
me.CreateGraphics.MeasureString(txtMyTextBox.Text, me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no*************@rislaa.no> wrote in message
news:Ua*******************@news2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Width according to the actual width of the text. It can also be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa


Nov 21 '05 #3
Eric & TIRislaa,
Remember to be very certain to Dispose of the Graphics object that
CreateGraphics return, other wise you can have a serious resource leak &
your program could stop working...

Dim gr As Graphics = Me.CreateGraphics()
Dim sz As Size
Try
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
Finally
gr.Dispose()
End Try

VS.NET 2005 (aka Whidbey due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ will include a Using statement to
simplify the above:

Dim sz As Size
Using gr As Graphics = Me.CreateGraphics()
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
End Using

Hope this helps
Jay

"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
me.CreateGraphics.MeasureString(txtMyTextBox.Text, me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no*************@rislaa.no> wrote in message
news:Ua*******************@news2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Width according to the actual width of the text. It can also
be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa


Nov 21 '05 #4
Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention?

"Jay B. Harlow [MVP - Outlook]" wrote:
Eric & TIRislaa,
Remember to be very certain to Dispose of the Graphics object that
CreateGraphics return, other wise you can have a serious resource leak &
your program could stop working...

Dim gr As Graphics = Me.CreateGraphics()
Dim sz As Size
Try
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
Finally
gr.Dispose()
End Try

VS.NET 2005 (aka Whidbey due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ will include a Using statement to
simplify the above:

Dim sz As Size
Using gr As Graphics = Me.CreateGraphics()
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
End Using

Hope this helps
Jay

"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
me.CreateGraphics.MeasureString(txtMyTextBox.Text, me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no*************@rislaa.no> wrote in message
news:Ua*******************@news2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Width according to the actual width of the text. It can also
be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa



Nov 21 '05 #5
Dennis,
Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention? I suspect you meant "without user intervention".
The Using statement does the Dispose for you. It allows for "cleaner" code,
most of the time "cleaner" code is simpler code.

It is effectively the Try Finally statement, with a check for the variable
being Nothing. My sample really should have checked for Nothing, however due
to the way it was written, it was "not necessary" as the Try would not be
entered if the "Me.CreateGraphics" throws an exception, hence I left it
off... I'm not sure (nor am I worried) if the End Using eliminates the check
for Nothing, when it deems it unnecessary... Including it is necessary to
avoid NullReferenceExceptions on the call to Dispose.
If you look at the IL for a Using statement you would see something very
similar to:

Using gr As Graphics = Me.CreateGraphics()
Dim gr As Graphics = Me.CreateGraphics()
Try
End Using Finally
If Not gr Is Nothing Then
gr.Dispose()
End If
End Try


Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com... Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention?

"Jay B. Harlow [MVP - Outlook]" wrote:
Eric & TIRislaa,
Remember to be very certain to Dispose of the Graphics object that
CreateGraphics return, other wise you can have a serious resource leak &
your program could stop working...

Dim gr As Graphics = Me.CreateGraphics()
Dim sz As Size
Try
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
Finally
gr.Dispose()
End Try

VS.NET 2005 (aka Whidbey due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ will include a Using statement to
simplify the above:

Dim sz As Size
Using gr As Graphics = Me.CreateGraphics()
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
End Using

Hope this helps
Jay

"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
> me.CreateGraphics.MeasureString(txtMyTextBox.Text, me.Font)
>
> Eric Dreksler
>
> "Tor Inge Rislaa" <no*************@rislaa.no> wrote in message
> news:Ua*******************@news2.e.nsc.no...
>> Finding the width of a text
>>
>> I need to find the width of a text. When the user change the font in a
>> textbox I want the textbox to fit the text automatically by changing
>> txtMyTextBox.Width according to the actual width of the text. It can
>> also
>> be
>> useful to know the actual height when using multiline textbox. Is this
>> possible?
>>
>> TIRislaa
>>
>>
>
>


Nov 21 '05 #6
> Dim gr As Graphics = Me.CreateGraphics()
Dim sz As Size
Try
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
Finally
gr.Dispose()
End Try


Ok, how to know if it is managed or not,
that is Framework will garbage collect,
or it must be disposed in code manually?
Thanks,
Roger

Nov 21 '05 #7
Roger,
Generally when using a type for the first time I read the documentation for
that type to see if there are "special" requirements such as calling Dispose
on it.

A good general (very general) rule of thumb to follow is if it implements
IDisposable, call Dispose. The "problem" with this rule is if you inherit
from System.ComponentModel.Component, then you have implemented IDisposable
& Dispose may or may not be needed. Of course calling Dispose when it is not
needed is "better" then not calling it when it is needed!!

Note: implementing IComponent directly, rather then inheriting from
Component would allow you to exclude the IDisposable itself!

Hope this helps
Jay
"Roger" <ro***@pcsrevenuecontrol.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Dim gr As Graphics = Me.CreateGraphics()
Dim sz As Size
Try
sz = gr.MeasureString(txtMyTextBox.Text,me.Font)
Finally
gr.Dispose()
End Try


Ok, how to know if it is managed or not,
that is Framework will garbage collect,
or it must be disposed in code manually?
Thanks,
Roger


Nov 21 '05 #8
> Generally when using a type for the first time I read the documentation
for
Rule # 1.
Hope this helps
Jay


Thanks, Jay, it was.
Roger
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Noam Dekers | last post: by
5 posts views Thread by Matthew Robinson | last post: by
10 posts views Thread by tshad | last post: by
2 posts views Thread by Oenone | last post: by
15 posts views Thread by rhino | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.