简体   繁体   English

如何获取图像ListView中所选项目的索引?

[英]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#. 我正在用c#编写WinForms程序来调整图像大小。

I have a ListView. 我有一个ListView。 The items in this ListView are images, from an ImageList. 此ListView中的项目是来自ImageList的图像。

The ImageList and ListView are populated when the user drags and drops images onto the form. 当用户将图像拖放到窗体上时,将填充ImageList和ListView。

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. 我还创建了两个字符串数组,imageFilePaths []和imageFileNames [](这很容易解释),它们与ImageList和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. 由于所有这四个对象都是通过dragDrop方法中的迭代填充的,因此ImageListListViewimageFilePaths []imageFileNames []的索引完美匹配。

I have an event listener for the ListView. 我有一个用于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. 单击ListView中的项目时,我从前面提到的数组中与ListView.SelectedItems索引匹配的索引位置获取文件名和文件路径。 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. 例如,如果我在ListView中有20张图像,并尝试通过按住Shift键并单击来选择区域,则将所有这些图像突出显示大约需要10-20秒。 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. 我有95%的把握,这是因为此事件侦听器正在遍历每个单个项目,显示每个选定项目的尺寸和文件名,直到到达最后一个为止,即使这不是必需的。

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: 编辑:基于注释,我查找了SelectedIndices属性,并尝试了以下操作:

    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. 而不是使用SelectedIndexChanged事件,请尝试使用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)的二维数组解决了选择图像缓慢的问题,而不是从图像路径中获取所选图像的尺寸,从将图像放到表单上时初始化的数组中获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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