Search code examples
delphilistviewdelphi-xe5

Background Item list view - Delphi


I would like to know how to paint items Listview.

My situation is as follows: I have a listview where every time you do a check and this check returns true, you have to change the listview line color. I saw examples by changing the color, but I cannot adapt to what I want.

procedure TForm1.OncustomDrawItem(Sender: TCustomListView; Item: TListItem;
  State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if corlistview then Begin
         LstbxDados.Canvas.Brush.Color:= RGB(0, 0, 0);
         corlistview := false;
       End;
end;

Procedure

var corlistview : boolean = false;

procedure carrega(t:String);
begin
           if beginNada then begin
                  corlistview :=  true;
            end;

                LstbxDados.Items.BeginUpdate;
                try
                  countX := countX +1;
                with LstbxDados.Items.Add do begin
                  Caption := IntToStr(i+1);
                  Subitems.add(title);
                  Subitems.add(url);
                end;
                finally
                  LstbxDados.Items.EndUpdate;
                end;
end;

How do I adapt the code for my situation?


Solution

  • Try this. I used random odd and even numbers for TListItem captions to emulate the function with boolean result that you have in your sample.

    procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
      var i:integer;
    begin
      i:= strtoint(Item.Caption);
      if i mod 2 =0 then
      begin
        Sender.Canvas.Brush.Color:=clNavy;
        Sender.Canvas.FillRect(Item.DisplayRect(TDisplayCode.drBounds));
      end;
    end;
    

    enter image description here