48

How can I fill a combo-box with a list of all the available fonts in the system?

Moon
  • 19,518
  • 56
  • 138
  • 200
  • Please have a look at these examples (http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Loadallsysteminstalledfonts.htm)(http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Loadallsysteminstalledfonts.htm), (http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Fontlist.htm)(http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Fontlist.htm). – thelost Aug 06 '10 at 17:19
  • @thelost the link no longer exists – Massimiliano Kraus Jun 30 '16 at 15:50

7 Answers7

70

You can use System.Drawing.FontFamily.Families to get the available fonts.

List<string> fonts = new List<string>();

foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
    fonts.Add(font.Name);
}

// add the fonts to your ComboBox here
Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
  • I installed Montserrat font. This code does not list the Montserrat fonts, but Microsoft Word does and Control Panel Appearance and Visualization does list the font as well. So what is wrong? Download URl of font is https://www.fontsquirrel.com/fonts/montserrat – Tomas Kubes May 09 '17 at 13:11
  • 1
    I got it. This code list only TrueType fonts, see this fix http://stackoverflow.com/questions/329225/fonts-missing-in-winforms-fontdialog – Tomas Kubes May 09 '17 at 13:23
11

Not sure why we need to foreach here.

IList<string> fontNames = FontFamily.Families.Select(f => f.Name).ToList();
Paul
  • 12,392
  • 4
  • 48
  • 58
  • I wondered. But then since it's an enumerable, isn't a `foreach` still needed? Or is there a C# syntax for something like `python`'s generator? – Jamie Jan 16 '14 at 15:42
  • 2
    `IList fontNames = FontFamily.Families.Select(f => f.Name).ToList();` ComboBox don't accept `IEnumerable`. – Nicke Manarin Oct 16 '14 at 00:08
4

Use Installed Font Collection class:

http://msdn.microsoft.com/en-us/library/system.drawing.text.installedfontcollection.aspx

This is alternative and equivalent approach to answer from Zach Johnson.

List<string> fonts = new List<string>();
InstalledFontCollection installedFonts = new InstalledFontCollection();          
foreach (FontFamily font in installedFonts.Families)
{               
    fonts.Add(font.Name);
}
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
STO
  • 10,390
  • 8
  • 32
  • 32
3

This is the easy way to do it. It includes two comboboxes 1 for the font name and one for the font size

 public FontFamily[] Families { get; }


 private void Form1_Load(object sender, EventArgs e)
    {

        foreach (FontFamily oneFontFamily in FontFamily.Families)
        {
            comboBox1.Items.Add(oneFontFamily.Name);
        }

        comboBox1.Text = this.richTextBox1.Font.Name.ToString();
        comboBox2.Text = this.richTextBox1.Font.Size.ToString();

        richTextBox1.Focus();

    }

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {

         float size = Convert.ToSingle(((ComboBox)sender).Text);

        richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, size);
    }
Larryrl
  • 121
  • 1
  • 7
1

Please keep in mind all will come from "System.Drawing"

foreach (System.Drawing.FontFamily font in System.Drawing.FontFamily.Families)
{
    comboBox1.Items.Add(font.Name);
}
agileDev
  • 423
  • 5
  • 14
0
ComboBox1.ItemsSource = new InstalledFontCollection().Families;

and for the first time selected item:

private void Combo1_Loaded(object sender, RoutedEventArgs e)
{
    ComboBox1.Text = "Tahoma";
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Ali.DM
  • 258
  • 5
  • 10
-1

You can just bind like this:

ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"
Dyppl
  • 12,161
  • 9
  • 47
  • 68
Nandha kumar
  • 693
  • 7
  • 15