替换ImageList和ListView顶部的图像

时间:2013-07-03 02:28:40

标签: c# listview imagelist

我将在图像列表中加载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;      
    }

1 个答案:

答案 0 :(得分:0)

对于犯同样错误的人,我在替换功能中创建了一个新项目,这是不必要的。新的替换功能应如下所示

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