简体   繁体   中英

How do I get the index of a selected item in an image ListView?

I am writing a WinForms program for resizing images, in c#.

I have a ListView. The items in this ListView are images, from an ImageList.

The ImageList and ListView are populated when the user drags and drops images onto the form.

I also created two string arrays, imageFilePaths[ ] and imageFileNames[ ] (Which are pretty self-explanatory), which are populated at the same time as the ImageList and ListView.

As all four of these objects are populated through iteration in the dragDrop method, so the indexes of the ImageList , ListView , imageFilePaths[ ] and imageFileNames[ ] match up perfectly.

I have an event listener for the ListView. When an item in the ListView is clicked, I get the filename and file path from the previously mentioned arrays at index positions that match up with the ListView.SelectedItems indexes. Here's the code:

    private void imageListView_SelectedIndexChanged(object sender, EventArgs e)           
    {
        foreach (ListViewItem item in imageListView.SelectedItems)
        {
            int imgIndex = item.ImageIndex;
            if (imgIndex >= 0 && imgIndex < imageList1.Images.Count)
            {
                filenameTb.Text = imageFileNames[imgIndex];
                updateDimensions(imageFilePaths[imgIndex]);
            }
        }
    }

This works, but not as well as I'd like. If I have, for example, 20 images in the ListView and try to area-select the items by shift-clicking, it takes about 10-20 seconds for all of those to be highlighted. This is important to me, because I also have a 'Remove selected' button. It takes just as long to 'de-select' the items.

I am 95% sure that this is because this event listener is looping through every single item, displaying the dimensions and filename for each selected item until it gets to the last one, even though that is not necessary.

How could I re-write this so that I can get the index of only the selected item, or if multiple are selected, the index of the last one?

Thanks

EDIT : Based on comments, I've looked up the SelectedIndices property, and tried this:

    private void imageListView_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListView.SelectedIndexCollection indexes = this.imageListView.SelectedIndices;
        foreach (int index in indexes)
        {
            filenameTb.Text = imageFileNames[index];
            updateDimensions(imageFilePaths[index]);
        }
    }

It's still painfully slow however...

 foreach (ListViewItem item in imageListView.SelectedItems.Select((value, i) => new { i, value })
{
    //your code
}

Where i is the index and value the item

Instead of using SelectedIndexChanged event, try using ItemSelectionChanged. The event passed to that event handler gives you relevant item directly. No need to iterate.

        private void imageListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        e.Item ... <- this is your item
        e.ItemIndex ... <- this is your item's index
    }

不完全是我最初寻找的答案,但是我通过创建一个存储图像尺寸(x,y)的二维数组解决了选择图像缓慢的问题,而不是从图像路径中获取所选图像的尺寸,从将图像放到表单上时初始化的数组中获取它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM