-
May 11th, 2013, 07:19 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Combobox Item Colour
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
-
May 11th, 2013, 08:29 AM
#2
Re: Combobox Item Colour
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
-
May 11th, 2013, 08:36 AM
#3
Thread Starter
Addicted Member
Re: Combobox Item Colour
Thanks. Unfortuantly its giving me this error message:
InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index
-
May 11th, 2013, 08:43 AM
#4
Re: Combobox Item Colour
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
-
May 11th, 2013, 08:57 AM
#5
Re: Combobox Item Colour
Originally Posted by Sean.Downes
Thanks. Unfortuantly its giving me this error message:
InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index
Yea, that happens sometimes. Take Joacim's advice and bail when it is -1.
-
May 11th, 2013, 11:12 AM
#6
Thread Starter
Addicted Member
Re: Combobox Item Colour
Fair enough, what should I change the number to then or do I do something different?
-
May 11th, 2013, 11:28 AM
#7
Re: Combobox Item Colour
Joacim's code is post #4 shows what to do. He wrapped the drawing code inside an If...Then to check for -1.
-
May 11th, 2013, 11:37 AM
#8
Thread Starter
Addicted Member
Re: Combobox Item Colour
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
-
May 11th, 2013, 12:00 PM
#9
Re: [RESOLVED] Combobox Item Colour
If you really used the code I posted in post #4 it's impossible for you to get the exception you described.
-
May 11th, 2013, 12:37 PM
#10
Thread Starter
Addicted Member
Re: [RESOLVED] Combobox Item Colour
Unless something has been missed out of the code, I've done exactly what you said and the exception kept coming up :/
-
May 11th, 2013, 02:05 PM
#11
Re: [RESOLVED] Combobox Item Colour
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
Last edited by Joacim Andersson; May 11th, 2013 at 02:09 PM.
-
May 11th, 2013, 02:12 PM
#12
Thread Starter
Addicted Member
Re: [RESOLVED] Combobox Item Colour
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|