ListView 处于虚拟模式时无法访问所选项目集合?

发布于 2024-09-26 21:13:37 字数 296 浏览 10 评论 0原文

我有一个处于虚拟模式的 ListView。我想访问 SelectedItems 属性。
但是当我使用 ListView1.SelectedItems 时,我收到以下异常:

Cannot access the selected items collection when the ListView is in virtual mode

如何在 VirtualMode 中访问 ListView1.SelectedItems

I have a ListView in Virtual Mode. I wanna to access SelectedItems property.
But when I use ListView1.SelectedItems , I receive the following Exception :

Cannot access the selected items collection when the ListView is in virtual mode

How can I access to ListView1.SelectedItems in VirtualMode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

So要识趣 2024-10-03 21:13:37

这是很旧的帖子,但也许其他人会受益。

只需使用ListView.SelectedIndexCollection col = listView.SelectedIndices;
然后您可以访问一个项目:

forearch(var item in col)
{
   string txt = listView.Items[item].Text;
}

..但是您将无法使用 foreach 迭代 ListView.Items,因为在此模式下没有可用的迭代器。使用索引器运行得很好:-)

当尝试使用 foreach 时,你会得到一个异常:

当ListView处于虚拟模式时,无法通过
ListView 项目集合使用枚举器或调用 GetEnumerator。
请改用 ListView 项目索引器并按索引访问项目
值。

It is quite old post but maybe someone else will benefit.

Simply use ListView.SelectedIndexCollection col = listView.SelectedIndices;
Then you can access an item:

forearch(var item in col)
{
   string txt = listView.Items[item].Text;
}

..but you won't be able to iterate through ListView.Items using foreach because there is no iterator available in this mode. Using indexer is just flying fine :-)

When trying to use foreach you get an exception:

When the ListView is in virtual mode, you cannot enumerate through the
ListView items collection using an enumerator or call GetEnumerator.
Use the ListView items indexer instead and access an item by index
value.

菩提树下叶撕阳。 2024-10-03 21:13:37

来自 docs

在虚拟模式下, Items 集合被禁用。尝试访问它会导致 InvalidOperationException。 CheckedItems 集合和 SelectedItems 集合也是如此。如果要检索选定或选中的项目,请改用 SelectedIndices 和 CheckedIndices 集合。

From the docs

In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.

写下不归期 2024-10-03 21:13:37

我将所有项目存储在列表中,并使用此列表在 RetrieveVirtualItem 中提供项目
您可以找到如下所示的选定项目

Dim lstData As List(Of ListViewItem) = New List(Of ListViewItem)
Dim lstSelectedItems As List(Of ListViewItem) = lstData.FindAll(Function(lstItem As ListViewItem) lstItem.Selected = True)
Me.Text = lstItems.Count.ToString()

I you store all items in list and use this list to give item in RetrieveVirtualItem
you can find selected items like following

Dim lstData As List(Of ListViewItem) = New List(Of ListViewItem)
Dim lstSelectedItems As List(Of ListViewItem) = lstData.FindAll(Function(lstItem As ListViewItem) lstItem.Selected = True)
Me.Text = lstItems.Count.ToString()
命比纸薄 2024-10-03 21:13:37

我已经通过以下代码完成了此操作,但是当选择多个项目时会出现异常:

索引超出范围。必须为非负数且小于集合的大小。
参数名称:索引

List<ListViewItem> ListViewItems = new List<ListViewItem>();

foreach (int index in listView1.SelectedIndices)
{
    ListViewItem SelectedListViewItem = listView1.Items[index];  // exception
    ListViewItems.RemoveAt(index);
}
…

void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    e.Item = ListViewItems[e.ItemIndex];
}

每当从集合中删除项目时,始终从最大索引迭代到最小索引,如下所示:

for (int index = listView1.SelectedIndices.Count - 1; i >= 0; i--)
{
    …
}

这是因为每次删除集合中的项目时,索引都会改变如果不从最大索引到最小索引进行迭代。

I've done it by the following code, but it has an exception when more than one item are selected:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

List<ListViewItem> ListViewItems = new List<ListViewItem>();

foreach (int index in listView1.SelectedIndices)
{
    ListViewItem SelectedListViewItem = listView1.Items[index];  // exception
    ListViewItems.RemoveAt(index);
}
…

void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    e.Item = ListViewItems[e.ItemIndex];
}

Whenever you remove item(s) from a collection, always iterate from the largest index to the smallest index, like this:

for (int index = listView1.SelectedIndices.Count - 1; i >= 0; i--)
{
    …
}

This is because every time you remove an item in a collection, the index will change if you do not iterate from the largest to the smallest index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文