1

I've got a combobox control which I've set to "Drop Down List" style (because I don't want the user to be able to type text in the combobox). Only probably is that I don't like the way that the "Drop Down List" style looks, so I'm trying to change it to where it looks like the normal combobox style.

So I set the draw mode to "owner draw fixed". My goal is to have a white background / black text for the combobox items that are not hovered over, and a blue background / white text for the items that are hovered over (just like a normal combobox would look).

The background colors are working as desired, but the text colors are not (the text is staying black, even on items that are hovered over).

Here is my code...

Private Sub ComboBox1_DrawItem_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

    Dim TextBrush As Brush

    If (e.State And DrawItemState.HotLight) = DrawItemState.HotLight Then
        TextBrush = Brushes.White
    Else
        TextBrush = Brushes.Black
    End If

    Dim index As Integer = If(e.Index >= 0, e.Index, 0)
    e.DrawBackground()
    e.Graphics.DrawString(ComboBox1.Items(index).ToString(), e.Font, TextBrush, e.Bounds, StringFormat.GenericDefault)
    e.DrawFocusRectangle()

End Sub

Any ideas?

I've been searching Google for a couple hours for the solution, but no luck so far.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
NotQuiteThereYet
  • 477
  • 3
  • 13
  • 25
  • Have you set a breakpoint, or print debug statements so you can tell if e.State ever contains the HotLight flag? – Sam Axe Dec 02 '13 at 20:06
  • Just tested that, and it doesn't appear to ever trigger the "HotLight" state, even when an item is hovered over. When an item is hovered over, the e.State.ToString value appears to be "Selected, Focus, NoAccelerator, NoFocusRect". Not sure what to make of that. Strange! – NotQuiteThereYet Dec 02 '13 at 20:16
  • Then I'd suggest trying one of those values, like `Selected`, instead of `HotLight`. – Sam Axe Dec 02 '13 at 20:17
  • I'd also respect the `NoFocusRect` flag. – Sam Axe Dec 02 '13 at 20:18
  • OK... just did a little more testing, and the e.State value (not the 'e.State.ToString' value, just the 'e.State' value) for a hovered item appears to be "785". No idea what the reasoning behind that would be, but I've got it working now based on that. Thanks anyways, Dan-o :-) – NotQuiteThereYet Dec 02 '13 at 20:27
  • ComboBox simply doesn't support the HotLight state, the item you hover is always Selected. TreeView and Listview support it, if you set their HotTracking property to True. – Hans Passant Dec 02 '13 at 20:50

3 Answers3

2

e.State is a flag-set. Its value is comprised by ORing flag values together. So you will want to use If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then, and not a the observed 785 value.

true == 785 == (Selected OR Focus OR NoAccelerator OR NoFocusRect)

If you remove the NoFocusRect the integer value will change, but the bit mask will still contain the value for Selected.

InteXX
  • 6,135
  • 6
  • 43
  • 80
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
1

Check these two links:

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
1

here's how i change the selected background color of the combobox and the selection text forecolor

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
    'create custom color and brush for selection color
    Dim myColor As Color
    Dim myBrush As Brush

    myColor = Color.FromArgb(231, 199, 100)

    myBrush = New SolidBrush(myColor)

    If e.Index < 0 Then Exit Sub

    Dim rect As Rectangle = e.Bounds
    Dim TextBrush As Brush
    Dim index As Integer = If(e.Index >= 0, e.Index, 0)

    If e.State And DrawItemState.Selected Then
        'selection background color
        e.Graphics.FillRectangle(myBrush, rect)
        'selection forecolor
        TextBrush = Brushes.Black
        e.Graphics.DrawString(ComboBox1.Items(index).ToString(), e.Font, TextBrush, e.Bounds, StringFormat.GenericDefault)
    Else
        e.Graphics.FillRectangle(Brushes.Black, rect)
        TextBrush = Brushes.White
        e.Graphics.DrawString(ComboBox1.Items(index).ToString(), e.Font, TextBrush, e.Bounds, StringFormat.GenericDefault)
    End If

End Sub
Shimrod
  • 39
  • 8