[英]What exactly is the difference between <listview.Clear> and <listview>.items.clear in delphi 7?
我想知道为什么有两种不同的清除列表视图的方法。 一个是通过调用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;
来自文档:
BeginUpdate方法暂停屏幕重绘,直到调用EndUpdate方法。 使用BeginUpdate加速处理,避免在将项目添加到集合或从集合中删除时闪烁。
更好的做法是使用BeginUpdate
/ EndUpdate
来提高速度并避免闪烁。
但使用ListView.Clear
主要原因是因为使用“高级VCL方法”(由@Arnaud评论)总是一个好主意,并且实现可能会改变(顺便说一句,该方法是在D7中引入的)。
编辑:我已经用10k项目(D7 / WinXP)测试了TListView
:
ListView.Items.Clear
:~5500 ms ListView.Clear
:~330 ms 结论: ListView.Clear
比快约16倍ListView.Items.Clear
时BeginUpdate
/ EndUpdate
不使用!
ListView.Clear
是一种在内部调用ListView.Items.Clear
的便捷方法。 无论你调用哪两个,都没有语义差异。
我更喜欢第一个,因为它更短,并且它没有显示内部表示,此时我对此毫无兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.