Changing the text color of the "hovered" item in a Combobox?

3.6k views Asked by At

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.

3

There are 3 answers

1
Sam Axe On BEST ANSWER

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.

1
Victor Zakharov On
0
Shimrod On

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