Jump to content
xisto Community
Sign in to follow this  
zachtk8702

[tutorial] Visual Basic 6 Removing List Duplicates Removing List Duplicates

Recommended Posts

Removing duplicates from lists is something that you'll have to put up with if you're, say, parsing names off Outwar.Many delete-duplicate for...next loops are very slow, especially when you have thousands of names to loop through several thousand times for each name on the list.This function that I made is, in my opinion, the best and quickest way to do it without too many annoying and slow for loops (good for lists 1k +). It compares lstA to lstB. Anything that is in both lists is added to lstC. To change it so that anything that isn't in both lists is added to lstC, change the "If Not" to "If".You'll need this API declaration:Code:Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As LongConst LB_FINDSTRINGEXACT = &H1A2And here's the function I put together:Code:Function CompareLists(FirstList As ListBox, SecondList As ListBox, FinalList As ListBox)Dim i As IntegerFor i = FirstList.ListCount - 1 To 0 Step -1 If Not SendMessageString(FirstList.hwnd, LB_FINDSTRINGEXACT, -1, ByVal SecondList.List(i)) <> i Then FinalList.AddItem FirstList.List(i) End IfNext iEnd FunctionIt's called with the code (example). This belongs in a sub or another seperate function:Code:CompareLists lstA, lstB, lstCIf you only have a single list:Change the function so it only uses a single list in all instances. It that case, the function will remove duplicates and leaves it with a single instance of the duplicate. You'd have to change the code within the for loop to "lst.RemoveItem i". And then you're set.Hope this helps.

Share this post


Link to post
Share on other sites

Removing duplicates from lists is something that you'll have to put up with if you're, say, parsing names off Outwar.

 

Many delete-duplicate for...next loops are very slow, especially when you have thousands of names to loop through several thousand times for each name on the list.

 

This function that I made is, in my opinion, the best and quickest way to do it without too many annoying and slow for loops (good for lists 1k +). It compares lstA to lstB. Anything that is in both lists is added to lstC. To change it so that anything that isn't in both lists is added to lstC, change the "If Not" to "If".

 

You'll need this API declaration:

 

Code:

 

Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _

(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

Const LB_FINDSTRINGEXACT = &H1A2

And here's the function I put together:

 

Code:

Function CompareLists(FirstList As ListBox, SecondList As ListBox, FinalList As ListBox)

Dim i As Integer

 

For i = FirstList.ListCount - 1 To 0 Step -1

    If Not SendMessageString(FirstList.hwnd, LB_FINDSTRINGEXACT, -1, ByVal SecondList.List(i)) <> i Then

        FinalList.AddItem FirstList.List(i)

    End If

Next i

End Function

It's called with the code (example). This belongs in a sub or another seperate function:

 

Code:

 

CompareLists lstA, lstB, lstC

If you only have a single list:

Change the function so it only uses a single list in all instances. It that case, the function will remove duplicates and leaves it with a single instance of the duplicate. You'd have to change the code within the for loop to "lst.RemoveItem i". And then you're set.

 

Hope this helps.

48756[/snapback]

I never figured out the api thing in vb6, and now I haven't used it in years and I forgot everything.

Share this post


Link to post
Share on other sites

LIST BOX

[tutorial] Visual Basic 6 Removing List Duplicates

 

Replying to zachtk8702

Hi,

I have two list box and one command button and I want to remove the item listed in list box 2 which is already in the list box 1 when I press the button.

For example in list box 1 the item is:

Banana

Apple

Mango

Etc.

And in list box 2 are:

Melon

Grapes

Apple ' this one I want to remove

 

Thank you very much hopping for your reply.

 

-reply by dudey

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.