0
Use owner-drawing. This example draws every other line in a different color.
Create a windows form and add a button and a listbox. In the button click event, put this code:
for (int j = 0; j < 10; j++)
listBox1.Items.Add("Item " + j.ToString());
Set the listbox DrawMode property to OwnerDrawFixed. This causes the DrawItem event to get fired when an item needs to be rendered. In the DrawItem event, put this code.
//Determine characteristics of the current item.
int lIndex = e.Index;
string lItemText = (string)listBox1.Items[lIndex];
Rectangle lItemBounds = e.Bounds;
//We need a Graphics object that we can draw upon using GDI+ functions.
Graphics canvas = e.Graphics;
//draw the background.
Brush lBrush = Brushes.LightSeaGreen;
canvas.FillRectangle(lBrush, lItemBounds);
//every other line will alternate text color
Font lFont = new Font("courier new", 8, FontStyle.Italic);
if ((lIndex / 2) * 2 == lIndex)
lBrush = Brushes.Purple;
else
lBrush = Brushes.OliveDrab;
//draw the text
canvas.DrawString(lItemText, lFont, lBrush, lItemBounds);
