Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

A C# ColorListBox

Rate me:
Please Sign up or sign in to vote.
4.40/5 (22 votes)
29 May 2002 149.1K   3.2K   49   10
In this article we will see how to write an owner drawn ListBox

Sample Image - ColorListBox.gif

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,

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

Next add following lines below above line

C#
//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

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


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

 
PraiseGreat example; I used your example in my program Pin
Brad Soverns26-Feb-16 8:12
Brad Soverns26-Feb-16 8:12 
GeneralMy vote of 5 Pin
ChriSSi00313-Aug-14 21:00
ChriSSi00313-Aug-14 21:00 
GeneralMy vote of 3 Pin
MB Seifollahi10-Jun-12 1:07
professionalMB Seifollahi10-Jun-12 1:07 
GeneralColorListBox class Pin
brtnik7-Oct-08 22:27
brtnik7-Oct-08 22:27 
I have formalised the approach described by following class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MidiNames
{
	public class ColorListBox : ListBox
	{
		public List<color> ColorList;

		public ColorListBox()
		{
			ColorList = new List<color>();
			DrawMode = DrawMode.OwnerDrawFixed;
		}

		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();

			// Draw the current item text based on the current Font and the custom brush settings.
			if ((e.Index >= 0) && (e.Index < Items.Count)) {
				e.Graphics.DrawString(Items[e.Index].ToString(), e.Font,
					new SolidBrush(ColorList[e.Index]), e.Bounds, StringFormat.GenericDefault);
			}
			base.OnDrawItem(e);
		}

		public int Add(object item, Color color)
		{
			ColorList.Add(color);
			return Items.Add(item);
		}

		public void Clear()
		{
			ColorList.Clear();
			Items.Clear();
		}

		public void RemoveAt(int index)
		{
			ColorList.RemoveAt(index);
			Items.RemoveAt(index);
		}

		public void Remove(object item)
		{
			ColorList.RemoveAt(Items.IndexOf(item));
			Items.Remove(item);
		}

	}
}</color></color>


1) Any program has at least one error.
2) When corrected, point (1) remains valid

GeneralRe: ColorListBox class Pin
Rainer Gustin24-May-14 7:27
Rainer Gustin24-May-14 7:27 
GeneralAnother example Pin
brtnik7-Oct-08 1:06
brtnik7-Oct-08 1:06 
QuestionHow can I use XML resx file Pin
vicpark25-Nov-02 21:42
vicpark25-Nov-02 21:42 
AnswerRe: How can I use XML resx file Pin
Heath Stewart10-Dec-02 2:01
protectorHeath Stewart10-Dec-02 2:01 
GeneralVariable Geight ListBox-es Pin
wqw1-Jun-02 9:32
wqw1-Jun-02 9:32 
GeneralNice Pin
Nish Nishant30-May-02 17:11
sitebuilderNish Nishant30-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.