In this article we will see how to write owner drawn
control. Typically, Windows handles the task of drawing the items to display in
ListBox. You can use the
property and handle the
events to provide the ability to override the automatic drawing that Windows
provides and draw the items yourself. You can use owner-drawn
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
to the form and set its
DrawMode property to
Alternatively you can add following line to the
function of your form,
this.lstColor.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
Next add following lines below above line
this.lstColor.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);
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)
private void MeasureItemHandler(object sender, MeasureItemEventArgs e)
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 firstname.lastname@example.org
Sanjay Ahuja is a Bachelor of Engineer and has completed his CDAC in Pune,India. He is currently working as a consultant for Verizon.