I would like to know why are there 2 different ways of clearing out a listview. One is by calling listview.clear and other is listview.items.clear. Actually, this extends to many other VCL components too. which method must be used and why?
I would like to know why are there 2 different ways of clearing out
Share
ListView.Clearis just a wrapper aroundListView.Items.ClearwithListItems.BeginUpdate/ListItems.EndUpdate. look at the source:From the docs:
A better practice is to use
BeginUpdate/EndUpdatefor speed and avoiding flicker.But the main reason to use
ListView.Clearis because using a “high-level VCL methods” (As well commented by @Arnaud) is always a good idea, and the implementation might change (BTW, the method was introduced in D7).EDIT: I have tested the
TListViewwith 10k Items (D7/WinXP):ListView.Items.Clear: ~5500 msListView.Clear: ~330 msConclusion:
ListView.Clearis about 16 times faster thanListView.Items.ClearwhenBeginUpdate/EndUpdateis not used!