How to make a Font Combobox in C#?

前端 未结 3 904
孤独总比滥情好
孤独总比滥情好 2020-12-21 15:04

I want to make a combobox in a c# .NET 4.5 Windows Forms application (note: not WPF) that displays all the truetype installed font on the system and that every font is form

3条回答
  •  借酒劲吻你
    2020-12-21 15:54

    You can achieve this by following a project published couple of years ago here, https://www.codeproject.com/Articles/318174/Creating-a-WYSIWYG-font-ComboBox-using-Csharp

    But instead of doing that, you can follow the Narek Noreyan steps and adding more, use a rich text box, whose text font will be changed after selecting a font family from the combo box.

    Some code snippet here,

    using System.Drawing.Text;
    void mainformload(object sender, EventArgs s)
    {
        InstalledFontCollection inf=new      InstalledFontCollection();
        foreach(Font family font in inf.Families)
            combobox.Items.Add(font.Name); //filling the font name
        //get the font name of the rich text box text
        combobox.Text=this.richtextbox.Font.Name.ToString();
        }
    void comboboxselectionchanged(object sender, EventArgs e)
    {
        richtextbox.Font=new Font(combobox.text, richtextbox.Font.Size);
    }
    

提交回复
热议问题