2012-07-21 68 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

相关问题