Marco
Marco

Reputation: 563

Changing the format of a ComboBox item

Is it possible to format a ComboBox item in C#? For example, how would I make an item bold, change the color of its text, etc.?

Upvotes: 11

Views: 20837

Answers (5)

Martin Davies
Martin Davies

Reputation: 199

Just to add to the answer supplied by Dan, don't forget that if you have bound the list to a Datasource, rather than just having a ComboBox with plain strings, you won't be able to redraw the entry by using combo.Items[e.Index].ToString().

If for example, you've bound the ComboBox to a DataTable, and try to use the code in Dan's answer, you'll just end up with a ComboBox containing System.Data.DataRowView, as each item in the list isn't a string, it's a DataRowView.

The code in this case would be something like the following:

 private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                if (e.Index == -1)
                    return;
                ComboBox combo = ((ComboBox)sender);

                using (SolidBrush brush = new SolidBrush(e.ForeColor))
                {
                    Font font = e.Font;
                    DataRowView item = (DataRowView)combo.Items[e.Index];

                    if (/*Condition Specifying That Text Must Be Bold*/) {
                        font = new System.Drawing.Font(font, FontStyle.Bold);
                    }
                    else {
                        font = new System.Drawing.Font(font, FontStyle.Regular);
                    }                    

                    e.DrawBackground();
                    e.Graphics.DrawString(item.Row.Field<String>("DisplayMember"), font, brush, e.Bounds);
                    e.DrawFocusRectangle();
                }

            }

Where "DisplayMember" is name of the field to be displayed in the list (set in the ComboBox1.DisplayMember property).

Upvotes: 5

Dan
Dan

Reputation: 45752

As old as this post is, I found it useful as a starting point to search from but ended up having better results using the method shown here by @Paul.

Here is the code I used to conditionally make items in a combo box appear bold, I find that the answer marked correct for this question has strange behaviour - when you hover over an item is goes slightly bolder and stays that way as if it is being redrawn over. This code results in a more natural look:

private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
                return;
            ComboBox combo = ((ComboBox)sender);
            using (SolidBrush brush = new SolidBrush(e.ForeColor))
            {
                Font font = e.Font;
                if (/*Condition Specifying That Text Must Be Bold*/)
                    font = new System.Drawing.Font(font, FontStyle.Bold);
                e.DrawBackground();
                e.Graphics.DrawString(combo.Items[e.Index].ToString(), font, brush, e.Bounds);
                e.DrawFocusRectangle();
            }

        }

Upvotes: 14

Bob
Bob

Reputation: 99824

You can do this by setting the DrawMode to OwnerDrawFixed which allows you to manually draw the items using the DrawItem event.

comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) {    
    Font font = comboBox1.Font;
    Brush brush = Brushes.Black;
    string text = comboBox1.Items[e.Index]; 

    if (you want bold)
        font = new Font(font, FontStyle.Bold);

    if (you want green)
        brush = Brushes.Green;

    e.Graphics.DrawString(text, font, brush, e.Bounds);
}

Upvotes: 9

Fabian Vilers
Fabian Vilers

Reputation: 2992

No, there's no built-in property to do it. You'll have to built your own control and override the rendering.

Upvotes: 0

RvdK
RvdK

Reputation: 19800

Yes, but with creating your own ComboBox with custom drawing See here on MSDN

Upvotes: 0

Related Questions