< listview.Clear>之间的区别究竟是什么和< listview> .items.clear在delphi 7中?
我想知道为什么有两种不同的清除列表的方法。一个是通过调用 listview.clear
而另一个是 listview.items.clear
。实际上,这也扩展到许多其他VCL组件。必须使用哪种方法,为什么?
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?
提前感谢
ListView.Clear
只是围绕 ListView.Items.Clear
与 ListItems.BeginUpdate
/ ListItems.EndUpdate
。看看来源:
ListView.Clear
is just a wrapper around ListView.Items.Clear
with ListItems.BeginUpdate
/ListItems.EndUpdate
. look at the source:
procedure TCustomListView.Clear;
begin
FListItems.BeginUpdate;
try
FListItems.Clear;
finally
FListItems.EndUpdate;
end;
end;
从文档:
BeginUpdate方法暂停屏幕重绘,直到调用EndUpdate
方法。使用BeginUpdate来加快处理速度,避免
闪烁,同时将项目添加到集合中或从集合中删除。
The BeginUpdate method suspends screen repainting until the EndUpdate method is called. Use BeginUpdate to speed processing and avoid flicker while items are added to or deleted from a collection.
更好的做法是使用 BeginUpdate
/ EndUpdate
以提高速度,避免闪烁。
但是使用的主要原因 ListView.Clear
是因为使用高级VCL方法(由@Arnaud评论)总是一个好主意,实现可能会改变(BTW,方法在D7中引入。
A better practice is to use BeginUpdate
/EndUpdate
for speed and avoiding flicker.
But the main reason to use ListView.Clear
is 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).
编辑:我已经测试了 TListView
与 10k 项目(D7 / WinXP):
I have tested the TListView
with 10k Items (D7/WinXP):
-
ListView.Items.Clear
:〜5500 ms -
ListView.Clear
:〜330 ms
-
ListView.Items.Clear
: ~5500 ms -
ListView.Clear
: ~330 ms
结论: ListView.Clear
比 ListView.Items.Clear
当 BeginUpdate
/ EndUpdate
不被使用!
Conclusion: ListView.Clear
is about 16 times faster than ListView.Items.Clear
when BeginUpdate
/EndUpdate
is not used!