Drawing Strings: the Font Class

Another useful class is the Font. You can set a font two ways:


Example 1: Use the properties of the form

In the design window, select a Font and ForeColor for the form. Write the code to draw the words when you click:

Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
   Dim MyBrush As New SolidBrush(Me.ForeColor)
   Me.CreateGraphics.DrawString("Hello", Me.Font, MyBrush, e.X, e.Y)
End Sub 'Form1_MouseClick

Example 2: Declare the values in the code

Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
   Dim MyBrush As New SolidBrush(Color.Red)
   Dim MyFont As New Font("Courier New", 12, FontStyle.Bold)
   Me.CreateGraphics.DrawString("Hello", MyFont, MyBrush, e.X, e.Y)
End Sub 'Form1_MouseClick
Experiment: Duplicate the program shown in the illustration.
INDEX, Draw a circle, The Brush, Drawing rectangles, Arc and Pie, Array of Points, Drawing Strings: The Font Class, Numbered Dots Project, Numbered Dots Solution
Next lesson: Procedures in Visual Basic

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Drawing Strings: The Font Class