In the Visual Studio forms designer, the property grids allow you to pick a colour from a drop-down box.
I was a little surprised to find that there was no existing control that already had this functionality.
A search on the internet turned up a number of results, but they all seemed to have over complicated the problem, and were hundreds of lines of code or they used the tool-strip drop down.
This little control extension sets the
, this activates the
event and the
method (if you override the
method, but don't set the control to be
is never invoked, and the
event is never called).
method is overridden, and if the item that is being drawn is a
, then the method manufactures a
object, with the
property set to the current item colour.
event is handled, and in this event we simply duplicate what the standard event did, which is to draw the background and the text value over it.
class ColourCombo : ComboBox
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler(ColourCombo_DrawItem);
void ColourCombo_DrawItem(object sender, DrawItemEventArgs e)
e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
protected override void OnSelectedIndexChanged(EventArgs e)
this.BackColor = this.SelectedColour;
if (this.BackColor.GetBrightness() < 0.5)
this.ForeColor = Color.White;
this.ForeColor = Color.Black;
protected override void OnDrawItem(DrawItemEventArgs e)
var item = this.Items[e.Index];
if (item is Color)
DrawItemEventArgs de = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State, e.ForeColor, (Color)item);
public void LoadRainbow()
public void LoadDarkRainbow()
public Color SelectedColour
if (SelectedItem != null && SelectedItem is Color)
this.SelectedItem = value;