Display a list of all fonts installed on a computer using Windows Forms
Posted by: Suprotim Agarwal ,
on 3/19/2008,
in
Category WinForms & WinRT
Abstract: The System.Drawing.Text.InstalledFontCollection is a non-inheritable class that contains the functionality to represent the fonts installed on the system. You can use this class and return an array containing the names of all the fonts on your system. In this article, let us see how to use this class and display the list of available fonts
Display a list of all fonts installed on a computer using Windows Forms
The System.Drawing.Text.InstalledFontCollection is a non-inheritable class that contains the functionality to represent the fonts installed on the system. You can use this class and return an array containing the names of all the fonts on your system. Let us see how to use this class and display the list of available fonts:
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. Rename it to ‘lstBoxFonts’. 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;
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)
{
lstBoxFonts.Items.Add(font.Name);
}
}
catch
{
}
}
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim fonts As InstalledFontCollection = New InstalledFontCollection()
Try
For Each font As FontFamily In fonts.Families
lstBoxFonts.Items.Add(font.Name)
Next font
Catch
End Try
End Sub
Note: If you want the font name to be displayed in the style that the font supports, then while displaying the font, set the control’s font family to ‘new Font(font,12)’. Just remember that some fonts do not support the Regular style, so an error may occur while doing so.
Well that was quiet simple. We saw how we can use the InstalledFontCollection class to represent the fonts installed on the system. I hope you liked the article and I thank you for viewing it.
To stay updated with the latest articles, please subscribe to the RSS feed over here.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn