BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Graphics
.NET 1.1+

Retrieving the Installed Font List

A common task when creating programs that permit text editing, particularly What-You-See-Is-What-You-Get (WYSIWYG) applications, is to retrieve the list of installed typefaces. The user's preferred fonts and styles may then be utilised as desired.

InstalledFontCollection Class

Microsoft Windows permits users to add new fonts to the list available to the operating system. In this way, a user can ensure that they have access to their favourite typefaces for use in their applications. When you are developing software that involves the editing of text, it is important that you can identify all of the available fonts and allow their use in your program.

The InstalledFontCollection class is provided as a part of the System.Drawing.Text namespace of the .NET framework. This class holds a collection of FontFamily objects, each representing a single font, with one object for every typeface installed in Microsoft Windows.

Listing the Available Fonts

Listing the names of the installed typefaces using the above classes is reasonably simple. To begin, create a new Windows Forms application. Add a listbox to the form that is created automatically, naming the listbox "FontListBox". We can then add code to the form's Load event to read the font list and add the name of each style to the list. Double-click the background of the form to add the new event, which should look similar to the following:

private void Form1_Load(object sender, EventArgs e)
{
}

The InstalledFontCollection is found in the System.Drawing.Text namespace. FontFamily is in the System.Drawing namespace. Ensure that this namespace is referenced in a using directive at the top of the form's code:

using System.Drawing;
using System.Drawing.Text;

We can now create a new InstalledFontCollection object. This object will automatically be populated with the list of installed typefaces. To create the object, add the following code in the form's load event:

InstalledFontCollection fonts = new InstalledFontCollection();

The new object includes a property named "Families". This is a collection of FontFamily objects, each representing a single installed font. FontFamily objects have various members that describe the fonts in the family and how they may be used. In this case we are simply interested in the Name property, which contains the font's descriptive name. Using a foreach loop, each font family's name may be added to the listbox. Add the following loop to the load event and execute the program to see the results.

foreach (FontFamily family in fonts.Families)
{
    FontListBox.Items.Add(family.Name);
}
16 July 2008