Set ComboBox Item color

Background

Not sure when this is useful, but its handy code to have when developing windows forms applications. The need can arise when we have to color code the data that we are displaying like status, severity, types, etc. As title describes we are going to set the fore ground color every ComboBox item in Windows Forms Application.

Let’s Start Coding

Create a Windows Forms Application project

Add a ComboBox on Form.

Most importantly set the ComboBox’s DrawMode property to OwnerDrawVariable – if not, we will not be able to see our drawings!

Fill the combobox using following code

private void Form1_Load(System.Object sender, System.EventArgs e)
{
	string col = null;
	foreach (string c in System.Enum.GetNames(typeof(System.Drawing.KnownColor)))
	{
		col = c;
		ComboBox1.Items.Add(Color.FromName(col));
	}
}

Then add DrawItem evet definition for combobox and add following code to it.

private void cmbExpTypes_DrawItem(object sender, DrawItemEventArgs e)
{
	if (e.Index < 0)
	{
		e.DrawBackground();
		e.DrawFocusRectangle();
		return;
	}
	// set default color
	Color CurrentColor = Color.Red;

	// get a square using the bounds height
	Rectangle SizeRect = new Rectangle(2, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height - 2);

	Brush ComboBrush = Brushes.Blue;

	// call these methods first
	e.DrawBackground();
	e.DrawFocusRectangle();

	// change brush color if item is selected or you can add you own condition based on item values
	if (e.State == System.Windows.Forms.DrawItemState.Selected)
	{
		ComboBrush = Brushes.White;
	}
	else
	{
		ComboBrush = Brushes.Blue;
	}

	// draw a rectangle and fill it
	//e.Graphics.DrawRectangle(new Pen(CurrentColor), SizeRect);
	//e.Graphics.FillRectangle(new SolidBrush(CurrentColor), SizeRect);

	// draw a border
	//SizeRect.Inflate(1, 1);
	//e.Graphics.DrawRectangle(Pens.Black, SizeRect);

	// draw the item with same and text specified color name
	e.Graphics.DrawString(cmbExpTypes.Items[e.Index].ToString(), cmbExpTypes.Font, ComboBrush, e.Bounds.X, e.Bounds.Y);
}

You can also set the background color as well, I have commented the code of the same.

Tagged with: ,
  • The best way to do this is create a customComboBox, like this:

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    namespace TyroDeveloper
    {
    public class ComboBoxCustom:ComboBox
    {
    public ComboBoxCustom() {
    this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
    base.OnDrawItem(e);
    e.DrawBackground();
    ComboBoxItem item = (ComboBoxItem)this.Items[e.Index];
    Brush brush = new SolidBrush(item.ForeColor);
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    { brush = Brushes.Yellow; }
    e.Graphics.DrawString(item.Text, this.Font, brush, e.Bounds.X, e.Bounds.Y);
    }
    }
    public class ComboBoxItem
    {
    public ComboBoxItem() { }

    public ComboBoxItem(string pText, object pValue)
    {
    text = pText; val = pValue;
    }

    public ComboBoxItem(string pText, object pValue, Color pColor)
    {
    text = pText; val = pValue; foreColor = pColor;
    }

    string text = “”;
    public string Text {
    get { return text; } set { text = value; }
    }

    object val;
    public object Value {
    get { return val; } set { val = value; }
    }

    Color foreColor = Color.Black;
    public Color ForeColor {
    get { return foreColor; } set { foreColor = value; }
    }

    public override string ToString()
    {
    return text;
    }
    }
    }

    //To add the items:

    comboBoxCustom1.Items.Add(new ComboBoxItem(“México”,”0″,Color.Green));
    comboBoxCustom1.Items.Add(new ComboBoxItem(“USA”, “1”, Color.Blue));
    comboBoxCustom1.Items.Add(new ComboBoxItem(“China”, “2”, Color.Red));

    the source url page:
    http://www.tyrodeveloper.com/2012/04/color-in-combobox-item.html