DelphiFAQ Home Search:

ListBox.Items.Add is slow and flickers

 

commentsThis 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:

// 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;

Comments:

2007-02-16, 05:08:27
anonymous from Lithuania  
aa

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.