How to change the color of text in a column in TTNTListView?

I am using TTNTListView in Delphi 7. It is installed on vsReport. On the OnCustomDrawSubItem event, I use this code:

procedure TForm.ListViewCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean); begin if SubItem = 2 then if (Item.SubItems.Strings[1] = Text1) or (Item.SubItems.Strings[1] = Text2) then Sender.Canvas.Font.Color := clGreen else Sender.Canvas.Font.Color := clRed; end; 

The problem is that all subelements> = 3 are drawn with the same color as subelement 2. I checked for SubItem> = 3 Sender.Canvas.Font.Color - clBlack, but they are drawn using clRed and clGreen. If this is a problem in my code, please show me how to fix it. If this is a mistake, maybe someone knows a workaround. Thanks.

+2
source share
1 answer

I would suggest that you just need to explicitly set the color for other cases. Since you are not doing this, the state of the canvas is preserved. Try the following:

 procedure TForm.ListViewCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean); var Color: TColor; begin if SubItem = 2 then if (Item.SubItems.Strings[1] = Text1) or (Item.SubItems.Strings[1] = Text2) then Color := clGreen else Color := clRed; else Color := clBlack; Sender.Canvas.Font.Color := Color; end; 
+1
source

Source: https://habr.com/ru/post/1399434/


All Articles