Re: Finding the width of a text
From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 02/18/05
- Next message: Herfried K. Wagner [MVP]: "Re: Window Registry"
- Previous message: Jay B. Harlow [MVP - Outlook]: "Re: Thread.Sleep"
- In reply to: Dennis: "Re: Finding the width of a text"
- Next in thread: Roger: "Re: Finding the width of a text"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 17 Feb 2005 20:34:23 -0600
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" <Dennis@discussions.microsoft.com> wrote in message
news:E0252EF6-5CE6-4022-BE35-B1E154B6367C@microsoft.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:uGtpMETFFHA.3376@TK2MSFTNGP12.phx.gbl...
>> > me.CreateGraphics.MeasureString(txtMyTextBox.Text,me.Font)
>> >
>> > Eric Dreksler
>> >
>> > "Tor Inge Rislaa" <nospam.tor.inge@rislaa.no> wrote in message
>> > news:Ua7Rd.9398$IW4.224134@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
>> >>
>> >>
>> >
>> >
>>
>>
>>
- Next message: Herfried K. Wagner [MVP]: "Re: Window Registry"
- Previous message: Jay B. Harlow [MVP - Outlook]: "Re: Thread.Sleep"
- In reply to: Dennis: "Re: Finding the width of a text"
- Next in thread: Roger: "Re: Finding the width of a text"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|