替换ImageList和ListView顶部的图像

本文关键字:图像 顶部 ListView ImageList 替换 | 更新日期: 2023-09-27 18:20:44

我将在imagelist和list View中加载10个图像。然后,我想替换前5个(列表中的前5个)。但新图像总是添加在显示列表的底部。

    Initialize_ListView()
    for (int i = 0; i < 10; i++) 
        {
           AddItemTo_ListView(i);
        }
Now I later use this code to replace the top 5
   for (int i = 0; i < 5; i++) 
        {
           ImageAndThumb imgObj = new ImageAndThumb(output);
           Replace(imgObj , i)
        }

   private void Initialize_ListView()
    {
        this.imageList1.Images.Clear();
        this.listView1.Clear();
        this.listView1.View = View.SmallIcon;
        this.imageList1.ImageSize = new Size(75, 80);
        this.listView1.SmallImageList = this.imageList1;
    }
    private void AddItemTo_ListView(int index)
    {
        ImageAndThumb imgObj = new ImageAndThumb(output);
        ImageAndThumbLst.Add(imgObj);
         this.imageList1.Images.Add(ImageAndThumbLst[index].Thumb);
        ListViewItem item = new ListViewItem();
        item.ImageIndex = index;
        this.listView1.Items.Add(item);
    }

    private void Replace(ImageAndThumb obj, int indx)
    {
        ImageAndThumbLst[indx] = obj;          
        this.imageList1.Images[indx] = ImageAndThumbLst[indx].Thumb;
        ListViewItem item = new ListViewItem();
        item.ImageIndex = indx;           
        this.listView1.Items[indx]=item;      
    }

替换ImageList和ListView顶部的图像

对于犯同样错误的人,我在替换函数中创建了一个新项,这是不必要的。新的替换功能应该看起来像这个

private void Replace(ImageAndThumb obj, int indx)
{
    ImageAndThumbLst[indx] = obj;          
    this.imageList1.Images[indx] = ImageAndThumbLst[indx].Thumb;       
     listView1.Refresh();   
}