473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Listview Items help

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

Nov 21 '05 #1
1 2760
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
"OpticTygre" <op********@adelphia.net> wrote in message
news:-8********************@adelphia.com...
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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J_Max | last post by:
Hello, This might be a really easy question, but... I am developing a simple Smart Device application that uses a listview. I have a function that adds a item to the listview - code is below. I...
2
by: Anushya devi | last post by:
Hi All I used listview and tried to update it by using Addrange. When the number of items is less, it works fine.. But I need to update nearly 200,000 items and it hangs. Also i need to add...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
3
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
6
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the...
1
by: samoore33 | last post by:
I found this code on MSDN, and it works great. It creates a ListView dynamically and add items to it and all. It is great. I have changed a few of the column names to suit me. Dim listView1 As...
0
by: inandout | last post by:
Hi, I was wondering if anyone can help me with this. I have a listview that is specified to a size of: 348W x 308H When items are added to it, no problems. However when items continue to be...
7
by: Brad Pears | last post by:
I have something strange going on - pretty sure it used to work before - and now it does not... Why does the following code not clear a combo box? Me.cboLocation.Text = String.Empty OR ...
2
by: witpo | last post by:
Hi, I would like to display all listview items in one row with scroll bar below it – instead of multiple rows and scroll bar on the right. Someone told me that I can achieve it using...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.