Click here to Skip to main content
12,697,076 members (26,701 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked

Color List Box

, 2 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A list box with color name and previews


This is a list box that can view default or own color names and previews.

Using the Code

These items are manually draw in DrawItem event. It only runs if DrawMode property of listbox is set to OwnerDrawFixed or OwnerDrawVariable.

Here is my DrawItem code:

Private Sub ColoredComboBox_DrawItem(ByVal sender As Object, _
	ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
        If e.Index = -1 Then Exit Sub
        Dim mycolor As String = Strings(e.Index)
        Dim brush1 As Brush = New SolidBrush(GetColor(e.Index))
        Dim rect As Rectangle = e.Bounds
        e.Graphics.FillRectangle(New SolidBrush(sender.backcolor), rect)
        Dim highlightcolor As Color = SystemColors.Highlight
        If (e.State = DrawItemState.Focus) _
        	Or (e.State = DrawItemState.Selected) _
        	Or (e.State = DrawItemState.Selected + DrawItemState.Focus) Then
            e.Graphics.FillRectangle(New SolidBrush(highlightcolor), e.Bounds)
            e.Graphics.FillRectangle(New SolidBrush(sender.BackColor), e.Bounds)
        End If
        rect.Width = WidthOfColorBar
        rect.Height -= 4
        rect.X += 2
        rect.Y += 2
        e.Graphics.FillRectangle(brush1, rect)
        e.Graphics.DrawRectangle(Pens.Black, rect)
        e.Graphics.DrawString(mycolor, sender.font, _
        	New SolidBrush(sender.forecolor), WidthOfColorBar + 5, rect.Y - 2)
    Catch ex As Exception
    End Try
End Sub 

First, it sets variables.

Then it clears the old state of item:

e.Graphics.FillRectangle(New SolidBrush(sender.backcolor), rect)  

It then checks if this item is focused or selected or both, and fills it with the highlight Color.

Then Draw Color Preview and item value.

This class has two arrays:

  1. Colors: It has colors of items.
  2. Strings: It has item values.

This class has two properties:

  1. SelectedColor: It returns the color of the selected item. That color is from Colors(Me.SelectedIndex). It is ReadOnly.
  2. ColorBarWidth: It sets or gets the Width of Color Bar Previews.

Also it has two subroutines for adding items into the listbox:

  1. Public Sub AddItem(ByVal Item As String, ByVal Color As Color) 

    It adds "Item" string with "Color" preview into the listbox.

  2. Public Sub AddKnownColors() 

    It adds all Known Colors with their default names into the listbox.

Here is the code for AddKnownColors():

Dim colorTable As Color = New Color
Dim t As Type = GetType(Color)
Dim pis() As System.Reflection.PropertyInfo = t.GetProperties
For Each pi As System.Reflection.PropertyInfo In pis
    If (pi.PropertyType Is GetType(Color)) Then
        Dim color As Color = CType(pi.GetValue(colorTable, Nothing), Color)
        AddItem(color.Name, color)
    End If

Points of Interest

Remember, if DrawMode property of a listbox is Normal, DrawItem Event will not run!


  • 12-01-2009
    • A listbox that can view default or own color names and previews (first version)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mehdi Ghiasi
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...


Comments and Discussions

Generalcode horror Pin
Mr.PoorEnglish11-Jan-10 0:52
memberMr.PoorEnglish11-Jan-10 0:52 
GeneralMemory Leak PinPopular
VCSKicks11-Dec-09 12:35
memberVCSKicks11-Dec-09 12:35 
GeneralSmall improvement suggestion - split the color names into words... Pin
SimmoTech3-Dec-09 21:02
memberSimmoTech3-Dec-09 21:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 2 Dec 2009
Article Copyright 2009 by Mehdi Ghiasi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid