Moon
Moon

Reputation: 20022

Fill ComboBox with List of available Fonts

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

Upvotes: 49

Views: 50348

Answers (7)

agileDev
agileDev

Reputation: 441

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);
}

Upvotes: 2

STO
STO

Reputation: 10658

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);
}

Upvotes: 4

Larryrl
Larryrl

Reputation: 131

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);
    }

Upvotes: 3

Ali.DM
Ali.DM

Reputation: 258

ComboBox1.ItemsSource = new InstalledFontCollection().Families;

and for the first time selected item:

private void Combo1_Loaded(object sender, RoutedEventArgs e)
{
    ComboBox1.Text = "Tahoma";
}

Upvotes: 0

Nandha kumar
Nandha kumar

Reputation: 773

You can just bind like this:

ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"

Upvotes: -1

Paul
Paul

Reputation: 12440

Not sure why we need to foreach here.

IList<string> fontNames = FontFamily.Families.Select(f => f.Name).ToList();

Upvotes: 12

Zach Johnson
Zach Johnson

Reputation: 24232

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

Upvotes: 71

Related Questions