Hi again,
Can anyone tell me how to change the font colour of an individual item in a combobox?
IE
"A-Alpha" = Red
"B - Bravo" = Orange
"C-Charlie" = Green
"D-Delta" = Black
Thanks
Printable View
Hi again,
Can anyone tell me how to change the font colour of an individual item in a combobox?
IE
"A-Alpha" = Red
"B - Bravo" = Orange
"C-Charlie" = Green
"D-Delta" = Black
Thanks
Set the DrawMode property of the ComboBox to OwnerDrawFixed and use code similar to this:Code:Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
Dim brush As Brush
Dim text As String = ComboBox1.Items(e.Index).ToString
Select Case text.Substring(0, 1)
Case "A"
brush = Brushes.Red
Case "B"
brush = Brushes.Orange
Case "C"
brush = Brushes.Green
Case "D"
brush = Brushes.Black
Case Else
brush = Brushes.Black
End Select
e.DrawBackground()
e.Graphics.DrawString(text, e.Font, brush, e.Bounds.X, e.Bounds.Y)
End Sub
Thanks. Unfortuantly its giving me this error message:
InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index
I see, you didn't mention that you've set the DropDownStyle to DropDownList. You can simply just check if the Index is -1 and not draw anything.Code:Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.Index > -1 Then
Dim brush As Brush
Dim text As String = ComboBox1.Items(e.Index).ToString
Select Case text.Substring(0, 1)
Case "A"
Brush = Brushes.Red
Case "B"
Brush = Brushes.Orange
Case "C"
Brush = Brushes.Green
Case "D"
Brush = Brushes.Black
Case Else
Brush = Brushes.Black
End Select
e.DrawBackground()
e.Graphics.DrawString(text, e.Font, Brush, e.Bounds.X, e.Bounds.Y)
End If
End Sub
Fair enough, what should I change the number to then or do I do something different?
Joacim's code is post #4 shows what to do. He wrapped the drawing code inside an If...Then to check for -1.
Yeah ive put that in and it still comes up with the same message. Thanks anyway very appreciated but ive sparked a new idea and i think i prefer the new idea. Thanks
If you really used the code I posted in post #4 it's impossible for you to get the exception you described.
Unless something has been missed out of the code, I've done exactly what you said and the exception kept coming up :/
Then you haven't used the code I posted. Look here:Code:Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.Index > -1 Then 'This line makes sure that the Index is above -1
Dim brush As Brush
Dim text As String = ComboBox1.Items(e.Index).ToString 'So if it was -1 this line will never be executed so no exception can be raised here
Select Case text.Substring(0, 1)
Case "A"
Brush = Brushes.Red
Case "B"
Brush = Brushes.Orange
Case "C"
Brush = Brushes.Green
Case "D"
Brush = Brushes.Black
Case Else
Brush = Brushes.Black
End Select
e.DrawBackground()
e.Graphics.DrawString(text, e.Font, Brush, e.Bounds.X, e.Bounds.Y)
End If
End Sub
Yep I used that, I'll try that again though, but that's the exact code I used and it came up with the exception