【问题标题】:What is the proper way to retrieve a listview items value in Delphi在 Delphi 中检索列表视图项值的正确方法是什么
【发布时间】:2012-07-21 02:34:17
【问题描述】:

现在我正在使用以下代码来获取 ListView 项目值,我想知道这是否是正确的方法,或者我应该以另一种方式来做。

父项值示例:

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;

【问题讨论】:

    标签: delphi tlistview


    【解决方案1】:

    您的第一个代码看起来不错,但您应该先检查是否有 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.Strings[0],您可以只使用MyStringList[0],这也适用于TMemo.LinesTListItem.SubItems 之类的东西。你不需要SubItems.Strings[Index],但可以使用SubItems[Index]

    【讨论】:

    • 赞赏,我正在我的生产代码中进行错误检查,我只是把上面的代码从我的脑海中抽了出来。
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多