I'm using a TListView to display some of my entered data.
I've had working for ages this small procedure to colour the rows alternately:
Code:
// OnDrawing ListView1, colour the rows alternately
procedure TMainForm.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
with ListView1.Canvas.Brush do
begin
if Item.Index mod 2 = 0 then Color := $FFFFFF else Color := $F6F4F4;
end;
end;
Now I'm trying to highlight certain sub-items in red, when they are the first instance entered, as so:
[highlight #FFFFFF]18:20 Harry Fish 20Kg[/highlight]
[highlight #F6F4F4]18:22 Harry Cheese 5Kg[/highlight]
[highlight #FFFFFF]19:01 Fred Fish 3Kg[/highlight]
[highlight #F6F4F4]19:02 John Fish 5Kg[/highlight]
[highlight #FFFFFF]19:07 Fred Milk 2l[/highlight]
[highlight #F6F4F4]19:13 Harry Milk 5l[/highlight]
As the rows of data are entered, or read from file, a line at a time, I'm setting a flag for each entry type which would indicate whether it should be coloured red or not. The flag is set true or false each time to suit.
I'm trying to colour the text with this procedure:
Code:
procedure TMainForm.ListView1AdvancedCustomDrawSubItem(
Sender: TCustomListView; Item: TListItem; SubItem: Integer;
State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean);
begin
with ListView1.Canvas.Font do
begin
if (SubItem = 1) and (NewNameFlag) then Color := clRed else Color := clBlack;
if (SubItem = 2) and (NewItemFlag) then Color := clRed else Color := clBlack;
end;
end;
However, I find this isn't quite working, and I can't get the combination of procedures to work in harmony.
Firstly, the new ListView1AdvancedCustomDrawSubItem procedure has broken the previously working ListView1CustomDrawItem procedure, so now only the caption is coloured alternately.
Also the ListView1AdvancedCustomDrawSubItem procedure is colouring the entire column, as if the flag is always true.
It may be worth saying that I am using ListView1.Items.BeginUpdate and EndUpdate when I load the data from file, though this has never affected the alternate row colouring.
Steve (Delphi 2007 & XP)