Click here to Skip to main content
12,886,892 members (36,041 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 31 Oct 2008

Owner drawn ListBox

, 31 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A listbox which supports custom drawing properties for each list item.


This is a list box which supports drawing list box items with custom fore colors. This could easily be modified to support more features, but I kept it simple and will allow others to explore.

The list box draw mode has been changed to:

ListBox.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed

This property change tells the list box to raise the DrawItem event. The DrawItem will be responsible for drawing all the list items. In this case, the list box will attempt to cast each list item to the ICustomListBoxItemSupport interface; if that works, it will then pull the TextColor and display the value using the interface. This allows each list item to provide its own color when drawing the text. If the cast does not work, the list box should still work close to the existing list box functionality.

The benefits to using the ICustomListBoxItemSupport interface is you can implement the interface in existing objects which inherit from others, which makes it easier to add to existing business objects.


I needed a way to indicate some list items as different (or errors, in my case).

Using the code

Have your list items implement the ICustomListBoxItemSupport interface, and the custom list box will do the rest.

Imports System.Drawing

''' <summary>
''' Example custom list box item.
''' </summary>
''' <remarks></remarks>
Public Class Person

    Implements ICustomListBoxItemSupport

    Private m_Name As String = String.Empty

    Public Property Name() As String
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

#Region "ICustomListBoxItemSupport implements"
    Public ReadOnly Property TextColor() As System.Drawing.Color _
           Implements ICustomListBoxItemSupport.TextColor
            If m_Name.Length > 25 Then
                Return Color.DarkRed
                Return SystemColors.WindowText
            End If
        End Get
    End Property

    Public Property DisplayValue() As String _
           Implements ICustomListBoxItemSupport.DisplayValue
            Return Me.Name
        End Get
        Set(ByVal value As String)

        End Set
    End Property

#End Region

End Class


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


About the Author

Eric Paul Schneider
Software Developer (Senior)
United States United States
Software Engineer, Finance market, VB5/6/NET/JAVA/C/C++.

You may also be interested in...

Comments and Discussions

QuestionProgressbar Pin
Amrykid1-Nov-08 4:52
memberAmrykid1-Nov-08 4:52 
AnswerRe: Progressbar Pin
eschneider1003-Nov-08 6:48
membereschneider1003-Nov-08 6:48 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 31 Oct 2008
Article Copyright 2008 by Eric Paul Schneider
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid