Results 1 to 12 of 12

Thread: [RESOLVED] Combobox Item Colour

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Resolved [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

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Combobox Item Colour

    Thanks. Unfortuantly its giving me this error message:

    InvalidArgument=Value of '-1' is not valid for 'index'.
    Parameter name: index

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Combobox Item Colour

    Quote Originally Posted by Sean.Downes View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Combobox Item Colour

    Fair enough, what should I change the number to then or do I do something different?

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    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

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    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 :/

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    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
  •  



Click Here to Expand Forum to Full Width