Title: Display font samples for all installed fonts in WPF and C#
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 the example to experiment with it and to see additional details.
|