Listview SelectedIndexChanged问题

时间:2009-09-30 13:52:03

标签: c#

我有一个Listview并加载缩略图。我想选择缩略图图像以将实际图像显示到用户控件中。当我选择第一个图像时,它不会显示在用户控件中。但是,当我选择第三张图像时,它工作正常。以下代码有什么问题?

            string pname;
            Image myImage;
           for (i = 0; i <= files.Length ; i++)
            {
                pname = System.IO.Path.GetFullPath(files[i]);
                myImage = Image.FromFile(pname);
                ImgListView.Images.Add(getThumbnaiImage(ImgListView.ImageSize.Width, myImage));
                myImage = null;
            }


          for (i = 0; i <= files.Length ; i++)
            {
                fname = System.IO.Path.GetFileName(files[i]);
                ListViewItem lvwItem = new ListViewItem(fname, i);
                lvwItem.Tag = files[i];

                lstThumbNailView.Items.AddRange(new ListViewItem[] { lvwItem });
                lvwItem = null;
            }

private void listView_SelectedIndexChanged(Object sender, EventArgs e)
{
   ListView.SelectedListViewItemCollection selectedItems = lstThumbNailView.SelectedItems;

        ListViewItem lvi = lstThumbNailView.SelectedItems[0];
        string s = (string)lvi .Tag;
        this.viewer1.Image = Image.FromFile(s);

}

2 个答案:

答案 0 :(得分:2)

循环不应该是:

      for (i = 0; i < files.Length ; i++)

这是具有Length属性的数组/集合的标准模式。

答案 1 :(得分:0)

这是我想要做的。

private void backgroundWorker_DoWork(object sender,DoWorkEventArgs e)         {            尝试{            int progress = 0;

        string pname;
        Image myImage;
        max_length = files.Length - 2;

        for (i = 0; i < files.Length; i++)
        {
          ProgressInfo info = new ProgressInfo();

            pname = System.IO.Path.GetFullPath(files[i]);
            myImage = Image.FromFile(pname);
            info.Image = myImage;
            info.ImageIndex = i;
            backgroundWorker1.ReportProgress(progress, info);
            myImage = null;
        }

       }
       catch (TargetInvocationException tiEx)
       {
           throw tiEx.InnerException;
       }
       catch (Exception ex)
       {
           throw ex.InnerException;
       }
    }


  private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        try
        {
             ProgressInfo img = e.UserState as ProgressInfo;
            //Set image to ListView here.
            ImgListView.Images.Add(getThumbnaiImage(ImgListView.ImageSize.Width, img.Image));
            fname = System.IO.Path.GetFileName(files[img.ImageIndex]);
            ListViewItem lvwItem = new ListViewItem(fname, img.ImageIndex);
            lvwItem.Tag = files[i];

            lstThumbNailView.Items.AddRange(new ListViewItem[] { lvwItem });

        }
        catch (TargetInvocationException tiEx)
        {
            throw tiEx.InnerException;
        }
        catch (Exception ex)
        {
            throw ex.InnerException;
        }
    }

class ProgressInfo
{
    public Image m_Image;
    public int m_ImageIndex;
          public Image Image
    {
        set { m_Image = value; }
        get { return m_Image; }
    }

    public int ImageIndex
    {
        set { m_ImageIndex = value; }
        get { return m_ImageIndex; }
    }
}