Listview Items help

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
 
L

Lloyd Sheen

I think you are confusing an object with identical data, with the same
object. Each listview contains a listviewitem with the same information but
they are not the same listviewitem. To test you can use whatever columns in
the listview uniquely identify that listviewitem.

What you are asking in the contains is whether the listview contains the
listviewitem that is actually contained in another listview. Since a
listviewitem can only exist in one listview's item collection it will alway
return false.

Hope this helps.

Lloyd Sheen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top