【问题标题】:Deleting item in Firemonkey TListView causes out of range exception删除 Firemonkey TListView 中的项目导致超出范围异常
【发布时间】:2015-07-30 19:54:24
【问题描述】:

我有一个TListView 有几个项目,其中每个项目都有一个相应的“删除”按钮。 TListView 具有以下按钮单击事件处理程序:

void __fastcall TSettingsCopyWizard_Fac::CopyToListViewButtonClick(TObject * const Sender,
      TListViewItem * const AItem, TListItemSimpleControl * const AObject)

{
    CopyToListView->BeginUpdate();
    CopyToListView->Items->Delete(AItem->Index);
    CopyToListView->EndUpdate();
}

当我尝试删除项目时,会引发“参数超出范围”异常。但是,它不在Delete 调用中,而是在System.Generics.Collections 中:

procedure TListHelper.CheckItemRange(AIndex: Integer);
begin
  CheckItemRangeInline(AIndex); //this is the line that throws the exception
end;

我是否不正确地从列表视图中删除了一个项目?还是有其他原因导致此问题?

【问题讨论】:

  • 您通常不能从与该对象关联的事件处理程序内部销毁该对象。尝试使用TThread::Queue() 将删除延迟到OnButtonClick 处理程序退出之后。
  • @Remy Lebeau 不幸的是,TThread::Queue 立即执行,如果 CurrentThread == MainThread。我认为使用计时器更正确。
  • @kami 让我的按钮单击处理程序启用TTimer 以完美地删除项目!

标签: c++ firemonkey c++builder-xe8


【解决方案1】:

您拥有的代码是删除 ListView 中的项目的正确方法,因此肯定有其他问题。作为确认,这里有一些来自 Embarcadero 的示例,它们显示了正在使用这种技术:

此问题已在 C++ Builder 的新更新中得到纠正。我在东京和柏林亲自尝试过,可以确认效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    相关资源
    最近更新 更多