我想知道为什么有两种不同的方法来清除 ListView 。一种是调用listview.clear
,另一种是listview.items.clear
。实际上,这也适用于许多其他 VCL 组件。必须使用哪种方法以及为什么?
最佳答案
ListView.Clear
只是 ListView.Items.Clear
的包装与 ListItems.BeginUpdate
/ListItems.EndUpdate
。看源码:
procedure TCustomListView.Clear;
begin
FListItems.BeginUpdate;
try
FListItems.Clear;
finally
FListItems.EndUpdate;
end;
end;
来自文档:
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 所评论的那样)始终是一个好主意,并且实现可能会改变(顺便说一句,该方法是在 D7 中引入的)。
编辑:我已经测试了TListView
包含 10k 项 (D7/WinXP):
-
ListView.Items.Clear
:~5500 毫秒 -
ListView.Clear
:~330 毫秒
结论:ListView.Clear
比 ListView.Items.Clear
快约 16 倍当BeginUpdate
/EndUpdate
没用过!
关于delphi - delphi 7中<listview.Clear>和<listview>.items.clear到底有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10169896/