Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

Need help fixing my code for detecting duplicates in my listview

I have a listview called lvStands. I need to have code that makes lblDup.visible = true when a duplicate item is found in the listview.

No errors are thrown but it does not find the duplicates either.

PBLack

    Public Sub dupStands()

        Dim x As Integer
        Dim y As Integer
        Dim boolDup As Boolean
        Dim diff As Integer = 0
        Dim lvArray As New ArrayList()
        ' Search in the listview
        For x = 0 To (lvStands.Items.Count - 1)
            boolDup = False
            ' Search in the array
            For y = 0 To (lvArray.Count - 1)
                If lvStands.Items(x - diff).Text = lvArray.Item(y) Then
                    boolDup = True
                    Exit For
                End If
            Next

            If booDup = True Then
                lblDup.Visible = True
            Else
                lblDup.Visible = False
            End If
        Next
    End Sub
Avatar of TreadHead
TreadHead
Flag of United States of America image

You don't seem to be populating the lvArray ArrayList, so its count will always be zero (hence your inner loop won't execute even once).

I think the following might suit:
 Public Sub dupStands()
 
        Dim x As Integer
        Dim y As Integer
        Dim boolDup As Boolean
        ' Search in the listview
        For x = 0 To (lvStands.Items.Count - 1)
            boolDup = False
            ' Search in the array
            For y = 0 To (lvStands.Count - 1)
                If x <> y AndAlso lvStands.Items(x).Text = lvStands.Item(y).Text Then
                    boolDup = True
                    Exit For
                End If
            Next
 
            If booDup = True Then
                lblDup.Visible = True
            Else
                lblDup.Visible = False
            End If
        Next
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TreadHead
TreadHead
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Todd MacPherson

ASKER

perfect

thanks