2012-07-21 34 views
1

現在我正在使用下面的代碼來獲取ListView項目的值,我想知道這是否是正確的方法來做到這一點,或者我應該做的這是另一種方式。什麼是在Delphi中檢索列表視圖項值的正確方法

實例父項值:

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ShowMessage(ListView1.Selected.Caption); 
end; 

舉例子項值:

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ShowMessage(ListView1.Selected.SubItems.Strings[items_index_here]); 
end; 

回答

8

你的第一個代碼看起來正常,但你應該檢查,看看是否有第一個Selected項目:

if Assigned(ListView1.Selected) then // or ListView1.Selected <> nil 
    ShowMessage(ListView1.Selected.Caption); 

你的第二可被簡化(並應包括同一校驗我如上所述):

if Assigned(ListView1.Selected) then 
    ShowMessage(ListView1.Selected.SubItems[Index]); 

TStrings後代(如TStringListTListItem.SubItems)具有默認屬性,這是一個快捷方式使用TStrings.Strings[Index];你可以改爲使用TStrings[Index]。您可以使用MyStringList[0]而不是MyStringList.Strings[0],這也適用於TMemo.LinesTListItem.SubItems之類的內容。你不需要SubItems.Strings[Index],但可以使用SubItems[Index]

+0

已經確認了,我正在做我的生產代碼中的錯誤檢查,我只是把上面的代碼從頭頂上扯下來了。 – avue 2012-07-21 03:00:22

相關問題