Display font samples for all installed fonts in WPF and C#

[font samples]

The example Display font samples on a computer in C# lets you select a font and see a sample of that font. This example lets you enter text and then see font samples for every font installed on the system. That makes it a bit easier to search for fonts with a particular appearance. (For example, if you want a nice brush-like font to use in writing an invitation.)

Enter a sample string and select a font size. When you click Show Samples, the following code executes.

// Display samples of the text in the available fonts.
private void btnShowSamples_Click(object sender, RoutedEventArgs e)
{
    lblFontName.Content = null;
    string sample = txtSample.Text;
    lstSamples.Items.Clear();
    foreach (FontFamily family in Fonts.SystemFontFamilies)
    {
        Label label = new Label();
        label.Content = sample;
        label.FontFamily = family;
        label.FontSize = sliSize.Value;
        lstSamples.Items.Add(label);
    }
}

This code clears the font name label at the bottom of the window, gets the text you entered, and clears the lstSamples ListBox.

To display the font samples, the code then loops through all of the font families in the Fonts.SystemFontFamilies collection. For each font family, the program creates a new Label and displays the sample text in it using that font. It then adds the Label to the ListBox. Adding a control to the ListBox.Items collection also sets the control’s parent so you don’t need to do that separately.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in fonts, graphics, wpf, XAML and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *