【问题标题】:OnData event receives invalid Item.Index in WinXPOnData 事件在 WinXP 中接收到无效的 Item.Index
【发布时间】:2020-04-15 06:48:06
【问题描述】:

我在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中,app会弹出'Invalid item index'的错误。为什么?

更新:

我已经将整个项目源码上传到https://www.dropbox.com/s/bsi7bh6xfkp6av3/Test4_1.zip?dl=0,在XP下可以显示错误。

【问题讨论】:

  • 对我来说效果很好,无论是否启用主题。多年来,我一直在虚拟模式下使用TListView,包括在 XP 上,并且从未在OnData 事件中从Item.Index 获得错误索引。在调用OnData 事件处理程序之前,我从来不需要手动确保Item.IndexTListView.Items.Count - TListView 的范围内已经保证。跨度>
  • @RemyLebeau,我已经将整个项目源码上传到dropbox.com/s/bsi7bh6xfkp6av3/Test4_1.zip?dl=0,在XP下可以显示错误。

标签: listview delphi windows-xp


【解决方案1】:

link 表示加载 WinXP 主题时会出现类似错误。

似乎有缺陷的库错误地计算了位置索引对应关系。

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

if (Item.Index > -1) and (Item.Index < ListView1.Items.Count) then
begin
...
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2019-01-20
    相关资源
    最近更新 更多