Mega Code Archive

 
Categories / Delphi / Examples
 

Listbox items add is slow and flickers

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?/a>: // method #1 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; // method #2 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;