使用可用字体列表填充ComboBox

时间:2010-08-06 17:14:29

标签: c# winforms fonts windows-xp

如何使用系统中所有可用字体的列表填充组合框?

7 个答案:

答案 0 :(得分:63)

您可以使用System.Drawing.FontFamily.Families获取可用字体。

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

答案 1 :(得分:8)

我们不确定为什么需要foreach

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

答案 2 :(得分:3)

这是一种简单的方法。它包括两个用于字体名称的组合框1和一个用于字体大小的组合框

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

答案 3 :(得分:2)

使用Installed Font Collection类:

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

这是Zach Johnson回答的替代和等效方法。

List<string> fonts = new List<string>();
InstalledFontCollection installedFonts = new InstalledFontCollection();          
foreach (FontFamily font in installedFonts.Families)
{               
    fonts.Add(font.Name);
}

答案 4 :(得分:0)

For Example X=1 and Y=1
[]()
()[]
[()]

并且第一次选择了项目:

ComboBox1.ItemsSource = new InstalledFontCollection().Families;

答案 5 :(得分:0)

请记住,所有内容都将来自“ System.Drawing”

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

答案 6 :(得分:-1)

你可以像这样绑定:

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