Mega Code Archive

 
Categories / Delphi / Graphic
 

How to alternate the color of TListbox lines

Title: How to alternate the color of TListbox lines? Question: Do you ever needed to do a zebra-effect with a TListbox? Well, it's a quick and simple solution. Answer: You just have to set your TListBox.Style to, for example lbOwnerDrawFixed, and put the following code on the OnDrawItem event: with TListBox(Control).Canvas do begin // Draw odd items with another color if Odd(Index) and not (odSelected in State) then Brush.Color := clSilver; FillRect(Rect); TextOut(Rect.left, Rect.top, TListBox(Control).Items[Index]); end;