Public Class Form1
Private cb As New CCombobox With {.Parent = Me, .Location = New Point(10, 10)}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cb.Items.Add(New CComboboxItem("Item Number 1", Color.Green, Color.Yellow))
cb.Items.Add(New CComboboxItem("Item Number 2", Color.Blue, Color.Red))
cb.Items.Add(New CComboboxItem("Item Number 3", Color.Red, Color.Plum))
End Sub
End Class
Class CCombobox : Inherits ComboBox
Sub New()
Me.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
End Sub
Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
MyBase.OnDrawItem(e)
Dim item As CComboboxItem = DirectCast(Me.Items(e.Index), CComboboxItem)
Using ForeBrush As Brush = New SolidBrush(item.Forecolor)
Using BackBrush As Brush = New SolidBrush(item.Backcolor)
e.Graphics.FillRectangle(BackBrush, e.Bounds)
e.Graphics.TranslateTransform(0, e.Bounds.Y)
e.Graphics.DrawString(item.ToString, Me.Font, ForeBrush, 0, 0)
End Using
End Using
End Sub
End Class
Class CComboboxItem
Public Sub New(Value As Object, Color As Color, BackColor As Color)
Me.Value = Value
Me.Forecolor = Color
Me.Backcolor = BackColor
End Sub
Public Property Value As Object = Nothing
Public Property Forecolor As Color = Color.Black
Public Property Backcolor As Color = Color.Black
Public Overrides Function ToString() As String
Return Value.ToString
End Function
End Class