4

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)

NotWhatIsRequired

Whereas I want to change the blue highlight ion the control as shown here.

enter image description here

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • So, the problem is that the event is not firing? Have you tried your code on another event? – chaliasos Jul 25 '12 at 12:37
  • I haven't purley because of the use of the `DrawItemEventArg` in the above. I am not familiar with another event that uses this as an event argument. The event is hooked up correctly, but does not seem to fire when I select a new index etc. Thanks for your time. – MoonKnight Jul 25 '12 at 12:49
  • If the event fires normally, but doesn't fires when the selected item changes, how about using another event, that fires when the item changes and then invalidate the item, so that draw item is called? – Hinek Jul 25 '12 at 12:55
  • I think I may have to override/subclass the control. But in this case I have no experience of doing this for ComboBoxes, so any help in this respect would be appreicated. – MoonKnight Jul 25 '12 at 13:14

2 Answers2

16

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));
}
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
Teppic
  • 2,506
  • 20
  • 26
  • Thanks a ton! Please also explain what this line means: `if((e.State & DrawItemState.Selected) == DrawItemState.Selected)` – Ali Ashraf Dec 20 '16 at 13:28
  • e.State can represent multiple DrawItemState values at once. 'if((e.state & DrawItemState.Selected) == DrawItemState.Selected)' is a way to check if one of those values is DrawItemState.Selected. A detailed explanation can be found at http://stackoverflow.com/a/8480/193460 – Teppic Dec 21 '16 at 00:09
0

To make a ComboBox fire DrawItem event, you must set its DrawMode to OwnerDrawFixed or OwnerDrawVariable. You can read about it in detail on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode Then just check DrawItemEventArgs.State to find if item is selected or something.

Roman Zavalov
  • 575
  • 3
  • 8