2

I have a ComboBox that I have modified like this:

enter image description here

The code for that is this (cat_color is an array with strings like "#7FFFD4"):

private void cboCategory_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index != -1)
    {
        e.DrawBackground();
        e.Graphics.FillRectangle(new SolidBrush(ColorTranslator.FromHtml(cat_color1[e.Index])), e.Bounds);
        Font f = cboCategory.Font;
        e.Graphics.DrawString(cboCategory.Items[e.Index].ToString(), f, new SolidBrush(ColorTranslator.FromHtml(cat_color2[e.Index])), e.Bounds, StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }
}

My goal now is to change the item back color when I hover over an item. Is this possible?

spunit
  • 523
  • 2
  • 6
  • 23
  • Here's an example where the items text is changed but maybe it's helpful for you http://www.dreamincode.net/forums/topic/269678-change-combobox-items-text-color-on-hover/ – Sybren Nov 07 '14 at 19:01
  • Looks good... I'll try that out in a minute. Thanks. – spunit Nov 07 '14 at 19:18
  • I finally managed to get the color as I wanted, but that color stays when I'm closing the drop down, which I don't want. Maybe there's a way to change the background color when closing the drop down? – spunit Nov 07 '14 at 19:54
  • Think this is what you want? http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownclosed%28v=vs.110%29.aspx . Handle this event to set the background color when closing the drop down. – Sybren Nov 07 '14 at 19:59
  • But if i write this: [combobox1.BackColor = Color.Red;] in that event, nothing changes. I think something must be coded in the cboCategory_DrawItem Event to show... Or am I wrong? – spunit Nov 07 '14 at 20:08
  • Are you sure you've added the eventhandler to your combobox? What happens if you place for example a MessageBox.Show() in the dropdownclosed event? – Sybren Nov 07 '14 at 20:13
  • The MessageBox shows at the right time, so it happens. But what should I write in there then? To change the selected item back color. – spunit Nov 07 '14 at 20:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64512/discussion-between-sybren-and-spunit). – Sybren Nov 07 '14 at 20:21

2 Answers2

3

This will do the job for you.

if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
    e.DrawBackground();
    e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight), e.Bounds);
    Font f = cboCategory.Font;
    e.Graphics.DrawString(cboCategory.Items[e.Index].ToString(), f, new SolidBrush(ColorTranslator.FromHtml(cat_color2[e.Index])), e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}
Sybren
  • 1,071
  • 3
  • 17
  • 51
1

I believe your attempting to do this in a traditional Windows Form Application. An example to modify your color based on the 'hover' would be located in the Mouse Event.

  • Mouse Enter
  • Mouse Move
  • Mouse Hover / Mouse Down / Mouse Wheel
  • Mouse Up
  • Mouse Leave

In our particular instance we would want to focus on the Hover, Down, and Wheel Event. A useful project can be found here. An example that should allow you to modify that would be:

 public class ComboTheme : ComboBox
    {
         new public DrawMode DrawMode { get; set; }
         public Color HighlightColor { get; set; }



    public ComboTheme()
     {
          base.DrawMode = DrawMode.OwnerDrawFixed;
          this.HighlightColor = Color.Red;
          this.DrawItem += new DrawItemEventHandler(ComboTheme_DrawItem); 
     }

     public void ComboTheme_DrawItem(object sender, DrawItemEventArgs e)
     {
          if(e.Index > 0)
          {
               ComboBox box = ((ComboBox)sender);
               if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
               {
                    e.Graphics.FillRectangle(new SolidBrush(HighlightColor), e.Bounds);
               }

               else { e.Graphics.FillRectangle(new SolidBrush(box.BackColor), e.Bounds); }

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

}

You can find in depth information here.

But you can anchor:

public void cmbContent_MouseHover(object sender, EventArgs e)
{
    // Logic
}
Community
  • 1
  • 1
Greg
  • 11,302
  • 2
  • 48
  • 79