Windows Develop Bookmark and Share   
 index > Windows Forms General > Listview images index problem
 

Listview images index problem

Hi,

I have a Listviewloaded withthumbnail images using background thread.It loads all the images from the directory.But when I select the image fromthe Listview to display into the user control, it won't display the correct image. It looks like the SelectedIndex problem. SelectedIndex doesn't work with first two images. What could be the problem? Thanks for any help.

string

[] files = System.IO.Directory.GetFiles(ImageFolder);


private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
try{
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 (Exception ex)
{
throw ex.InnerException;
}
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
try
{
//Get image.
ProgressInfo img = e.UserState as ProgressInfo;

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 (Exception ex)
{
throw ex.InnerException;
}
}

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);
}
anate  Wednesday, September 30, 2009 6:10 PM
The SelectedIndexChanged event handler as posted cannot work, it will always crash on SelectedItems[0] when you change selections. The event fires twice, first when the selected item becomes unselected, again when the new item becomes selected. I reckon this has something to do with your problem.

Hans Passant.
nobugz  Wednesday, September 30, 2009 6:55 PM
Hi anate,

You need to check if there are some items selected before do anything. This is a code snippet:
    private void listView_SelectedIndexChanged(Object sender, EventArgs e)
    {
        if (lstThumbNailView.SelectedItems.Count > 0)
        {
            ListViewItem lvi = lstThumbNailView.SelectedItems[0];
            string s = (string)lvi.Tag;
            this.viewer1.Image = Image.FromFile(s);
        }
    }


Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, October 02, 2009 9:00 AM
The SelectedIndexChanged event handler as posted cannot work, it will always crash on SelectedItems[0] when you change selections. The event fires twice, first when the selected item becomes unselected, again when the new item becomes selected. I reckon this has something to do with your problem.

Hans Passant.
nobugz  Wednesday, September 30, 2009 6:55 PM
Thanks for your help. What could be the best way to solve this issue?

anate  Wednesday, September 30, 2009 7:55 PM
Hi anate,

You need to check if there are some items selected before do anything. This is a code snippet:
    private void listView_SelectedIndexChanged(Object sender, EventArgs e)
    {
        if (lstThumbNailView.SelectedItems.Count > 0)
        {
            ListViewItem lvi = lstThumbNailView.SelectedItems[0];
            string s = (string)lvi.Tag;
            this.viewer1.Image = Image.FromFile(s);
        }
    }


Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, October 02, 2009 9:00 AM

You can use google to search for other answers

Custom Search

More Threads

• Max value in a excel range
• Newbie question: Displaying an n*2 string array in a scrollable table
• open form inside mdi
• Datagridview with Unbounded Checkbox Column Not Refresh C#
• How do I make the close button on a Form close down threads?
• 'PostMessage' in c#
• How do I set a ComboBox to ReadOnly?
• Colors & performance
• How to display Nikon Camera Raw image format ?
• Adding nodes to a treeview dynamically