Tuesday, January 6, 2009

Display a list of all fonts installed on a computer using Windows Forms and WPF Application

For displaying all the fonts available on your computer in Windows Application, we use InstalledFontCollection class. It is a sealed class, so that it is not inheritable. InstalledFontClass is available under the System.Drawing.Text namespace.

This class returns an array containg all the installed font on your system. Lets see, the following example express how to use the InstalledFontCollection and display the list of fonts in the ListBox control.



The following steps are required to use InstalledFontCollection Class for displaying all the installed font of your computer in the ListBox control.


Step 1: Create a new WinForm application. Open VS 2005 or VS 2008 > File > New > Project > Choose either Visual Basic or Visual C# in the Project Types > Choose Windows Forms application in Templates> Give a Name to the project and click OK.


Step 2: Drag and drop a Listbox on the form. We will display the list of available fonts in the listbox.


Step 3: In the ‘Form1.cs’, add a reference to the System.Drawing.Text namespace. This namespace contains the ‘InstalledFontCollection’ class which contains the functionality to represent the installed fonts.
C#
using System.Drawing.Text;


Note:- If you r using VB.Net use the following lines of code for adding the System.Drawing.Text namespace
VB.NET
Imports System.Drawing.Text
Step 4: In the Form1_Load, we will create an instance of the ‘InstalledFontCollection’ class and then use the Families property to return an array of ‘FontFamily’ objects associated with the FontCollection class. The font family names are then added to the listbox on each loop.
C#
private void Form1_Load(object sender, EventArgs e)
{
InstalledFontCollection fonts = new InstalledFontCollection();
try
{
foreach (FontFamily font in fonts.Families)
{
listBox1.Items.Add(font.Name);
}
}
catch
{
}
}


Displaying all the installed font of your computer in WPF Application the following codes are useful.

The following XAML code is used for displaying all the installed font of your computer in the ComboBox control. For creating a WPF application choose the WPF templete from the new project dialog box. For creating a WPF application we need .Net 3.5 Framework and Visual Studio 2008.























No comments:

Is this blog solve your Problem?