Changing the format of a ComboBox item

前端 未结 5 1854
挽巷
挽巷 2021-01-02 04:33

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.?

相关标签:
5条回答
  • 2021-01-02 05:16

    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();
                }
    
            }
    
    0 讨论(0)
  • 2021-01-02 05:23

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

    0 讨论(0)
  • 2021-01-02 05:28

    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).

    0 讨论(0)
  • 2021-01-02 05:28

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

    0 讨论(0)
  • 2021-01-02 05:34

    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);
    }
    
    0 讨论(0)
提交回复
热议问题