I understand that using BeginUpdate and EndUpdate on VCL controls such as TListBox speeds up the process of populating the control with Items as it prevents the control from being repainted, until EndUpdate is called.
Example:
procedure TForm1.AddItems;
var
i: Integer;
begin
Screen.Cursor := crHourGlass;
try
for i := 0 to 5000 do
begin
ListBox1.Items.Add('Item' + IntToStr(i));
end;
finally
Screen.Cursor := crDefault;
end;
end;
The above will have a delay because the Listbox is allowed to be repainted, but the delay can be shorted by preventing repainting like so:
procedure TForm1.AddItems;
var
i: Integer;
begin
Screen.Cursor := crHourGlass;
try
ListBox1.Items.BeginUpdate;
try
for i := 0 to 5000 do
begin
ListBox1.Items.Add('Item' + IntToStr(i));
end;
finally
ListBox1.Items.EndUpdate;
end;
finally
Screen.Cursor := crDefault;
end;
end;
Now I tested this using a TStringList:
procedure TForm1.AddItems;
var
SL: TStringList;
i: Integer;
begin
SL := TStringList.Create;
try
Screen.Cursor := crHourGlass;
try
SL.BeginUpdate;
try
for i := 0 to 5000 do
begin
SL.Add('Item' + IntToStr(i));
end;
finally
SL.EndUpdate;
end;
ListBox1.Items.Assign(SL);
finally
Screen.Cursor := crDefault;
end;
finally
SL.Free;
end;
end;
It seems that regardless if the TStringList uses BegindUpdate and EndUpdate, the list is populated at approximately the same speed..
Are they really needed though as the TStringList is performed in memory and not visually. Should I use BeginUpdate and EndUpdate on a TStringList anyway, is it good practice to do this?
I feel silly for asking this but why does the TStringList have the procedures BeginUpdate and EndUpdate?
I think I may have answered my own question here, either way I would like to hear your views.
Thanks :)