Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

FontComboBox - a font listing combo box for .NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (18 votes)
4 Aug 2002Ms-PL2 min read 254.8K   8.8K   67   30
A fully owner drawn ComboBox derived class for enumerating and choosing fonts

Image 1

Image 2

Overview

FontComboBox is a ComboBox derived class that has been wrapped  into a .NET control library. It will drop down a list of all the true type fonts installed on your machine. There are two styles for the FontComboBox - one in which every font name is rendered using it's font, and the other where every font name is shown using a default font and then some text is shown using the selected font.

Using it in your project

Add the control to your toolbox

Right click on your toolbox and choose the Customize Toolbox option. Browse to the FontCombo.dll file and add it to your toolbox.

Image 3

Initialize the font combo-boxes

There is a public method named Populate which you need to call on each of your FontComboBox objects.

C#
public Form1()
{
    InitializeComponent();
    fontComboBox1.Populate(false); //Simple
    fontComboBox2.Populate(true); //Elaborate
}

Retrieving the currently selected font

All you need to do is to use the Text property.

C#
private void button1_Click(object sender, System.EventArgs e)
{
    textBox1.Font = new Font(fontComboBox1.Text,10);
    textBox2.Font = new Font(fontComboBox2.Text,10);
}

Class reference

There is just one public method for you to use [other than any public methods or properties available through the base classes]

Populate

public void Populate(bool b)

This will populate the combo box with all true type fonts that are installed on your machine. The bool parameter determines the style of the combo box. Call Populate(false) for a simple style font combo box and call Populate(true) for an elaborate style font combo box. In simple style each font name is rendered using it's font. In elaborate style, each font name is rendered using a default font, but some sample text is rendered using the specific font.

Technical details

Basically this is an owner drawn combo box with the DrawMode set to DrawMode.OwnerDrawVariable. The first thing we need to do would obviously be to enumerate the true type fonts on the machine. This is done as follows [partial code snippet, refer source for complete code]

C#
foreach (FontFamily ff in FontFamily.Families)
{
    if(ff.IsStyleAvailable(FontStyle.Regular))
        Items.Add(ff.Name);                                             
}

Now we need to override OnMeasureItem. This method is called when an owner-drawn combo item needs to be drawn and is called before

C#
OnDrawItem
. Here we can specify the width, height etc. We make use of
C#
Graphics.MeasureString
to get the text extend of the rendered text in a specific font.

C#
protected override void OnMeasureItem(
                        MeasureItemEventArgs e)
{   
    ...

    Graphics g = CreateGraphics();
    e.ItemHeight = (int)g.MeasureString(fontstring, 
        new Font(fontstring,10)).Height;
    w = (int)g.MeasureString(fontstring, 
        new Font(fontstring,10)).Width;
    if(both)
    {
        int h1 = (int)g.MeasureString(samplestr, 
            new Font(fontstring,10)).Height;
        int h2 = (int)g.MeasureString(
            Items[e.Index].ToString(), 
            new Font("Arial",10)).Height;
        int w1 = (int)g.MeasureString(samplestr, 
            new Font(fontstring,10)).Width;
        int w2 = (int)g.MeasureString(
            Items[e.Index].ToString(), 
            new Font("Arial",10)).Width;

        ...
    }

    ...
}

Now obviously we handle OnDrawItem where we do our actual drawing stuff.

C#
protected override void OnDrawItem(
                    DrawItemEventArgs e)
{   
    ...

    string fontstring = Items[e.Index].ToString();
    nfont = new Font(fontstring,10);
    Font afont = new Font("Arial",10);

    if(both)
    {
        Graphics g = CreateGraphics();
        int w = (int)g.MeasureString(fontstring, 
            afont).Width;

        if((e.State & DrawItemState.Focus)==0)
        {
            //Normal item
            e.Graphics.FillRectangle(new SolidBrush(
                SystemColors.Window),
                e.Bounds.X+ttimg.Width,e.Bounds.Y,
                e.Bounds.Width,e.Bounds.Height);
            e.Graphics.DrawString(fontstring,afont,
                new SolidBrush(SystemColors.WindowText),
                e.Bounds.X+ttimg.Width*2,e.Bounds.Y);   
            e.Graphics.DrawString(samplestr,nfont,
                new SolidBrush(SystemColors.WindowText),
                e.Bounds.X+w+ttimg.Width*2,e.Bounds.Y); 
        }
        else
        {
            //Highlighted item

            ...
        }   
    }
    else
    {

        if((e.State & DrawItemState.Focus)==0)
        {
            //Normal item
            e.Graphics.FillRectangle(new SolidBrush(
                SystemColors.Window),
                e.Bounds.X+ttimg.Width,e.Bounds.Y,
                e.Bounds.Width,e.Bounds.Height);
            e.Graphics.DrawString(fontstring,nfont,
                new SolidBrush(SystemColors.WindowText),
                e.Bounds.X+ttimg.Width*2,e.Bounds.Y);

        }
        else
        {
            //Highlighted item

            ...
        }           

    }

    ...
}

Acknowledgments

  • Chris Losinger - I wrote this class partially due to my current project's requirements and partially because I was inspired by Chris Losinger's MFC font combo box.
  • Senkwe Chanda - Senkwe's excellent beginner article for making owner drawn combo boxes helped me a great deal.
  • Nnamdi Onyeyiri - Nnamdi was with me on Sonork when I wrote this class and I owe him the tip for using system font colors for highlighting . Initially I was using a hard coded color.

Links

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
Questionthank you! Pin
Misael Moneró Thompson19-Oct-14 16:34
Misael Moneró Thompson19-Oct-14 16:34 
QuestionMemory Management Issues Pin
Eric Freed17-Jun-12 19:17
Eric Freed17-Jun-12 19:17 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey20-Feb-12 1:47
professionalManoj Kumar Choubey20-Feb-12 1:47 
QuestionPopulate doesn't catch new Fonts Pin
Phillip Rumple26-Oct-11 14:20
Phillip Rumple26-Oct-11 14:20 
AnswerRe: Populate doesn't catch new Fonts Pin
Member 109185549-Jul-14 6:06
Member 109185549-Jul-14 6:06 
SuggestionAnother code change (inspired by Lorad's changes) [modified] Pin
Life Lockszmith10-Aug-11 6:03
Life Lockszmith10-Aug-11 6:03 
GeneralFontcombobox in vb.net Pin
samuelkod21-May-10 10:59
samuelkod21-May-10 10:59 
QuestionGreat work... Pin
amolpbhavsar24-Feb-10 17:53
amolpbhavsar24-Feb-10 17:53 
GeneralSet Inital Value Pin
YoonMi10-Oct-07 6:10
YoonMi10-Oct-07 6:10 
AnswerRe: set Arial font as the initial value Pin
nipsonanomimata21-Oct-08 23:05
nipsonanomimata21-Oct-08 23:05 
GeneralRe: set Arial font as the initial value Pin
Larry_Smith_IPro17-Dec-10 10:04
Larry_Smith_IPro17-Dec-10 10:04 
GeneralRe: set Arial font as the initial value Pin
nipsonanomimata17-Dec-10 20:36
nipsonanomimata17-Dec-10 20:36 
GeneralRe: set Arial font as the initial value Pin
  Forogar  7-Jul-11 9:44
professional  Forogar  7-Jul-11 9:44 
GeneralPlacing this FontComboBox in a ToolStrip Pin
YoonMi9-Oct-07 13:58
YoonMi9-Oct-07 13:58 
Generalported to vb Pin
halsoft22-Mar-06 17:22
halsoft22-Mar-06 17:22 
QuestionFont Weight property for the text box control Pin
Baber Saeed15-Jan-06 22:36
Baber Saeed15-Jan-06 22:36 
GeneralAutoScale Pin
mmcguirk115-Dec-04 6:24
mmcguirk115-Dec-04 6:24 
GeneralRe: AutoScale Pin
Larry_Smith_IPro17-Dec-10 9:57
Larry_Smith_IPro17-Dec-10 9:57 
GeneralCouple of changes Pin
Jeff Lindholm14-Oct-04 9:38
Jeff Lindholm14-Oct-04 9:38 
GeneralA problem of OwnerDraw ComboBox Pin
sarmoo14-Sep-03 20:30
sarmoo14-Sep-03 20:30 
GeneralRe: A problem of OwnerDraw ComboBox Pin
James Miles / 20810974310-Nov-04 21:11
James Miles / 20810974310-Nov-04 21:11 
GeneralLayout Mod, editable ComboBox & MS look (yet no history) Pin
mistery2214-Sep-03 5:09
mistery2214-Sep-03 5:09 
GeneralAny Chance.... Pin
tdevere14-Jun-03 9:28
tdevere14-Jun-03 9:28 
GeneralLekker Pin
Paul Watson4-Aug-02 22:25
sitebuilderPaul Watson4-Aug-02 22:25 
GeneralNice work ! Pin
TigerNinja_4-Aug-02 21:13
TigerNinja_4-Aug-02 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.