Bulk updates

Another aspect of the same problem of overabundance of messages occurs when you want to add or modify multiple lines in a TListBox or TMemo. The demonstration program, BeginUpdate, demonstrates the problem and shows possible solutions.

Let's say we have a listbox and we want to populate it with lots of lines. The demo program displays 10,000 lines, which is enough to show the problem.

A naive program would solve the problem in two lines:

for i := 1 to CNumLines do  ListBox1.Items.Add('Line ' + IntToStr(i));

This, of course, works. It is also unexpectedly slow. 10,000 lines is really not much for modern computers so we would expect this code to execute very quickly. In reality, it takes 1.4 seconds on my test machine!

We could ...

Get Mastering Delphi Programming: A Complete Reference Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.