List every available font in a dropdown box

Hi,

In this post I’ll show you how can generate a list with every font that’s installed on your pc in your ASP.NET page or windows application. You need the following namespaces so don’t forget to include them.

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

Now I’ll show you the sourcecode:

protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { //Create an instance of this class, this contains all the fonts InstalledFontCollection fonts = new InstalledFontCollection(); //create a FontFamily variable and run through each available family foreach (FontFamily family in fonts.Families) { //and add that family to the dropdownbox lstFonts.Items.Add(family.Name); } } //change the font-family style of the label's text. this.lblTest.Style["font-family"] = lstFonts.SelectedItem.Text; }

I added a combobox to the page and enabled the AutoPostBack propperty. This way a PostBack is generated every time the user selects another font. With every different selection the Page_Load event is raised. This isn’t the best way to do this, but good enough for this example.

image


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word