How to Check for Dups in ListView?

  • Thread starter Thread starter Mr. B
  • Start date Start date
M

Mr. B

The following code is how I check for duplicates in a List box. This is
simple enough as there is only one column of stuff to check.

' Check for Duplicates
' Search listbox (from last to first)
For cntr = lbDwgList.Items.Count - 1 To 1 Step -1
' If next item is a duplicate -> Remove It
If lbDwgList.Items(cntr) = lbDwgList.Items(cntr - 1) Then _
lbDwgList.Items.RemoveAt(cntr)
Next

But I've a ListView (2 columns) that I want to check for duplicates. Having 2
columns is not the issue as I can check for a string of Col1+Col2.

But like my Listbox example, I want to check Last Item to First Item (bottom
to top). Both my ListBox and ListViews are sorted Ascending.

In a ListView, how do I do a reverse check (never having done it before).

The following of course does not work (because it doesn't search last to
first). And it returns an Error anyways. (:

' Check for Duplicates
i = lvModDwgs.Items.Count
For Each LItem In lvModDwgs.Items
' If next item is a duplicate -> Remove It
If LItem.SubItems(i).ToString = LItem.SubItems(i + 1).ToString Then _
lvModDwgs.Items.RemoveAt(i)
i = i + 1
Next

Anyone have any suggestions?

Regards,

Bruce
 
If you are retrieving the names from a database, the best way is to
use a SELECT DISTINCT when populating the listbox.

--mary
 
With Deft Fingers said:
If you are retrieving the names from a database, the best way is to
use a SELECT DISTINCT when populating the listbox.

Ah... sorry... I 'should' have mentioned that I'm not using a Data Base (:

What happens is that I initially select files (DWG files)... then on the 2nd
column I have names of Layout Tabs for each DWG file (there can be more than
one Tab per DWG). The application allows a user to increase the number of
Tabs per DWG file.

In my ADD tab logic, I give it a name of "Layout#" where # is a sequential
number (1, 2, 3, etc). So if a User has Layout1 and Layout2, and either adds
more Layouts or renames a Layout to an existing Layout name, I want to delete
the duplicates.

Regards,

Bruce
 
I'd suggest putting all the data in your listviews into an ArrayList() or
other Array based class. That way you can sort and filter through the array
quickly and more reliably (you can remove items from an array at any time -
unlike with a listview). Then simply dump the new array data back to the
listview.
_________________________________
The Grim Reaper
 
The Grim Reaper said:
I'd suggest putting all the data in your listviews into an ArrayList() or
other Array based class. That way you can sort and filter through the array
quickly and more reliably (you can remove items from an array at any time -
unlike with a listview). Then simply dump the new array data back to the
listview.

yeah... I kind of thought that was something like I'd have to do... just was
hoping there was an easier way.

Regards,

Bruce
 
slaprade said:
I have had similar issues removing duplicates fro 120K items. I used hashtables. The hashtable uses a key/value pair. do something like this
Public MyHashtable as new hashtable
....
MyHashtable.item(keyname) += 1

Interesting! I've never used Hashtables (now Hash is another thing <grin>)...

Thanks muchly... I'll play with this (probably better than dumping my stuff in
a Listbox... then back to an array.

Regards,

Bruce
 
Back
Top