Results 1 to 4 of 4

Thread: [RESOLVED] Weird behaviour with a combobox selection color

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    221

    Resolved [RESOLVED] Weird behaviour with a combobox selection color

    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 = 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
    e.DrawFocusRectangle()
    End Sub

    This works as planned. Now, if the color of the text is black I want it to turn white when it is selected. Otherwise it will be very difficult to read (black on blue). So I wrote the "if e.State = DrawItemState.Selected then" statement.

    And it works.

    But if I add a label anywhere on the form then this statement doesn't work at all.

    Just make a new form with a added combobox and copy the code. I think this is weird. As soon as I delete my label it works.

    Any ideas how to solve this problem? And why does it occur?

    Thank you.

    PS. The combobox DrawMode property must be set to OwnerDrawFixed or OwnerDrawVariable

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    221

    Re: Weird behaviour with a combobox selection color

    I have noticed that if I add a button I get the same behaviour? Can anyone help?

    If this can't be done, how can I change the selection color to a lighter one?

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

    Re: Weird behaviour with a combobox selection color

    For reasons that I wot not, the presence of a label or button (and possibly other controls) sends e.State into a kind of 'verbose' mode. Counter it with ...

    If e.State = DrawItemState.Selected OrElse e.State = 769 Then

    ... 769 is the integer value of [Selected, NoAccelerator, NoFocusRect], for what it's worth.
    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!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    221

    Re: Weird behaviour with a combobox selection color

    I struggled all day with this and found no solution. But your solution works just fine.
    I couldn't think of that in a million years

    Thanks a lot Dunfiddlin. It works very well now.

    Cheers!

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