Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

[tutorial] Visual Basic 6 Removing List Duplicates


3 replies to this topic

#1 zachtk8702

    Member [Level 2]

  • Kontributors
  • PipPipPipPipPip
  • 81 posts

Posted 10 February 2005 - 03:28 AM

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.

#2 Good Grief Graphics

    (--_--)

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 164 posts
  • Location:Millersburg, Ohio
  • Interests:Graphics

Posted 10 February 2005 - 12:12 PM

zachtk8702, on Feb 9 2005, 10:28 PM, said:

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.

View Post

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

#3 zachtk8702

    Member [Level 2]

  • Kontributors
  • PipPipPipPipPip
  • 81 posts

Posted 10 February 2005 - 01:08 PM

LoL, Well what exactly does that leave me to help you with?? Want me to just make the app for ya and have i FedExed? lol jk

#4 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 08 November 2008 - 04:19 PM

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




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users