C++ Builder Tutorials

C++Builder Tips: Speed up the display of list components

If you use a list component such as a TListBox or a TMemo, and you add or modify many items (lines, nodes,...), the visual response can becomes very slow. This is because after each change, the component is redrawn on the screen.

For example, modifying 10000 items of a TListBox causes 10000 redraws, and that takes from several seconds to several minutes, depending on the computer's speed and the complexity of the process.

But you can speed up things enormously with the following tip.

Call BeginUpdate before making the changes. When all changes are complete, call EndUpdate to show the changes on screen. BeginUpdate and EndUpdate prevent excessive redraws when items are added, deleted, or inserted.

int i;
// ...
ListBox1->BeginUpdate();
// add 10000 items:
for (i = 0; i < 10000; i++) {
  ListBox1->Items->Add("123456789");
}
ListBox1->EndUpdate;

To give you an idea of the improvement, we timed how long it takes to add 10000 items to a TListBox(your PC may be faster or slower):

»  without the BeginUpdate/EndUpdate lines: 4.3 seconds
»  with BeginUpdate/EndUpdate: 0.1 seconds, that's 43 times faster!