-1

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?

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
abcd
  • 441
  • 6
  • 24
  • What do you mean by "do a check"? And what do you mean by "line color"? What lines do you mean? You mean item background color? – Jerry Dodge Aug 09 '15 at 18:28
  • I do a check with `IF true then else` ( " do the check " ) . If true my validation , I have to change the ITEM color ( " Color line "), but I do not understand how to do this . For the way I do it paints all items ATT – abcd Aug 09 '15 at 18:38
  • What are you checking? What's the source of information which might return either true or false for each item? In any case, using a global variable for this information is definitely not the way to go. – Jerry Dodge Aug 09 '15 at 18:51

1 Answers1

2

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

asd-tm
  • 3,381
  • 2
  • 24
  • 41
  • I could do using a LOOP (FOR) . `if item.SubItems [i ] = ' ' then begin` ... Verifying a SubItem with space ... – abcd Aug 09 '15 at 22:04
  • 1
    You can also use `for ... in Item.Subitems`. You can use `Pos` function instead. That depends upon what you wish. It would be the second question. You can also accept this answer (ticking V) if it suits you and responds to the current question _How to paint items_. – asd-tm Aug 10 '15 at 06:29