This paper describes the item of Delphi implementation ListBox according to the content display different color method, the realization steps are as follows:
The ListBox1 Style property is changed to Lbownerdrawvariable
In the OnDrawItem event of the listbox, change the canvas property according to the item value
The sample code is as follows:
Procedure Tform1.listbox1drawitem (Control:twincontrol; Index:integer; Rect:trect; State:townerdrawstate);
Begin //font with the original default color
if ODD (index) then //When the index of items is odd, the color
begin
ListBox1. Canvas.brush.color:=clwindow;
ListBox1.Canvas.TextRect (Rect,rect. Left,rect.top,listbox1.items[index]);
end Else //When the index of items is even, the color
begin
ListBox1. Canvas.brush.color:=clinactivecaptiontext;
ListBox1.Canvas.TextRect (Rect,rect. Left,rect.top,listbox1.items[index]);
End; the If odselected in the state then //The selected timed color
begin
ListBox1. Canvas.brush.color:=clhighlight;
ListBox1.Canvas.TextRect (Rect,rect. Left,rect.top,listbox1.items[index]);
End;
End