Resolved Combo Box With Colors

tommyready

Member
Joined
Jul 1, 2009
Messages
23
Programming Experience
3-5
I'm trying to figure out how to change the background color of a combo box for every item in the selection.

Here is the code I tried:
VB.NET:
Expand Collapse Copy
    Private Sub FillTeamColorsCombo()

        Try
            Dim Colors As ArrayList = FillColorArray()

            For i = 0 To Colors.Count
                cboTeamColor.Items.Add(Colors(i))
                cboTeamColor.BackColor = System.Drawing.Color.FromName(Colors(i))
            Next

        Catch ex As Exception
            MsgBox("Error Filling Colors Combo Box: " + ex.ToString)
        End Try

    End Sub

    Public Function FillColorArray() As ArrayList
        Dim colorNames As New ArrayList

        ' Get an array of PropertyInfo objects
        '  from System.Drawing.Color.
        Dim typ As System.Type = GetType(System.Drawing.Color)
        Dim aPropInfo As System.Reflection. _
           PropertyInfo() = typ.GetProperties
        For Each pi As System.Reflection. _
           PropertyInfo In aPropInfo
            If pi.PropertyType.Name = "Color" _
               And pi.Name <> "Transparent" Then
                colorNames.Add(pi.Name)
            End If
        Next
        Return colorNames
    End Function

This method seems to set the whole combo box back color to whatever the last color was.


The color does not change per item. Can anyone help? I also get an error stating that "Index was out of range"
 
Last edited:
VB.NET:
Expand Collapse Copy
For i = 0 To (Colors.Count - 1)
Set DrawMode property to OwnerDrawFixed and handle the DrawItem event, f.ex with this code:
VB.NET:
Expand Collapse Copy
Dim c As Color = CType(Me.cboTeamColor.Items(e.Index), Color)
Using Brush As New SolidBrush(c)
    e.Graphics.FillRectangle(Brush, e.Bounds)
    e.Graphics.DrawString(c.Name, Me.cboTeamColor.Font, Brushes.Black, e.Bounds)
End Using
You can also list the colors like this:
VB.NET:
Expand Collapse Copy
Dim colors As New List(Of Color)
For Each known As KnownColor In System.Enum.GetValues(GetType(KnownColor))
    Dim c As Color = Color.FromKnownColor(known)
    If Not c.IsSystemColor AndAlso c <> Color.Transparent Then
        colors.Add(c)
    End If
Next
Me.cboTeamColor.DataSource = colors
 
I added This:
VB.NET:
Expand Collapse Copy
    Private Sub cboTeamColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cboTeamColor.DrawItem
        Dim c As Color = CType(Me.cboTeamColor.Items(e.Index), Color)
        Using Brush As New SolidBrush(c)
            e.Graphics.FillRectangle(Brush, e.Bounds)
            e.Graphics.DrawString(c.Name, Me.cboTeamColor.Font, Brushes.Black, e.Bounds)
        End Using
    End Sub

But no such luck. Any other suggestions?:confused:

Edit:
OK I figured it out

I changed:
VB.NET:
Expand Collapse Copy
Dim c As Color = CType(Me.cboTeamColor.Items(e.Index), Color)

to

VB.NET:
Expand Collapse Copy
Dim c As Color = Color.FromName(Me.cboTeamColor.Items(e.Index))

And now it works. Thanks for the Help!!!
 
Back
Top