【发布时间】: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.Index在TListView.Items.Count-TListView的范围内已经保证。跨度> -
@RemyLebeau,我已经将整个项目源码上传到dropbox.com/s/bsi7bh6xfkp6av3/Test4_1.zip?dl=0,在XP下可以显示错误。
标签: listview delphi windows-xp