Click here to Skip to main content
13,550,613 members
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 29 May 2002

A C# ColorListBox

, 29 May 2002
Rate this:
Please Sign up or sign in to vote.
In this article we will see how to write an owner drawn ListBox
<!-- Download Links --> <!-- Article image -->

Sample Image - ColorListBox.gif

<!-- Add the rest of your HTML here -->


In this article we will see how to write owner drawn ListBox control. Typically, Windows handles the task of drawing the items to display in the ListBox. You can use the DrawMode property and handle the MeasureItem and DrawItem events to provide the ability to override the automatic drawing that Windows provides and draw the items yourself. You can use owner-drawn ListBox controls to display variable-height items, images, or a different color or font for the text of each item in the list.


We start by creating a Windows Application. Add ListBox to the form and set its DrawMode property to OwnerDrawVariable. Alternatively you can add following line to the InitializeComponent function of your form,

//lstColor is ListBox control
this.lstColor.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;

Next add following lines below above line

//tell windows we are interested in drawing items in ListBox on our own
this.lstColor.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);

//tell windows we are interested in providing  item size
this.lstColor.MeasureItem += 
  new System.Windows.Forms.MeasureItemEventHandler(this.MeasureItemHandler);

 By doing this, windows will send us DrawItem and MeasureItem event for each item added to ListBox.

Next, add handlers for these events

private void DrawItemHandler(object sender, DrawItemEventArgs e)
                          new Font(FontFamily.GenericSansSerif, 
                                   14, FontStyle.Bold),
                          new SolidBrush(color[e.Index]), 

private void MeasureItemHandler(object sender, MeasureItemEventArgs e)
    e.ItemHeight= 22;

In above code date is array that holds items to be inserted and color is array of class Color

Thats it. We are done!

Send me your comments at


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sanjay Ahuja
United States United States
Sanjay Ahuja is a Bachelor of Engineer and has completed his CDAC in Pune,India. He is currently working as a consultant for Verizon.

You may also be interested in...

Comments and Discussions

PraiseGreat example; I used your example in my program Pin
Brad Soverns26-Feb-16 8:12
memberBrad Soverns26-Feb-16 8:12 
GeneralMy vote of 5 Pin
ChriSSi00313-Aug-14 21:00
memberChriSSi00313-Aug-14 21:00 
GeneralMy vote of 3 Pin
mbsmbs10-Jun-12 1:07
membermbsmbs10-Jun-12 1:07 
GeneralColorListBox class Pin
brtnik7-Oct-08 22:27
memberbrtnik7-Oct-08 22:27 
GeneralRe: ColorListBox class Pin
Rainer Gustin24-May-14 7:27
memberRainer Gustin24-May-14 7:27 
GeneralAnother example Pin
brtnik7-Oct-08 1:06
memberbrtnik7-Oct-08 1:06 
QuestionHow can I use XML resx file Pin
vicpark25-Nov-02 21:42
membervicpark25-Nov-02 21:42 
AnswerRe: How can I use XML resx file Pin
Heath Stewart10-Dec-02 2:01
memberHeath Stewart10-Dec-02 2:01 
GeneralVariable Geight ListBox-es Pin
wqw1-Jun-02 9:32
memberwqw1-Jun-02 9:32 
GeneralNice Pin
Nish - Native CPian30-May-02 17:11
memberNish - Native CPian30-May-02 17:11 

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
Web01-2016 | 2.8.180515.1 | Last Updated 30 May 2002
Article Copyright 2002 by Sanjay Ahuja
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid