想要取消选择有条件的物品。我注意到,当setselected时我得到了enxt循环的错误,因为索引在第一次循环后被更改,因此我决定刷新并再次进行每次循环,但是在取消选择一个项目后,我有第二个循环的invinitive循环。如何正确完成?列表框绑定到数据源。
sss:
For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
Dim variationswert1 As DataRowView = ListBox1.SelectedItems(i)
Dim name As String = ListBox1.GetItemText(variationswert1)
if name = "TR8" Then
ListBox1.SetSelected(i, False)
ListBox1.Refresh()
GoTo sss
End If
Next
编辑:(进一步讨论):
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox1.Items(i)) Then
MsgBox(ListBox1.GetItemText(ListBox1.Items(i)))
MsgBox(ListBox1.SelectedValue.ToString)
ListBox1.SetSelected(i, False)
End If
Next
ListBox1.Refresh()
答案 0 :(得分:0)
循环所有这些项目,不仅仅是选定的,可行的吗?
sss:
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox1.Items(i)) Then
Continue For
End If
Dim variationswert1 As DataRowView = ListBox1.Items(i)
Dim name As String = ListBox1.GetItemText(variationswert1)
if name = "TR8" Then
ListBox1.SetSelected(i, False)
ListBox1.Refresh()
GoTo sss
End If
Next
修改强>
使用每个,其余部分相同:
For Each item In ListBox1.Items
If Not ListBox1.SelectedItems.Contains(item) Then
Continue For
End If
Dim variationswert1 As DataRowView = item
Dim name As String = ListBox1.GetItemText(variationswert1)
If name = "TR8" Then
ListBox1.SetSelected(ListBox1.Items.IndexOf(item), False)
ListBox1.Refresh()
GoTo sss
End If
Next
简化它,我可能还没有理解它,但似乎你想要忽略所有列表框项目,它们的文本是“TR8”
For Each item In ListBox1.Items
If ListBox1.SelectedItems.Contains(item) And ListBox1.GetItemText(item) = "TR8" Then
ListBox1.SetSelected(ListBox1.Items.IndexOf(item), False)
ListBox1.Refresh()
End If
Next
编辑2:
所以没有为每个和
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox.Items(i)) And ListBox1.GetItemText(ListBox.Items(i)) = "TR8" Then
ListBox1.SetSelected(i, False)
ListBox1.Refresh()
End If
下一步
答案 1 :(得分:0)
您的问题是您正在迭代所选项目,但由于您要更改该数组,因此您将运行的任何循环最终都会失败:
For Each
将失败,因为您将修改集合For i = 0 to ...
将失败,因为您将移除一个项目,并以OutOfRangeException
(或类似的东西)结束这个想法是:
"TR8"
,请取消选中此项以下是代码:
Dim FoundItem As Object 'Or Whatever class the items are
For i = 0 to ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox1.Items(i)) AndAlso _
ListBox1.GetItemText(ListBox1.Item(i)) = "TR8" Then
FoundItem = ListBox1.Items(i)
ListBox1.SetSelected(i, False)
MsgBox(FoundItem.ToString())
End If
Next
ListBox1.Refresh()