Delphi .NET (2) Database (71) Delphi IDE (90) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
ListBox.Items.Add is slow and flickers
This article has not been rated yet. After reading, feel free to leave comments and rate it.
Adding a (larger) group of entries to a ListBox is very slow,
because after every "items.add" call the ListBox is repainted.
There are two ways to overcome this:
- use the Windows message WM_SETREDRAW (see Win32.hlp for details).
The VCL provides two methods for this: BeginUpdate and EndUpdate.
I would assume that this is faster than method #2.
- read the strings in a temporary TStringList object.
Maybe you already have such a list - in this case you should use your existing list.
Then use the Assign method to transfer the whole list.
Some sample code after a contribution from Björn:
 | |  | |
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin
ListBox1.Items.BeginUpdate;
for i:=1 to maxitems do
ListBox1.Items.add(IntToStr(i));
ListBox1.Items.EndUpate;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
tmp: tstringlist;
begin
tmp:=TStringList.Create;
for i:=1 to maxitems do
tmp.add(inttostr(i));
ListBox1.Items.Assign(tmp);
tmp.Free;
end;
| |  | |  |
Comments:
|