Count Items in a ListView

3

I need to count how many items I have added in a listview , I did the following:

LV1.Items.BeginUpdate;
 try
   for i := 0 to LV1.Items.Count-1 do
     Label11.Caption := inttostr(i+1);
 finally
  LV1.Items.EndUpdate;
 end;
end;

It works the most has a bug , when I add 2 ITEMS, it appears 2. When I turn off the 2 ITEMS instead of 0, "1" will appear. Any ideas?

    
asked by anonymous 20.04.2014 / 04:26

1 answer

1

You can count the items of a Listview by using Listview.Items.count ( as already mentioned in Motta Review ).

ShowMessage('Quantidade de Itens ' + IntToStr(ListView1.Items.Count));

While the problem related to the code you posted, the error is in:

Label11.Caption := inttostr(i+1); // O erro: i+1

Corrected code

var
  I: integer;
begin
  LV1.Items.BeginUpdate;
  try
    for i := 0 to LV1.Items.Count do
      Label1.Caption := IntToStr(i);
  finally
    LV1.Items.EndUpdate;
end;

But the best method to do this is to use Listview.Items.count .

    
20.04.2014 / 06:12