Click here to Skip to main content
15,881,650 members
Articles / Multimedia / GDI+

Color List Box

Rate me:
Please Sign up or sign in to vote.
4.88/5 (10 votes)
2 Dec 2009CPOL1 min read 43.6K   1.5K   17   4
A list box with color name and previews

Introduction

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:

VB.NET
Private Sub ColoredComboBox_DrawItem(ByVal sender As Object, _
	ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    Try
        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)
        Else
            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:

VB.NET
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. VB.NET
    Public Sub AddItem(ByVal Item As String, ByVal Color As Color) 

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

  2. VB.NET
    Public Sub AddKnownColors() 

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

Here is the code for AddKnownColors():

VB.NET
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
Next

Points of Interest

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

History

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

License

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


Written By
Other
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat Solution, but ... Pin
Jeff Law14-Jul-23 13:21
Jeff Law14-Jul-23 13:21 
Generalcode horror Pin
Mr.PoorEnglish10-Jan-10 23:52
Mr.PoorEnglish10-Jan-10 23:52 
GeneralMemory Leak Pin
VCSKicks11-Dec-09 11:35
VCSKicks11-Dec-09 11:35 
GeneralSmall improvement suggestion - split the color names into words... Pin
SimmoTech3-Dec-09 20:02
SimmoTech3-Dec-09 20: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.