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
PBLackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TreadHeadCommented:
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

0
TreadHeadCommented:
Some cleanup and correction yields the following:
Public Sub dupStands()
	Dim x As Integer
	Dim y As Integer
 
	lblDup.Visible = False
 
	' Search in the listview
	For x = 0 To (lvStands.Items.Count - 1)
		' Search in the array
		For y = 0 To (lvStands.Items.Count - 1)
			If x <> y AndAlso lvStands.Items(x).Text = lvStands.Item(y).Text Then
				lblDup.Visible = True
				Exit For
			End If
		Next
		If lblDup.Visible Then Exit For
	Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PBLackAuthor Commented:
perfect

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.