Changing ComboBox dropdown colors with SuggestAppend
I have a form with a ComboBox control whose DataSource is a DataTable. I want to color the backgrounds of the dropdown items based on a field in the DataSource. I'm filling the DataTable and setting the ComboBox DataSource programmatically at runtime.
Right now, I am able to color the choices with the following code that triggers on the DrawItem event of the ComboBox.
Private Sub DrawGridComboBoxItem(sender As Object, e As DrawItemEventArgs) Handles cboLocationID.DrawItem
If e.Index <> -1 Then
e.DrawBackground()
Dim cb As ComboBox = TryCast(sender, ComboBox)
Dim dt As DataTable = TryCast(cb.DataSource, DataTable)
If (e.State And DrawItemState.Focus) <> DrawItemState.Focus AndAlso cb.DroppedDown Then
If dt.Rows(e.Index).Item("locationStatus") = 1 Then
e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
ElseIf dt.Rows(e.Index).Item("locationStatus") = 2 Then
e.Graphics.FillRectangle(Brushes.Gainsboro, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
End If
End If
e.Graphics.DrawString(dt.Rows(e.Index).Item("locationName"), e.Font, Brushes.Black, e.Bounds)
e.DrawFocusRectangle()
End If
End Sub
This works just fine when I click the drop-down arrow on the ComboBox to see the list items. Most have a White background. Some have a grayish (Gainsboro) background. A few have a red background.
The problem happens when I type into the ComboBox instead of clicking and selecting from the list. The type-ahead works fine and the proper choices are filtered and displayed as I type, but none of them have their background color set to anything but white.
The DrawItem event doesn't even get called when typing into the ComboBox. Is there another event I could be hooking into?
The ComboBox has these properties set: