更改WinForms ComboBox的选择颜色

所有,我有一个深刻的外观,但似乎无法找到我要找的东西。 我想要改变ComboBoc控件的选择颜色(理想情况下不必对控件进行子类化)。 我虽然做以下工作会起作用,但这个事件甚至没有开火

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)); } 

但这个事件甚至没有开火。 我在这做错了什么?

谢谢你的时间。

编辑。 尽管非触发是由于未设置@Teppic正确指出的ComboBox的DrawMode属性引起的,但这仍然没有按照我的要求进行。 我想设置选择颜色,我上面做了什么(我在这里阻止了名字)

NotWhatIsRequired

而我想更改控件的蓝色突出显示,如此处所示。

在此处输入图像描述

将ComboBox控件的DrawMode属性设置为OwnerDrawFixed(如果每个项的高度相同)或OwnerDrawVariable(如果每个项的高度可能不同)。

然后将您的DrawItem事件修改为类似以下内容(显然替换您自己的颜色):

 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)); } 

要使ComboBox触发DrawItem事件,必须将其DrawMode设置为OwnerDrawFixedOwnerDrawVariable 。 您可以在MSDN上详细阅读它: http : //msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode然后只需检查DrawItemEventArgs.State以查找是否选择了项目。