2013-06-21 73 views
1

我試圖在TListView中放入一個圖標,當某些行顯示時,我的TImageList帶有加載的圖像,但它不連接。我的代碼是這樣的將圖標添加到TListView

with sListView2 do 
begin 
    test := sListView2.Items.Add; 
    test.Caption := sListbox2.Items[i]; 
    test.SubItems.Add(test'); 
    test.ImageIndex(ImageList1.AddIcon(1)); 
end; 

有人能告訴我我做錯了什麼嗎?

回答

2

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; 

BTW,您可以簡化代碼略(小心with,雖然!):

with sListView2.Items.Add do 
begin 
    Caption := sListbox2.Items[i]; 
    SubItems.Add(test'); 
    ImageIndex := 1; 
end; 
+0

感謝您的幫助。 – 14K