Search code examples
.netfontscomboboxdelphi-prismondrawitem

How to display FontFamily in Combobox?


I have a combobox and I need to populate it with all the available fonts in the system - their actual name, style, etc...

From all the information I can find online, I am able to put together the DrawItem event, but I keep running into the following error, "Cannot invoke non-delegate type 'System.Drawing.Font'" In fact, I borrowed line per line from other websites and made some changes. So, I thought it should have worked.

Here is how I populate the items list in the ComboBox:

method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
var thefont:Font;
begin
    if (ComboBox4.Items.Count>0) then
        ComboBox4.Items.Clear;

    for each oneFontFamily in FontFamily.Families do
    begin
        if (oneFontFamily.IsStyleAvailable(FontStyle.Regular)) then
            thefont := new Font(oneFontFamily.Name, 15)
        else if (oneFontFamily.IsStyleAvailable(FontStyle.Bold)) then
            thefont := new Font(oneFontFamily.Name, 15,FontStyle.Bold)
        else if (oneFontFamily.IsStyleAvailable(FontStyle.Italic)) then
            thefont := new Font(oneFontFamily.Name, 15,FontStyle.Italic)
        else if (oneFontFamily.IsStyleAvailable(FontStyle.Strikeout)) then
            thefont := new Font(oneFontFamily.Name, 15, FontStyle.Strikeout)
        else if (oneFontFamily.isStyleAvailable(FontStyle.Underline)) then
            thefont := new Font(oneFontFamily.Name, 15, FontStyle.Underline);

    if (thefont <> nil) then
        ComboBox4.Items.Add(theFont);
        end;
end;

Here is the combobox4 drawitem event:

method MainForm.comboBox4_DrawItem(sender: System.Object; e: System.Windows.Forms.DrawItemEventArgs);
begin
    if e.index = -1 then exit;

    // Draw the background of the item
    e.DrawBackground();

    // Should we draw the focus rectangle
    if ((e.State and DrawItemState.Focus) <> DrawItemState.Checked) then
        e.DrawFocusRectangle();

        // Create a new background brush.
        var b := new SolidBrush(e.ForeColor);

        // Draw the item.
        // This line raises the above mentioned error.
        e.Graphics.DrawString(FontFamily(comboBox4.Items[e.Index]).Name, font(comboBox4.Items[e.Index]), b, e.Bounds.x,e.Bounds.y);  <<===== Here is where the error is raised
end;

UPDATED: I modified the line that was causing the error and it compiles without error now but as I stated in my comment it is not drawing the fonts in their own style and size.

e.Graphics.DrawString(FontFamily(comboBox4.Items[e.Index]).Name, new font((comboBox4.Items[e.Index] as Font), (comboBox4.Items[e.Index] as Font).Style), b, e.Bounds.x,e.Bounds.y);

UPDATE: I forgot to set the DrawMode to OwnerDrawFixed. Now it is calling my DrawItem Event but still not drawing the Fonts in their own style and sizes.

I want the combobox to look like the following image:

someone else's combobox

Not like mine below:

actual Image of the winform with combobox


Solution

  • Here is my answer with working code.

    • create a new project and open your main winform. Open your ToolBox and place a combobox on your mainform.
    • Open the property window for the combobox that you just placed on the winform.
    • Set the following properties for your combobox as follows: DrawMode = OwnerDrawFixed, DropDownStyle = DropDownList, FormattingEnabled = true, GenerateMemeber = true, IntegralHeight = false and ItemHeight = 25.
      • Create Mainform_Load method by double clicking on the main winform and copy the following code accordingly to your load method.

    ,

    Method MainForm.MainForm_Load(sender: System.Object; e:System.EvenArgs);
    var 
       thefont:Font;
    begin
    if (ComboBox1.Items.Count>0) then
       ComboBox1.Items.Clear;
    
    for each oneFontFamily in FontFamily.Families do
    begin
        if (oneFontFamily.IsStyleAvailable(FontStyle.Regular)) then
            thefont := new Font(oneFontFamily.Name, 12)
        else if (oneFontFamily.IsStyleAvailable(FontStyle.Bold)) then
            thefont := new Font(oneFontFamily.Name, 12,FontStyle.Bold)
        else if (oneFontFamily.IsStyleAvailable(FontStyle.Italic)) then
            thefont := new Font(oneFontFamily.Name, 12,FontStyle.Italic)
        else if (oneFontFamily.IsStyleAvailable(FontStyle.Strikeout)) then
            thefont := new Font(oneFontFamily.Name, 12, FontStyle.Strikeout)
        else if (oneFontFamily.isStyleAvailable(FontStyle.Underline)) then
            thefont := new Font(oneFontFamily.Name, 12, FontStyle.Underline);
    
        if (thefont <> nil) then
            ComboBox1.Items.Add(theFont);
    end;
    end;
    
    • Create DrawItem event for the your comboBox and copy the following code accordingly into your drawitem event.

    '

        Method MainForm.ComboBox1_DrawItem(sender:System.Object; e: System.Windows.Forms.DrawItemEventArgs);
        var theobject:Font;
        begin
           if e.Index=-1 then exit;
           // Draw the background of the item
           e.DrawBackground();
    
           // Should we draw the focus rectangle
           if ((e.State and DrawItemState.Focus) <> DrawItemState.Checked) then
              e.DrawFocusRectangle();
    
           // Create a new background brush.
           var b := new SolidBrush(e.ForeColor);
           theobject := (ComboBox1.Items[e.Index] as font);
    
           // Draw the item.
           e.Graphics.DrawString(theobject.Name, theObject, b,e.Bounds);
        end;
    

    When you have it all done and running you should have a combobox1 displaying fonts as follows:

    ComboBox displaying Font Text