Counting Items from a Listview

Asked

Viewed 1,913 times

3

I need to count how many items you have added in one 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 more has a bug, when I add 2 ITEMS, 2 appears. When I delete the 2 ITEMS instead of 0, "1" appears. Any ideas?

  • I don’t understand, you already have the LV1.Items.Count method ...

1 answer

1


You can count the items of a Listview through the use of Listview.Items.count(as already mentioned in comment from Motta).

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

While to code related problem posted by you, 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 the Listview.Items.count.

  • So friend I tested this way INTTOSTR(i);, but there is a problem, only after it appears 2 ITEMS in LISTVIEW that it starts counting from 1, understand ? If only 1 ITEM appears, continue at 0.

  • Solved friends, follows the command for those who need: Label11.Caption := Inttostr(LV1.Items.Count); counts perfectly.... thanks to all for the help!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.