You can use the DrawItem
Event.
First you have to set the DrawMode
of the ComboBox
to OwnerDrawFixed
Then you set the DrawItem
to something like the following:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Font font = (sender as ComboBox).Font;
Brush backgroundColor;
Brush textColor;
if (e.Index == 1 || e.Index == 3)
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
backgroundColor = Brushes.Red;
textColor = Brushes.Black;
}
else
{
backgroundColor = Brushes.Green;
textColor = Brushes.Black;
}
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
backgroundColor = SystemBrushes.Highlight;
textColor = SystemBrushes.HighlightText;
}
else
{
backgroundColor = SystemBrushes.Window;
textColor = SystemBrushes.WindowText;
}
}
e.Graphics.FillRectangle(backgroundColor, e.Bounds);
e.Graphics.DrawString((sender as ComboBox).Items[e.Index].ToString(), font, textColor, e.Bounds);
}
This example will make the default background colour Green with black text, and the highlighted item will have a red background and black text, of the items at indexes 1 and 3.
You could also set the font of individual items using the font
variable.