虚拟模式下的Listview-获取所选项目的列表

时间:2018-12-12 16:38:38

标签: c# listview

当为此Winform启用虚拟模式时,有一种方法可以用鼠标在列表视图中选择所有项目。

正在使用的工作代码示例,我现在只能检索一个选定的文件。在网上找不到太多示例,这些示例可以被识别为重复示例,但对我而言不是结论性的,或者答案很简单。

        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

2 个答案:

答案 0 :(得分:1)

您可以将代码缩写为:

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; }
}

答案 1 :(得分:0)

我将发布适合自己目的的解决方案。我已经添加了ItemsSelectionRangeChanged事件,并获取了所选文件的列表。

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; }
    }