简体   繁体   中英

How to change the ForeColor of individual items in a ComboBox? (C# Winforms)

I know I can change the ForeColor of the ComboBox like this:

comboBox1.ForeColor = Color.Red;

But that makes all the items that color. When you drop down the ComboBox every single item is then red.

I want to individually color items so that the first item is always black, the second always red, the third always blue, et cetera. Is this possible?

Also, I don't think I can create a UserControl for this because the ComboBox I am using is the one for Toolstrips.

You can use DrawItem event .

This event is used by an owner-drawn ComboBox. You can use this event to perform the tasks needed to draw items in the ComboBox. If you have a variable sized item (when the DrawMode property set to DrawMode.OwnerDrawVariable), before drawing an item, the MeasureItem event is raised. You can create an event handler for the MeasureItem event to specify the size for the item that you are going to draw in your event handler for the DrawItem event.

MSDN Example:

// You must handle the DrawItem event for owner-drawn combo boxes.  
// This event handler changes the color, size and font of an 
// item based on its position in the array.
protected void ComboBox1_DrawItem(object sender, 
            System.Windows.Forms.DrawItemEventArgs e)
{

    float size = 0;
    System.Drawing.Font myFont;
    FontFamily family = null;

    System.Drawing.Color animalColor = new System.Drawing.Color();
    switch(e.Index)
    {
        case 0:
            size = 30;
            animalColor = System.Drawing.Color.Gray;
            family = FontFamily.GenericSansSerif;
            break;
        case 1:
            size = 10;
            animalColor = System.Drawing.Color.LawnGreen;
            family = FontFamily.GenericMonospace;
            break;
        case 2:
            size = 15;
            animalColor = System.Drawing.Color.Tan;
            family = FontFamily.GenericSansSerif;
            break;
    }

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

    // Create a square filled with the animals color. Vary the size
    // of the rectangle based on the length of the animals name.
    Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2, 
            e.Bounds.Height, e.Bounds.Height-4);
    e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

    // Draw each string in the array, using a different size, color,
    // and font for each item.
    myFont = new Font(family, size, FontStyle.Bold);
    e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

    // Draw the focus rectangle if the mouse hovers over an item.
    e.DrawFocusRectangle();
}

EDIT : Just found a similar thread .

For a ToolStripComboBox derive from ToolStripControlHost.

//Declare a class that inherits from ToolStripControlHost.
public class ToolStripCustomCombo : ToolStripControlHost
{
    // Call the base constructor passing in a MonthCalendar instance.
    public ToolStripCustomCombo() : base(new ComboBox()) { }

    public ComboBox ComboBox
    {
        get
        {
            return Control as ComboBox;
        }
    }
}

Then say you have a toolstrip named m_tsMain. Here's how to add the new control.

ToolStripCustomCombo customCombo = new ToolStripCustomCombo();
ComboBox c = customCombo.ComboBox;
c.Items.Add("Hello World!!!");
c.Items.Add("Goodbye cruel world!!!");
m_tsMain.Items.Add(customCombo);

And you should be able to add an event handler to c for DrawItem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM