Colour Individual Items in a winforms ComboBox?

52,724

Solution 1

You may try DrawItem event of ComboBox. Keep your dates on a list and compare them with ID's and brush your items.

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{    
    // Draw the background 
    e.DrawBackground();        

    // Get the item text    
    string text = ((ComboBox)sender).Items[e.Index].ToString();

    // Determine the forecolor based on whether or not the item is selected    
    Brush brush;
    if (YourListOfDates[e.Index] < DateTime.Now)// compare  date with your list.  
    {
        brush = Brushes.Red;
    }
    else
    {
        brush = Brushes.Green;
    }

    // Draw the text    
    e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
}

To fire this event (thanks to @Bolu)

You need to change ComboBox.DrawMode to OwnerDrawFixed/OwnerDrawVariable to fire the comboBox_DrawItem

Solution 2

///The ComboBoxCustom Control:

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

Solution 3

Changing color of combobox back color

You can use this codes for changing back color of combobox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Comboboxrenklendir
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }



    private void cmbrenkli_DrawItem(object sender, DrawItemEventArgs e)
    {




        Color HighlightColor = Color.Red;

        if (e.Index >=0)
        {

            //int sayi = cmbrenkli.Items.Count;

            string deger = cmbrenkli.Items[e.Index].ToString();

            if (deger == "30"|| deger == "50")
            {
                e.Graphics.FillRectangle(new SolidBrush(HighlightColor), e.Bounds);
            }



            e.Graphics.DrawString(cmbrenkli.Items[e.Index].ToString(), e.Font, new SolidBrush(cmbrenkli.ForeColor), new Point(e.Bounds.X, e.Bounds.Y));

            e.DrawFocusRectangle();

        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cmbrenkli.Items.Add("10");
        cmbrenkli.Items.Add("20");
        cmbrenkli.Items.Add("30");
        cmbrenkli.Items.Add("40");
        cmbrenkli.Items.Add("50");
    }
}
}
Share:
52,724
Mike
Author by

Mike

Fresh out of uni and working for a living. Software Developer: C#, Java, JS, Python, er... Let's just say a bit of everything? :)

Updated on July 09, 2022

Comments

  • Mike
    Mike almost 2 years

    I have a dilemma whereby I have a form which contains a number of comboboxes that contain information/options/items that may be invalid/out-of-date in certain circumstances.

    I can't simply remove the out-of-date information from the items, but I do want to give the user a visual-clue when options are invalid.

    I was thinking of colouring in the Items (probably red) to indicate if & when they are invalid. I don't necessarily need to prevent a user from selecting an invalid option, just make them visually aware that they are doing so.

    Can this be done? Can you - dyamically - change the colo(u)r of combobox items?

    Thanks,