Displaying list of installed fonts with C#
I know that all 1337 hax0rz (sorry, I promise I will not be doing this any more, as a matter of fact it really bugs me when people are using this 1337 crap) are using stuff like Ruby and Python, I know that system programming is for old farts and the future is in web development and all that. I am not disputing this, but there are some cool things you can do with C# and Windows coding. For some bizzare reason, I feel that it might be a good idea to publish some code which I find neat. One of the reasons I like C# is that generally if you need a solution to a particular problem, it is most likely to be alot simpler then what I think. Here is how you get the list of all installed fonts loaded into a ComboBox.
First thing firstusing System.Drawing.Text;
Need this to get the collection of fonts.
Here is the form load method:
private void Form1_Load(object sender, EventArgs e)
{
var fonts = new InstalledFontCollection();
foreach (var family in fonts.Families)
{
comboBox1.Items.Add(family.Name);
}
}
Pretty straight forward, get all the fonts into a collection called fonts, then loop through each element in this collection and add a new item to the ComboBox. One can say, hey Paul, this could be done via DataBinding – yeah, sure, here is what you would do:
private void Form1_Load(object sender, EventArgs e)
{
var fonts = new InstalledFontCollection().Families;
comboBox1.DataSource = fonts;
}
But let’s take a look at the output – here is what you get with my little foreach loop:
I know, not too fancy, but clean and I like clean. Now here is what you get with DataBinding:
I know, there are ways to clean this up, but why bother?
Next I just added a small text box to show how the fonts are going to look like:
To get this going, all I need is an event that will be triggered whenever the selection at the ComboBox is changed:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(comboBox1.SelectedItem.ToString(), 12);
}
That’s it, we are done here. It compiles, it runs, and it switches the font on the TextBox. Here is the complete code for the form:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Text;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var fonts = new InstalledFontCollection();
foreach (var family in fonts.Families)
{
comboBox1.Items.Add(family.Name);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(comboBox1.SelectedItem.ToString(), 12);
}
}
}
Beauty of C# is that it took me about 4 minutes to write this code. It is clean, simple and does exactly what it is supposed to do. It might not be the coolest technology out there, but it is good enough for what I do.


