How to Check for Dups in ListView?

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
 
M

Mary Chipman

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

--mary
 
M

Mr. B

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
 
T

The Grim Reaper

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
 
M

Mr. B

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
 
M

Mr. B

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top