|
-
Jan 19th, 2009, 06:40 PM
#1
[RESOLVED] [2005] Avoiding ListView Duplicates
Im adding listviewitems and want to avoid duplication. Now currently, I have resorted to looping through the listview and comparing each items text property against what Im adding. But for a big table this is very slow. The ListView has a .Contains Method but this does not seem to do what I would have assumed; for example
Dim LV As New ListView
Dim I1 As New ListViewItem
I1.Text = "A"
Dim I2 As New ListViewItem
I2 = I1
LV.Items.Add(I1)
'This box will show
If LV.Items.Contains(I1) Then MessageBox.Show("Its present")
'and this box will show
If LV.Items.Contains(I2) Then MessageBox.Show("Its not there")
Is there a way of doing this so I dont have to loop through all the items?
-
Jan 20th, 2009, 01:53 AM
#2
Lively Member
Re: [2005] Avoiding ListView Duplicates
how are you filling the listview? you said table so are you using an sql statement? if so, use the distinct clause in your statement. like this:
sqlStatement = "select Distinct columns from table where clause"
I am using Microsoft Visual Basic 2008 Express Edition. I use Access for my data base
-
Jan 20th, 2009, 04:27 AM
#3
Re: [2005] Avoiding ListView Duplicates
Im just loading it with data read from a file. The data is actually email addresses and I need to check that I dont add duplicate entries.
-
Jan 20th, 2009, 04:31 AM
#4
Re: [2005] Avoiding ListView Duplicates
vb.net Code:
Dim addresses As String() = IO.File.ReadAllLines("file path here") For index As Integer = 0 To addresses.GetUpperBound(0) If Array.IndexOf(addresses, addresses(index)) = index Then 'This is the first occurrence of this address so add it to the ListView. End If Next
-
Jan 20th, 2009, 08:34 AM
#5
Re: [2005] Avoiding ListView Duplicates
Ah, very neat. Thanks JM.
-
Jan 20th, 2009, 08:39 AM
#6
Re: [RESOLVED] [2005] Avoiding ListView Duplicates
Slight addendum here, so what is the .Contains method intended for? It seems odd that there is no method that shows whether a ListViewItem for example matches a string?
I havent tried it, but I suspect the .Contains of an ArrayList does do that function.
-
Jan 20th, 2009, 07:07 PM
#7
Re: [RESOLVED] [2005] Avoiding ListView Duplicates
The Contains method of all collections always does the very same thing. It uses the Equals method of the objects to determine whether the object being tested is equal to any of the items in the collection. If it is then Contains returns True, otherwise it returns False. For essentially all reference types other than String, the Equals method tests reference equality, i.e. it tests whether two variables refer to the same object. The fact that two ListViewItems may have the same value for their Text property doesn't make them the same object, therefore they are not equal.
Try creating a new WinForms project and adding a ListView to the form, then add this code:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim item1 As New ListViewItem(New String() {"Column1", "Column2"})
Dim item2 As New ListViewItem(New String() {"Column1", "Column2"})
Dim item3 As ListViewItem = item1
Me.ListView1.Items.Add(item1)
MessageBox.Show(Me.ListView1.Items.Contains(item1).ToString(), "item1")
MessageBox.Show(Me.ListView1.Items.Contains(item2).ToString(), "item2")
MessageBox.Show(Me.ListView1.Items.Contains(item3).ToString(), "item3")
End Sub
When you run that code you'll see that the first message is True, which you'd expect because you added item1 to the ListView. The second message is False because, although item2 contains the same text as item1, they are two different objects and item2 was never added to the ListView. The third message is True because item3 refers to the same ListViewItem object as item1 and that object was added to the ListView.
Now change the code to this:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim item1 As New ListViewItem(New String() {"Column1", "Column2"})
Dim item2 As New ListViewItem(New String() {"Column1", "Column2"})
Dim item3 As ListViewItem = item1
Dim list As New ArrayList
list.Add(item1)
MessageBox.Show(list.Contains(item1).ToString(), "item1")
MessageBox.Show(list.Contains(item2).ToString(), "item2")
MessageBox.Show(list.Contains(item3).ToString(), "item3")
End Sub
Now you're adding the ListViewItems to an ArrayList instead of to the ListView (or, more correctly, the Listview's Items property, which is a ListViewItemCollection) and yet you get exactly the same behaviour from Contains.
It seems odd that there is no method that shows whether a ListViewItem for example matches a string?
In fact there is such a method. It's list in the documentation so it's not hidden. It's FindItemWithText. If you're only interested in a single column, which I think you are, then it will do the job. It won't work if you want to compare multiple fields with multiple fields though.
If you were using .NET 3.5 then you'd have far more options with LINQ and extension methods.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|