Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Listview OnData事件接收到无效项。WinXP中的索引_Listview_Delphi_Windows Xp - Fatal编程技术网

Listview OnData事件接收到无效项。WinXP中的索引

Listview OnData事件接收到无效项。WinXP中的索引,listview,delphi,windows-xp,Listview,Delphi,Windows Xp,我正在WinXP下使用Delphi XE3 在以下代码中: procedure TForm1.Button1Click(Sender: TObject); var FileName: string; begin FFileList := TStringList.Create; for FileName in TDirectory.GetFiles(Edit1.Text) do FFileList.AddObject(FileName, nil); ListView1.

我正在WinXP下使用Delphi XE3

在以下代码中:

procedure TForm1.Button1Click(Sender: TObject);
var
  FileName: string;
begin
  FFileList := TStringList.Create;

  for FileName in TDirectory.GetFiles(Edit1.Text) do
    FFileList.AddObject(FileName, nil);

  ListView1.Items.Count := FFileList.Count;
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  I: Integer;
begin
  I := 0;
end;

procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
  //  The following error will never occur
  if Item.Index >= ListView1.Items.Count then
    Application.MessageBox('Invalid item index', 'Error');

  Item.Caption := 'TestCaption';
end;

我将ListView1.OwnerData设置为True,将ListView1.OwnerDraw设置为False

在XP下运行代码时,在TForm1.ListView1Data中,应用程序将弹出“无效项目索引”错误。为什么?

更新:

我已将整个项目的源代码上载到,它可以显示XP下的错误。

这表示加载WinXP主题时会出现类似的错误

似乎buggy库错误地计算了位置索引对应关系

解决方法-只需检查索引的有效性

if (Item.Index > -1) and (Item.Index < ListView1.Items.Count) then
begin
...
end
如果(Item.Index>-1)和(Item.Index
在启用和不启用主题的情况下,对我来说都很好。多年来,我一直在虚拟模式下使用
TListView
,包括在XP上,并且从未在
OnData
事件中从
Item.index
中获得错误的索引。而且我从来不用手动确保
Item.Index
TListView.Items.Count
-
TListView
的范围内,在调用
OnData
事件处理程序之前,我已经在内部保证了这一点。@RemyLebeau,我已经将整个项目的源代码上传到,它可以显示XP下的错误。