Font Enumeration in VB.Net

If you are developing some editors is .Net, you have to list all the Fonts in you system in a Font Combo. Here is a simple code snippet which will display all the Windows Font in a Dropdown list.

For Each MyFont As FontFamily In System.Drawing.FontFamily.Families
Dropdownlist.Items.Add(MyFont.Name)
Next

It is in C#

foreach (FontFamily item in System.Drawing.FontFamily.Families)
{
this.comboBox1.Items.Add(item.Name);
}

Here is an Update. You can render the Dropdown such way that is will display all the font name, in the Font.

ComboBox lst = (ComboBox)sender;
e.DrawBackground();
Font font = new Font(lst.Items[e.Index].ToString(), 12);
e.Graphics.DrawString(lst.Items[e.Index].ToString(), font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();

You need to write this code in the DrawItem event of the Combo box. Also you need to change the DrawMode property of the Combo box.

This entry was posted in .Net, .Net 3.0 / 3.5 and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>