Click here to Skip to main content
Click here to Skip to main content

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 -->

Introduction

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.

Description

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)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
    e.Graphics.DrawString(data[e.Index], 
                          new Font(FontFamily.GenericSansSerif, 
                                   14, FontStyle.Bold),
                          new SolidBrush(color[e.Index]), 
                          e.Bounds);
 
}

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 lparam@hotmail.com

License

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

Share

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.

Comments and Discussions

 
GeneralNice PinmemberNish - Native CPian30-May-02 17:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.141030.1 | Last Updated 30 May 2002
Article Copyright 2002 by Sanjay Ahuja
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid