2011. május 11., szerda

Accelerate TListView


Problem/Question/Abstract:

The TListView control is very slow if it contains a large amount of items. How do you accelerate it?

Answer:

One possibility is to use the functions

TListview.AllocBy;
TListItems.BeginUpdate;
TListItems.EndUpdate;

This is still not enough for very large ListViews with more than 20.000 Items. Here it is better to use virtual ListViews :

1. Set Listview.OwnerData to "true"

2. Use the OnData-Event of the Listview-Component, e.g. :

procedure TForm.ListViewData(Sender: TObject; Item: TListItem);
var
  n: integer;
  s: string;
begin
  Item.SubItems.Clear;
  for n := 1 to FList.Count - 1 do
  begin
    s := tStrings(FList[n])[Item.Index];
    Item.SubItems.Add(s);
  end;
end;

Of course you can not use TListView.AlphaSort to sort this kind of ListView. If you want to sort it, you have to write your own sort procedure.

The same method can be applied to accelerate TListBox Components with a large number of items:
set TListBox.Style to lbVirtual and use the OnData Event.

Nincsenek megjegyzések:

Megjegyzés küldése