Click here to Skip to main content
15,888,733 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.8K   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 
... quite a bit of the code is redundant, particularly ...
Dim LastColorIndex As Integer
Dim colors(65535) As Color
Dim Strings(65535) As String
... as the ListBox.Items or ComboBox.Items can be used instead.

Here's my code, all tried and tested ...

Imports System.Drawing
Imports System.Windows.Forms
Public Class ColoredListBox
    Inherits System.Windows.Forms.ComboBox
    Dim WidthOfColorBar As Integer
    Private Function GetColor(ColorName As String) As Color
        Return Color.FromName(ColorName)
    End Function
    Public Sub SetTo(ColorName As String)
        Me.SelectedIndex = Me.FindStringExact(ColorName)
    End Sub
    Public ReadOnly Property SelectedColor(ColorName As String) As Color
        Get
            Try
                Return GetColor(ColorName)
            Catch ex As Exception

            End Try
        End Get
    End Property
    Public Property ColorBarWidth() As Integer
        Get
            Return WidthOfColorBar
        End Get
        Set(ByVal value As Integer)
            WidthOfColorBar = value
        End Set
    End Property
    Public Sub New()
        Me.AddKnownColors()
        WidthOfColorBar = 30
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    End Sub
    Public Sub AddKnownColors()
        Me.Items.Clear()
        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
    End Sub
    Public Sub AddItem(ByVal Item As String, ByVal Color As Color)
        Me.Items.Add(Item)
    End Sub
    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 sColor As String = Me.Items(e.Index)
            Dim brush1 As Brush = New SolidBrush(Color.FromName(sColor))
            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(sColor, sender.font, New SolidBrush(sender.forecolor), WidthOfColorBar + 5, rect.Y - 2)
            brush1.Dispose()
        Catch ex As Exception

        End Try
    End Sub
End Class


Hopefully this helps someone.
Generalcode horror Pin
Mr.PoorEnglish10-Jan-10 23:52
Mr.PoorEnglish10-Jan-10 23:52 
GeneralMemory Leak PinPopular
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.