472,353 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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
8 1342
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm...
5
by: Matthew Robinson | last post by:
Can anybody see what is wrong with this code? apparently i have an 'empty delimiter' on the line that sets $pos - can anybody see what is wrong with...
2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent...
1
by: Doug | last post by:
The html below shows DataList "DiscountList" nested within DataList "EventItemList". DiscountList contains a Label control. I'm trying to find...
4
by: News | last post by:
I have a page with many controls. Among these controls there is a table which is a datagrid with nested repeater inside. My problem is that I can...
10
by: tshad | last post by:
I have a Datagrid with a column: <asp:HyperLinkColumn DataTextField="JobTitle" DataNavigateUrlField="PositionID"...
2
by: Oenone | last post by:
I'm trying to create a form similar to a VB messagebox but with custom content. One of the things I want to be able to do is display some text in a...
1
by: hyperian | last post by:
Hey, I've hit a problem and haven't had much luck in finding a solution. I have DIV tags that enclose text of variable length. This text is...
15
by: rhino | last post by:
I've put together a prototype of two-tiered CSS tabs that works really well in IE6, IE7, and FF2. It also works very well in Opera 9.27 _except_...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.