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
 
 
 
 
 
 
TitleDisplay samples of fonts in VB .NET
DescriptionThis example shows how to display samples of fonts in VB .NET.
Keywordsfont, bold, underscore, underline, strikeout, strikethru, strikethrough, italic, InstalledFontCollection, installed fonts, VB.NET
CategoriesVB.NET, Graphics
 
When the program loads, it creates an InstalledFontCollection object. It loops over this object's Families collection and adds the names of the fonts to a ListBox.
 
Imports System.Drawing.Text

' Load the list of fonts.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim installed_fonts As New InstalledFontCollection
    lstFont.Items.Clear()
    For Each font_family As FontFamily In _
        installed_fonts.Families
        lstFont.Items.Add(font_family.Name)
    Next font_family
    lstFont.SelectedIndex = 0
End Sub
 
The txtSize_TextChanged event handler executes if the user changes the value of the text's size, clicks on one of the check boxes (bold, italic, underline, strikeout), or selects a font from the ListBox. The event handler calls subroutine ShowSample.
 
' If something changes, display a new sample.
Private Sub txtSize_TextChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    txtSize.TextChanged, chkBold.Click, chkItalic.Click, _
    chkUnderline.Click, chkStrikeout.Click, _
    lstFont.SelectedIndexChanged
    ShowSample()
End Sub
 
Subroutine ShowSample builds the selected font and assigns it to the sample TextBox.
 
' Display a sample of the font.
Private Sub ShowSample()
    ' Compose the font style.
    Dim font_style As FontStyle = FontStyle.Regular
    If chkBold.Checked Then font_style = font_style Or _
        FontStyle.Bold
    If chkItalic.Checked Then font_style = font_style Or _
        FontStyle.Italic
    If chkUnderline.Checked Then font_style = font_style Or _
        FontStyle.Underline
    If chkStrikeout.Checked Then font_style = font_style Or _
        FontStyle.Strikeout

    ' Get the font size.
    Dim font_size As Single = 8
    Try
        font_size = Single.Parse(txtSize.Text)
    Catch ex As Exception
    End Try

    ' Get the font family name.
    Dim family_name As String = "Times New Roman"
    If Not (lstFont.SelectedItem Is Nothing) Then
        family_name = lstFont.SelectedItem.ToString
    End If
    ' Make the new font.
    Dim new_font As New Font( _
        family_name, font_size, font_style)

    ' Set the sample's font.
    txtSample.Font = new_font
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated