Re: Finding the width of a text

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 02/18/05


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
>> >>
>> >>
>> >
>> >
>>
>>
>>



Relevant Pages

  • Re: Finding the width of a text
    ... Do you then have to still dispose gr or has M'soft figured out yet how to ... dispose of objects with user intervention? ... > Dim gr As Graphics = Me.CreateGraphics ... > Dim sz As Size ...
    (microsoft.public.dotnet.languages.vb)
  • Re: to dispose or not ?
    ... If someone calls a custom method Dispose and even bether is filling the ... As said before by me and others the Idisposable interface is a contract, ... Dim intCount As Integer ... Dim entry As New DirectoryServices.DirectoryEntry ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB 2008 and 2005, Image object problem.
    ... Dispose has nothing to do with finalizing, you can do it at any moment, what is aviced for things like pens and modal forms. ... However to use dispose were it not needed is for me the same as by some called best practise to set every string to nothing before a program closes. ... Dim result As Boolean = False ... Dim compressionValue As Int16 = 0 ...
    (microsoft.public.vb.general.discussion)
  • Re: How do I complete this?
    ... interface (has a dispose method)). ... Dim connection as new OleDbConnection ... these two objects and will call their Finalize methods. ... Dim conn as new OleDbConnection ...
    (microsoft.public.dotnet.languages.vb)
  • Re: to dispose or not ?
    ... You never see me writing that the dispose method should not be invoked, especially by Drawing, Sharepoint it is know that it uses many resources. ... Dim intCount As Integer ... Dim entry As New DirectoryServices.DirectoryEntry ...
    (microsoft.public.dotnet.languages.vb)