Results 1 to 8 of 8

Thread: [RESOLVED] ComboBox with different color Items and DropDownList Style

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    221

    Resolved [RESOLVED] ComboBox with different color Items and DropDownList Style

    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
    Last edited by ovi_gm; Apr 25th, 2013 at 07:48 AM.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: ComboBox with different color Items and DropDownList Style

    Not sure where you got "If e.State = 769" from, but I do it like this and seems to work as expected...
    Code:
    'DRAW TEXT USING SPECIFIED FONT AND COLOR
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected 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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    221

    Re: ComboBox with different color Items and DropDownList Style

    Well, as far as I understood from Dunfiddlin here on the forum the 769 is the equivalent for DrawItemState.Selected.

    Something happens in VB when I also put a label or a button on the form and the DraItem event doesn't work anymore.

    See this post:
    http://www.vbforums.com/showthread.p...election-color

    Now I cheked your code and it works if the DropDownStyle property of the combo is set to DropDown. But it still doesn't work with DropDownList and this is what I needed.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: ComboBox with different color Items and DropDownList Style

    It's all in those e.State values!

    vb.net Code:
    1. Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
    2.         e.DrawBackground()
    3.         'GET ITEM TO DRAW
    4.         If Not e.Index = -1 Then
    5.             Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem)
    6.             'DRAW TEXT USING SPECIFIED FONT AND COLOR
    7.             ListBox1.Items.Add(CInt(e.State).ToString)
    8.             If e.State = 769 Or e.State = 785 Or e.State = 4881 Then
    9.                 e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
    10.             Else
    11.                 e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
    12.             End If
    13.         End If
    14.         e.DrawFocusRectangle()
    15.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: ComboBox with different color Items and DropDownList Style

    Quote Originally Posted by ovi_gm View Post
    Now I cheked your code and it works if the DropDownStyle property of the combo is set to DropDown. But it still doesn't work with DropDownList and this is what I needed.
    Well I have the combo set to OwnerDrawFixed and DropDownList, works just fine, you must have some sort of other problems, And adding buttons or labels to a form shouldn't have any effect on the way an owner drawn control draws, at least I've never seen it have any! BTW, Win7 with themes enabled here.

    Edit: "If e.State = 769 Or e.State = 785 Or e.State = 4881 Then"... acts the same way as what I posted above, I don't see any difference.

    Good Luck!

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: ComboBox with different color Items and DropDownList Style

    And adding buttons or labels to a form shouldn't have any effect on the way an owner drawn control draws
    It doesn't. It has an effect on the way that e.State is reported. Without a label or a button on the form you get a single state reported. With a label or a button on the form, it reports combinations of states. I have absolutely no idea why that's true but trust me, after a fun hour or two tracking every change, there is no doubt that it is.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    221

    Re: ComboBox with different color Items and DropDownList Style

    Hello and thank you for your answers.
    Edgemeal, at home I also have Win7 and it works ok. But in windows xp it is not working ok. The text of the selection remains black and it is very hard to read over a blue background.

    Anyway, Dunfiddlin's code works ok for me now.

    Topic resolved.

    Thx a lot.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ComboBox with different color Items and DropDownList Style

    Quote Originally Posted by dunfiddlin View Post
    It's all in those e.State values!

    vb.net Code:
    1. Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
    2.         e.DrawBackground()
    3.         'GET ITEM TO DRAW
    4.         If Not e.Index = -1 Then
    5.             Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem)
    6.             'DRAW TEXT USING SPECIFIED FONT AND COLOR
    7.             ListBox1.Items.Add(CInt(e.State).ToString)
    8.             If e.State = 769 Or e.State = 785 Or e.State = 4881 Then
    9.                 e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
    10.             Else
    11.                 e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
    12.             End If
    13.         End If
    14.         e.DrawFocusRectangle()
    15.     End Sub
    You should absolutely never be comparing a variable or property whose type is an Enum with numbers like that. Absolutely NEVER. As Edgemeal has shown, if you're interested in one specific value then you can pick that value out of an arbitrary combination regardless of what the other values are with a bitwise logic. If you're interested in specific combinations then you should specify those combinations as their Enum values, not as numbers. To combine two Enum values you use the bitwise Or operator. Or is to combine, And is to test, Xor is to toggle, And Not is to remove.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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