Change the Selection Color of a WinForms ComboBox

c#, combobox, winforms

Solution

Set the DrawMode property of the ComboBox control to either OwnerDrawFixed (if the height of each item will be the same) or OwnerDrawVariable (if the height of each item may vary).

Then modify your DrawItem event to something like the following (substitute your own colours in obviously):

private void comboBoxDb_DrawItem(object sender, DrawItemEventArgs e) 
{
    var combo = sender as ComboBox;

    if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.BlueViolet), e.Bounds);
    }
    else
    {
        e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window), e.Bounds);
    }

    e.Graphics.DrawString(combo.Items[e.Index].ToString(),
                                  e.Font,
                                  new SolidBrush(Color.Black),
                                  new Point(e.Bounds.X, e.Bounds.Y));
}

Problem

All, I have had an indepth look but can't seem to find what I am looking for. I want ot change the selection color of a ComboBoc control (ideally without having to sub-class the control). I though doing the following would work, but this event is not even firing ``` private void comboBoxDb_DrawItem(object sender, DrawItemEventArgs e) { ComboBox combo = sender as ComboBox; e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds); string strSelectionColor = @"#99D4FC"; Color selectionColor = System.Drawing.ColorTranslator.FromHtml(strSelectionColor); e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, new SolidBrush(selectionColor), new Point(e.Bounds.X, e.Bounds.Y)); } ``` but this event is not even firing. What am I doing wrong here? Thanks for your time. Edit. Although the non firing was caused by not setting the DrawMode property of the ComboBox correctly pointed out by @Teppic, this is still not doing what I require. I want to set the selection color, what I have done above does (I have blocked out names here) Whereas I want to change the blue highlight ion the control as shown here.

Original source

Related problems