delphi - delphi 7中<listview.Clear>和<listview>.items.clear到底有什么区别?

标签 delphi

我想知道为什么有两种不同的方法来清除 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.ClearListView.Items.Clear 快约 16 倍当BeginUpdate/EndUpdate没用过!

关于delphi - delphi 7中<listview.Clear>和<listview>.items.clear到底有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10169896/

相关文章:

delphi - 为什么在应用程序关闭时释放类析构函数中的成员组件会导致 EInvalidPointer 错误?

delphi - 我可以在 Delphi 中组合对齐布局吗?

delphi - 如何实现IEnumerable<T>?

delphi - Delphi中应用程序最大化时组件的anchor属性

delphi - 如何仅支持 TLS 1.x(在我的网络服务中)?

Delphi在outlook msg中显示嵌入图像

delphi - Delphi或BDS IDE是否能够保存源代码断点?

javascript - 使用应用程序中嵌入的浏览器时,无法从 Android 上的网页访问地理位置

string - 为什么程序在分配字符串后崩溃

visual-studio - Delphi 等同于 Visual Studio 编码的 UI 测试?