这是一个简单的代码但是真的洗了我的大脑..移动到任何位置代码都无济于事。下面是在从combobox添加新项目之前检查listview中的每个项目。不幸的是只检查最后一项的工作。帮我。提前致谢
Dim checkType As String
Dim checkSubType As String
Dim TempNode As ListViewItem
For Each TempNode In ListView1.Items
checkSubType = TempNode.SubItems.Item(0).Text
checkType = TempNode.SubItems.Item(1).Text
Next
If ComboBox1.Text = checkSubType AndAlso ComboBox2.Text = checkType Then
With ListView1.Items.Add(ComboBox1.Text, 0)
.SubItems.Add(ComboBox2.Text)
.SubItems.Add("DUPLICATE")
End With
Else
With ListView1.Items.Add(ComboBox1.Text, 0)
.SubItems.Add(ComboBox2.Text)
.SubItems.Add("ADD NEW")
End With
End If
解决方案:
Dim total As Integer
If ListView1.View = View.Details AndAlso ListView1.Items.Count > 0 Then
'Dim lvi As ListViewItem = ListView1.FindItemWithText(ComboBox1.Text, True, 0)
Dim lva As ListViewItem = ListView1.FindItemWithText(ComboBox2.Text, True, 0)
If lva IsNot Nothing Then
'......
Else
'......
End If
Else
'......
End If
答案 0 :(得分:0)
问题出在For Each循环中。每次循环时,都会消除checkSubType和checkType中的旧值并在其上保存新值。这就是为什么只检查最后一项。
Dim checkType as List(Of String) = new List(Of String)
Dim checkSubType as List(Of String) = new List(Of String)
For Each TempNode In ListView1.Items
checkSubType.Add(TempNode.SubItems.Item(0).Text)
checkType.Add(TempNode.SubItems.Item(1).Text)
Next
此时,您现在有两个完整的项目列表,然后可以检查它们。
或者你可以试试这样的东西
Dim checkType As String
Dim checkSubType As String
Dim TempNode As ListViewItem
For value As Integer 0 To ListView1.Items.Count
checkSubType = TempNode.SubItems.Item(0).Text
checkType = TempNode.SubItems.Item(1).Text
If ComboBox1.Text = checkSubType AndAlso ComboBox2.Text = checkType Then
With ListView1.Items.Add(ComboBox1.Text, 0)
.SubItems.Add(ComboBox2.Text)
.SubItems.Add("DUPLICATE")
End With
Else
With ListView1.Items.Add(ComboBox1.Text, 0)
.SubItems.Add(ComboBox2.Text)
.SubItems.Add("ADD NEW")
End With
End If
Next