Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
HI guys,
I used this code to check if the item added exists in the listview.but it's not working.can you take a quick look?
VB
Private Sub CheckDuplicates()
        Dim itemI As ListViewItem
        Dim itemJ As ListViewItem
        Dim ok As Integer

        For i As Integer = ListView1.Items.Count - 1 To 0 Step -1
            itemI = ListView1.Items(i)

            For j As Integer = i + 1 To ListView1.Items.Count - 1 Step 1
                itemJ = ListView1.Items(j)

                If itemI.SubItems(0).Text = itemJ.SubItems(0).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(1).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(2).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(3).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(4).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(5).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(6).Text AndAlso _
                   itemI.SubItems(1).Text = itemJ.SubItems(7).Text AndAlso _
                   itemI.SubItems(2).Text = itemJ.SubItems(8).Text Then
                    'Duplicate item found.
                    ok = MsgBox("A duplicate item is added,please remove the new item", vbCritical + vbOKCancel, _
                                 "Bridge Construction Cost Estimate")
                    If ok = vbOK Then
                        ListView1.Items.Remove(itemJ)
                    End If
                    Exit For
                End If
            Next j
        Next i
    End Sub
Posted

1 solution

There are several issues:

1. The SubItem index for itemI was not incremented. See below
VB
If itemI.SubItems(0).Text = itemJ.SubItems(0).Text AndAlso _
   itemI.SubItems(1).Text = itemJ.SubItems(1).Text AndAlso _
   itemI.SubItems(2).Text = itemJ.SubItems(2).Text AndAlso _
   itemI.SubItems(3).Text = itemJ.SubItems(3).Text AndAlso _
   itemI.SubItems(4).Text = itemJ.SubItems(4).Text AndAlso _
   itemI.SubItems(5).Text = itemJ.SubItems(5).Text AndAlso _
   itemI.SubItems(6).Text = itemJ.SubItems(6).Text AndAlso _
   itemI.SubItems(7).Text = itemJ.SubItems(7).Text AndAlso _
   itemI.SubItems(8).Text = itemJ.SubItems(8).Text Then
    'Duplicate item found.


2. When you Remove an item, the index number of all of the items with higher index numbers is decremented by one.

3. I am not sure about the second For statement. I would use this:
VB
For j As Integer = ListView1.Items.Count - 1 to 0 Step -1


4. Need to add a check to ensure you are not comparing the same entry to itself.
VB
If i<>j then
    If itemI.SubItems(0).Text = itemJ.SubItems(0).Text AndAlso _
       itemI.SubItems(1).Text = itemJ.SubItems(1).Text AndAlso _
       itemI.SubItems(2).Text = itemJ.SubItems(2).Text AndAlso _
       itemI.SubItems(3).Text = itemJ.SubItems(3).Text AndAlso _
       itemI.SubItems(4).Text = itemJ.SubItems(4).Text AndAlso _
       itemI.SubItems(5).Text = itemJ.SubItems(5).Text AndAlso _
       itemI.SubItems(6).Text = itemJ.SubItems(6).Text AndAlso _
       itemI.SubItems(7).Text = itemJ.SubItems(7).Text AndAlso _
       itemI.SubItems(8).Text = itemJ.SubItems(8).Text Then
       'Duplicate item found
       ok = MsgBox("A duplicate item is added,please remove the new item", vbCritical + vbOKCancel, _
           "Bridge Construction Cost Estimate")
       If ok = vbOK Then
          ListView1.Items.Remove(itemJ)
     End If
End If


Check for duplicate before adding an item to the ListView control.
VB
Dim itemJ As ListViewItem
Dim bFound As Boolean = False
For j As Integer = 0 To ListView1.Items.Count - 1
    itemJ = ListView1.Items(j)

    If payItem.ToString() = itemJ.SubItems(0).Text AndAlso _
       subpayItem.ToString() = itemJ.SubItems(1).Text AndAlso _
       subItem = itemJ.SubItems(2).Text AndAlso _
       unit = itemJ.SubItems(3).Text AndAlso _
       bridgeType = itemJ.SubItems(4).Text AndAlso _
       span = itemJ.SubItems(5).Text AndAlso _
       quantity.ToString() = itemJ.SubItems(6).Text AndAlso _
       Rate.ToString() = itemJ.SubItems(7).Text AndAlso _
       amount.ToString = itemJ.SubItems(8).Text Then
        'Duplicate item found.
        bFound = True
        Exit For
    End If
Next
If bFound Then
    Msgbox("Cannot add a duplicate item")
Else
    ListView1.Items.Add(payItem.ToString()).SubItems.AddRange({subpayItem.ToString(), subItem, unit, _
       bridgeType, span, quantity.ToString(), Rate.ToString(), amount.ToString()})
End If
 
Share this answer
 
v8
Comments
Nebilo 11-Nov-13 12:46pm    
I just corrected the code but i didn't see any thing.I put the procedure in another sub procdure that inserts items like this:
CheckDuplicates()
ListView1.Items.Add(payItem.ToString()).SubItems.AddRange({subpayItem.ToString(), subItem, unit, _
bridgeType, span, quantity.ToString(), rate.ToString(), amount.ToString()})
Mike Meinz 11-Nov-13 14:08pm    
I don't understand what you are trying to tell me.

See section of Solution 1 titled Check for duplicate before adding an item to the ListView control. This code checks for duplicate before adding a item to the ListView.
Nebilo 12-Nov-13 2:02am    
Thanks Mikey it worked
Mike Meinz 12-Nov-13 7:08am    
That's good. Please rate my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900