2

There is a way to get all items selected with the mouse in a list view when virtual mode is enabled for this winform.

Example of an working code in use, I can retrieve only one selected file for now. Not too much examples finded on the web and could be identified as duplicate but is not conclusive for me, or the answer is to simple.

        private void FilesFoundList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        try
        {


            if (e.ItemIndex >= 0 && e.ItemIndex < ListFilesInfos.Count)
            {
                try
                {
                    var acc = ListFilesInfos[e.ItemIndex];

                   //with colors
                    e.Item = new ListViewItem(new string[] { acc.TagItem, acc.FileName, acc.FilePath.ToString() })
                   { Tag = acc, 
                     BackColor = SearchLabColor(0, Path.GetExtension(acc.FileName.ToString()), acc.FilePath.ToString(), acc.FileName.ToString()),
                     ForeColor = SearchLabColor(1, Path.GetExtension(acc.FileName.ToString()), acc.FilePath.ToString(), acc.FileName.ToString()),
                     UseItemStyleForSubItems = false
                   }; // Set Tag object property to our actual AccountInfo object
                }
                catch { this.Refresh(); }
            }
        }
        catch
        {

        }

    }

    private void ShowItemsVirtual(List<SearchFilesInfo> infos)
    {
        try
        {
            FilesFoundList.VirtualListSize = infos.Count; // Set number of items in list view
        }
        catch { this.Refresh(); }
    }

    private void FilesFoundList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {

        if (FilesFoundList.VirtualMode == true)
        {

            SelectedFiles.GlobalVar = (e.Item.SubItems[2]).Text.ToString() + (e.Item.SubItems[1]).Text.ToString();
        }
    }

Firing when clicked

busymind
  • 67
  • 13
  • The ItemSelectionChanged will not occur if the ListView is in virtual mode. you need to use `SelectedIndices` - https://stackoverflow.com/questions/3894222/cannot-access-the-selected-items-collection-when-the-listview-is-in-virtual-mode – Leo Dec 12 '18 at 16:44
  • Possible duplicate of [Cannot access the selected items collection when the ListView is in virtual mode?](https://stackoverflow.com/questions/3894222/cannot-access-the-selected-items-collection-when-the-listview-is-in-virtual-mode) – Leo Dec 12 '18 at 16:47
  • The ItemSelectionChanged is firing here when I click on item – busymind Dec 12 '18 at 16:47

2 Answers2

1

You could abbreviate your code to:

List<multiSearchSelect> multiSearchSelect = new List<multiSearchSelect>();

private void FilesFoundList_VirtualItemsSelectionRangeChanged(object sender, ListViewVirtualItemsSelectionRangeChangedEventArgs e)
{
    if (FilesFoundList.VirtualMode == true)
    {
        multiSearchSelect=
            FilesFoundList.SelectedIndices
            .Select(i=> new multiSearchSelect()
            { 
                fileName = FilesFoundList.Items[i].SubItems[1].Text, 
                filePath = FilesFoundList.Items[item].SubItems[2].Text
            });
    }
}

class multiSearchSelect
{
    public string fileName { set; get; }
    public string filePath { set; get; }
}
Leo
  • 5,013
  • 1
  • 28
  • 65
0

I will post my solution that fits to my purpose. I have added ItemsSelectionRangeChanged event and get the list of file selected.

List<multiSearchSelect> multiSearchSelect = new List<multiSearchSelect>();

private void FilesFoundList_VirtualItemsSelectionRangeChanged(object sender, ListViewVirtualItemsSelectionRangeChangedEventArgs e)
        {
            if (FilesFoundList.VirtualMode == true)
            {
                multiSearchSelect.Clear();

                ListView.SelectedIndexCollection col = FilesFoundList.SelectedIndices;
                if (col.Count > 1)
                {
                    foreach (int item in col)
                    {
                        multiSearchSelect.Add(new multiSearchSelect
                        {
                            fileName = FilesFoundList.Items[item].SubItems[1].Text,
                            filePath = FilesFoundList.Items[item].SubItems[2].Text
                        });
                    }
                }
            }
        }

class multiSearchSelect
    {
        public string fileName { set; get; }
        public string filePath { set; get; }
    }
busymind
  • 67
  • 13