【问题标题】:Add icon to TListView将图标添加到 TListView
【发布时间】:2013-06-18 00:37:45
【问题描述】:

当某些行显示并且我有带有加载图像的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;

谁能告诉我我做错了什么?

【问题讨论】:

    标签: delphi


    【解决方案1】:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2013-12-06
      • 2019-01-20
      相关资源
      最近更新 更多