O
OpticTygre
I have a listview where users can add items via a textbox and an 'add'
button. The problem is, the code below doesn't work for keeping duplicates
entered. If I create a ListViewItem with the same data in it as another
ListViewItem, why aren't those two items considered equal? For example, a
Debug.Writeline(lvItem1.Equals(lvItem2)) would return 'false' even if both
ListViewItems contained the same data.
Thanks for any help.
-Jason
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim Address As IPAddress
If txtIP.Text <> "" Then
If IPAddress.TryParse(txtIP.Text, Address) = True Then 'Add to
list
Dim lvItem As New ListViewItem(txtIP.Text)
lvItem.SubItems.Add(txtIP.Text)
lvItem.SubItems.Add("Not Tested")
lvItem.Checked = True
If Not lvIP.Items.Contains(lvItem) Then
lvIP.Items.Add(lvItem)
Else
MessageBox.Show("IP Address is already in the list.",
"Duplicate Item", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageBox.Show("Not a valid IP Address.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageBox.Show("You must enter an IP Address.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
button. The problem is, the code below doesn't work for keeping duplicates
entered. If I create a ListViewItem with the same data in it as another
ListViewItem, why aren't those two items considered equal? For example, a
Debug.Writeline(lvItem1.Equals(lvItem2)) would return 'false' even if both
ListViewItems contained the same data.
Thanks for any help.
-Jason
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim Address As IPAddress
If txtIP.Text <> "" Then
If IPAddress.TryParse(txtIP.Text, Address) = True Then 'Add to
list
Dim lvItem As New ListViewItem(txtIP.Text)
lvItem.SubItems.Add(txtIP.Text)
lvItem.SubItems.Add("Not Tested")
lvItem.Checked = True
If Not lvIP.Items.Contains(lvItem) Then
lvIP.Items.Add(lvItem)
Else
MessageBox.Show("IP Address is already in the list.",
"Duplicate Item", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageBox.Show("Not a valid IP Address.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageBox.Show("You must enter an IP Address.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub