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
 
 
 
 
 
TitleCreate fonts easily and interactively by using the Font constructor that uses a prototype in Visual Basic .NET
DescriptionThis example shows how to create fonts easily and interactively by using the Font constructor that uses a prototype in Visual Basic .NET.
KeywordsFont, create font, font prototype, VB.NET
CategoriesGraphics, VB.NET
 
Thanks to Gary Winey.

The Font class provides a constructor that takes only two parameters: an existing font to use as a prototype and a set of font styles (bold, italic, etc.). This is the easiest way to create a new font.

This example displays check boxes to let the user specify the font style. When a box's value changes, the code uses the simple constructor to make an example font and sets a Label control's Font property to the result.

 
Private m_FontStyle As FontStyle

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    m_FontStyle = Me.lblSample.Font.Style
End Sub

Private Sub chkBold_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    chkBold.CheckedChanged
    If Me.chkBold.Checked Then
        ' Turn on Bold
        m_FontStyle = m_FontStyle Or FontStyle.Bold
    Else
        ' Turn off Bold
        m_FontStyle = m_FontStyle And Not FontStyle.Bold
    End If
    lblSample.Font = New Font(Me.lblSample.Font, _
        m_FontStyle)
End Sub

Private Sub chkItalic_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    chkItalic.CheckedChanged
    If Me.chkItalic.Checked Then
        ' Turn on Italic
        m_FontStyle = m_FontStyle Or FontStyle.Italic
    Else
        ' Turn off Italic
        m_FontStyle = m_FontStyle And Not FontStyle.Italic
    End If
    lblSample.Font = New Font(Me.lblSample.Font, _
        m_FontStyle)
End Sub

Private Sub chkStrikeout_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    chkStrikeout.CheckedChanged
    If Me.chkStrikeout.Checked Then
        ' Turn on Strikeout
        m_FontStyle = m_FontStyle Or FontStyle.Strikeout
    Else
        ' Turn off Strikeout
        m_FontStyle = m_FontStyle And Not _
            FontStyle.Strikeout
    End If
    lblSample.Font = New Font(Me.lblSample.Font, _
        m_FontStyle)
End Sub

Private Sub chkUnderline_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    chkUnderline.CheckedChanged
    If Me.chkUnderline.Checked Then
        ' Turn on Underline
        m_FontStyle = m_FontStyle Or FontStyle.Underline
    Else
        ' Turn off Underline
        m_FontStyle = m_FontStyle And Not _
            FontStyle.Underline
    End If
    lblSample.Font = New Font(Me.lblSample.Font, _
        m_FontStyle)
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated