Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Avoiding ListView Duplicates

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [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?

  2. #2
    Lively Member
    Join Date
    Feb 2008
    Location
    Botswana
    Posts
    107

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Avoiding ListView Duplicates

    vb.net Code:
    1. Dim addresses As String() = IO.File.ReadAllLines("file path here")
    2.  
    3. For index As Integer = 0 To addresses.GetUpperBound(0)
    4.     If Array.IndexOf(addresses, addresses(index)) = index Then
    5.         'This is the first occurrence of this address so add it to the ListView.
    6.     End If
    7. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Avoiding ListView Duplicates

    Ah, very neat. Thanks JM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width