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
Views: 63751
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.
What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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 a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by vineetha on Wednesday, January 28, 2009 4:25 AM
please help me to cut copy and images as well as lines circles and etc. from the form
Comment posted by Joe on Friday, June 19, 2009 7:38 AM
Just what I need. but how do I use it with Web page development. The word Imports creates an error. What I would like to use it for is to compare a user input to make sure they enter a valid system font Name. Again this is for ASP.Net web page development.
Comment posted by Ahmed on Friday, September 30, 2011 3:17 AM
Problem on windows 7,your method will return font family name but will not necessarily return postscript font name for example the font appear at fonts folder as Kalinga Bold while font family name is Kalinga ,is there a way to return font data like Tile ,Designed for ,font embediability
Comment posted by Alan8 on Wednesday, August 29, 2012 1:20 PM
InstalledFontCollection only returns TrueType fonts; not OpenType or PostScript.

Non-TrueType fonts don't work in .NET; you can't create a Font with them, and DrawString won't display with them.  (Thanks Microsoft!)

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook