Finding the width of a text

T

Tor Inge Rislaa

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
G

Guest

Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention?
 
J

Jay B. Harlow [MVP - Outlook]

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()
End Using
Hope this helps
Jay
 
R

Roger

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top