验证重复ListViewItems的文本框

时间:2013-05-29 18:24:37

标签: vb.net validation listview

我有4个用于填充ListView的文本框。我需要第4个文本框来验证数据是否已经在列表中。

2 个答案:

答案 0 :(得分:0)

您需要遍历列表并验证每个单元格。我不确定你是如何提交你的物品的,但我认为它是在Button_Click事件中。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'I'll store "abc" in TextBox4.Text for my example
    TextBox4.Text = "abc"

    'First we loop through each listview item (or row)
    For Each item As ListViewItem In ListView1.Items
        'For each item, we will loop through it's subitems
        For Each subItem As ListViewItem.ListViewSubItem In item.SubItems
            'If textbox4.text is equal to the current subitem, we've found that the item already exists
            If Textbox4.Text = subItem.Text Then
                MsgBox("Item abc is already in the list")
            End If
        Next
    Next
End Sub

答案 1 :(得分:0)

您也可以使用Find方法ListView1.Items.Find("myString", True)。这将搜索每个项目及其所有子项目。

不是直接调用按钮单击处理程序,而是使用特定按钮的PerformClick方法可能更好。