TImageList.ImageIndex是整数,需要正确设置,调用AddIcon需要提供TIcon。
如果您已经在TImageList 中有它,只需将TListView.ImageIndex 设置为该图像的正确索引:
// Assign an image from the ImageList by index
test.ImageIndex := 1; // The second image in the ImageList
或者,如果您在TImageList 中没有现有图标并且需要添加一个,请添加并存储来自AddIcon 的返回值:
// Create a new TIcon, load an icon from a disk file, and
// add it to the ImageList, and set the TListView.ImageIndex
// to the new icon's index.
Ico := TIcon.Create;
try
Ico.LoadFromFile(SomeIconFileName);
test.ImageIndex := ImageList1.Add(Ico);
finally
Ico.Free;
end;
顺便说一句,你可以稍微简化你的代码(不过要小心with!):
with sListView2.Items.Add do
begin
Caption := sListbox2.Items[i];
SubItems.Add(test');
ImageIndex := 1;
end;