Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw a font at different sizes in VB .NET
Keywords.NET, VB.NET, font, text, size, font size
CategoriesVB.NET, Graphics
 
When the program receives a Paint event, the code clears the graphics context. It then loops over a series of font sizes. For each size, it creates a new Font object at that size. It uses the object's Height property to see if we have room to fit the next line of text on the form. If we are too close to the bottom of the form, the code uses the graphics context's MeasureString method to see how wide the string WWWW is and adds that amount to the next string's X coordinate.

Next the routine uses the graphics context's DrawString method to draw the text using the new font and a black brush. It finishes by increasing the Y coordinate for the next line of text.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    Dim gr As Graphics
    Dim font_size As Single
    Dim X As Single
    Dim Y As Single
    Dim new_font As Font

    gr = e.Graphics
    gr.Clear(BackColor)
    X = 0
    Y = 0
    For font_size = 9.75 To 30 Step 0.75
        ' Get the next font.
        new_font = New Font("Times New Roman", font_size)

        ' See if we have room.
        If Y + new_font.Height > Me.ClientSize.Height Then
            ' Start a new column.
            Y = 0
            X += gr.MeasureString("WWWW", new_font).Width
        End If

        ' Draw the text.
        gr.DrawString( _
            font_size.ToString, _
            new_font, _
            Brushes.Black, _
            X, Y)
        Y += new_font.Height
    Next font_size
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated