Hello everybody,
I want to have a combobox that has different colors for it's items. So here is an example. New Form with an added combobox.
'STRUCTURE THAT HOLDS THE DATA FOR THE COMBOBOX ITEMS
Public Structure ColoredComboboxItem
Dim Text As String
Dim Color As Color
Public Overrides Function ToString() As String
Return Text
End Function
End Structure
WHEN FORM LOADS, LOAD SOME DATA INTO THE COMBOBOX:
Dim myItem As New ColoredComboboxItem
With myItem
.Text = "Festo AG"
.Color = Color.Black
End With
ComboBox1.Items.Add(myItem)
myItem = New ColoredComboboxItem
With myItem
.Text = "Bosch-Rexroth"
.Color = Color.LightGray
End With
ComboBox1.Items.Add(myItem)
myItem = New ColoredComboboxItem
With myItem
.Text = "Parcon"
.Color = Color.Black
End With
ComboBox1.Items.Add(myItem)
and the last thing is to write the DrawItem event for the combobox:
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
e.DrawBackground()
'GET ITEM TO DRAW
Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem)
'DRAW TEXT USING SPECIFIED FONT AND COLOR
If e.State = 769 Then
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
Else
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
End If
e.DrawFocusRectangle()
End Sub
The combo must have the DrawItem property set to OwnerDrawFixed in order to work.
NOW, this works only if the DropDownStyle property is set to DropDown but i need it to be DropDownList so the user can't change any of the items it contains.
If I do it like this I get: InvalidArgument=Value of '-1' is not valid for 'index'. at the DIM line in the Draw item event.
I tried to insert If e.index<>-1 then in the drawItem event and it works. But then the e.DrawBackground() doesn't work the way it should. Background is blue but text must be white or lightgray in order to have a contrast.
Any help with this?
Thx
I want to have a combobox that has different colors for it's items. So here is an example. New Form with an added combobox.
'STRUCTURE THAT HOLDS THE DATA FOR THE COMBOBOX ITEMS
Public Structure ColoredComboboxItem
Dim Text As String
Dim Color As Color
Public Overrides Function ToString() As String
Return Text
End Function
End Structure
WHEN FORM LOADS, LOAD SOME DATA INTO THE COMBOBOX:
Dim myItem As New ColoredComboboxItem
With myItem
.Text = "Festo AG"
.Color = Color.Black
End With
ComboBox1.Items.Add(myItem)
myItem = New ColoredComboboxItem
With myItem
.Text = "Bosch-Rexroth"
.Color = Color.LightGray
End With
ComboBox1.Items.Add(myItem)
myItem = New ColoredComboboxItem
With myItem
.Text = "Parcon"
.Color = Color.Black
End With
ComboBox1.Items.Add(myItem)
and the last thing is to write the DrawItem event for the combobox:
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
e.DrawBackground()
'GET ITEM TO DRAW
Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem)
'DRAW TEXT USING SPECIFIED FONT AND COLOR
If e.State = 769 Then
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
Else
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
End If
e.DrawFocusRectangle()
End Sub
The combo must have the DrawItem property set to OwnerDrawFixed in order to work.
NOW, this works only if the DropDownStyle property is set to DropDown but i need it to be DropDownList so the user can't change any of the items it contains.
If I do it like this I get: InvalidArgument=Value of '-1' is not valid for 'index'. at the DIM line in the Draw item event.
I tried to insert If e.index<>-1 then in the drawItem event and it works. But then the e.DrawBackground() doesn't work the way it should. Background is blue but text must be white or lightgray in order to have a contrast.
Any help with this?
Thx