在Firemonkey TListView中删除项目会导致超出范围的异常

Deleting item in Firemonkey TListView causes out of range exception

本文关键字:范围 异常 TListView Firemonkey 删除项目      更新时间:2023-10-16

我有一个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();
}

当我尝试删除一个项时,抛出一个"Arguments out of range"异常。然而,它不在Delete调用上,而是在System.Generics.Collections中:

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

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

您拥有的代码是在ListView中删除项的正确方法,因此一定有其他错误。作为确认,以下是来自Embarcadero的一些例子,展示了这种技术的使用:

    东京
  • 柏林

这个问题在c++ Builder的新更新中得到纠正。我自己也在东京和柏林试过,可以确认效果很好。