Font Size vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • truezplaya
    New Member
    • Jul 2007
    • 115

    Font Size vb.net

    Hello

    I am currently developing an application that displays information to users. I have everything working but one tiny thing thats bugging me. I create a font as below
    Code:
    Dim[ myfont AsNew Font(["Arial", size, System.Drawing.FontStyle.Regular)
    I use g.measurestring to measure a string and if it exceeds limits I set the size of the font to a smaller font in a loop until the string fits the limits.
    My problem is that when i set my size value to a lower value, myfont does not recognise this, Do i have to dispose of the font and then recreate it for the value of size to be recognised.

    I thought it would be as easy as setting font1.size = value but i was wrong! any help would be welcomed with open arms :)

    cheers
    truez
    Last edited by Frinavale; Jan 12 '09, 02:44 PM. Reason: cleaned tags
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    Font class is immutable in .Net, meaning you cannot change its properties once it is instantiated. It also implements IDisposable, meaning that you must dispose it manually to ensure garbage collection as soon as possible.

    In other words, whenever you need a font with a different size, you will have to create a new instance and then dispose it after use:

    Code:
    Using font As New Font("Tahoma", 1.0F, FontStyle.Regular)
        ' do something with font
    End Using ' this will dispose the font

    Comment

    • truezplaya
      New Member
      • Jul 2007
      • 115

      #3
      ok so i would be looking at something like this
      Code:
      Dim wob As SizeF = g.MeasureString(scheduleResults(ScreenNumber).Item(1).ToString, font1)]while wob.width > me.width
      size - = 5 
      end while 
      'dispose of my old font create my new font here
      My only problem with doing this is knowing which font to put in this line of code
      Code:
      g.DrawString(scheduleResults(Screen).Item(2).ToString, font1, Brushes.Red, Me.Width * 0.7F, y)
      Last edited by Frinavale; Jan 12 '09, 02:46 PM. Reason: cleaned and fixed tags

      Comment

      • vekipeki
        Recognized Expert New Member
        • Nov 2007
        • 229

        #4
        Please fix your CODE tags, your source is really hard to read this way.

        I am not sure what you are trying to do. If you want to resize a font to fit your form you must call g.MeasureString several times for different font sizes, until the text fits the control:

        Code:
                Dim fontSize As Single = 12.0F
                Dim measuredWidth As Single
        
                Do
                    Using font As New Font("Tahoma", fontSize, FontStyle.Regular)
                        measuredWidth = g.MeasureString("text", font).Width
                    End Using
                    If (measuredWidth > Me.Width) Then
                        fontSize = fontSize - 0.1
                    End If
                Loop While (measuredWidth > Me.Width)
        
                Using font As New Font("Tahoma", fontSize, FontStyle.Regular)
                    ' do something
                End Using

        Comment

        • truezplaya
          New Member
          • Jul 2007
          • 115

          #5
          :) cheers thats got me back on track

          Comment

          Working...