Results 1 to 8 of 8

Thread: Making sure listbox contents are not the same

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Posts
    56

    Making sure listbox contents are not the same

    I have a number of different entries in a listbox (they vary from 1 to 1001, so making a basic (set) IF statement will not work. The entries must not be the same. If they are the same, a messagebox will show saying so.

    How can i do this?

    Thanks

    Dave

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    To quickly catch duplicates, you can set the Sorted property of the listbox to True. However I still think you will need to run a loop to automatically remove duplicates.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Filter duplicates without API :

    VB Code:
    1. Dim i As Long, J As Long
    2.            
    3.             With .cboPending
    4.                
    5.                 For i = 0 To .ListCount - 1
    6.                    
    7.                     For J = .ListCount To (i + 1) Step -1
    8.                        
    9.                         If .LIST(J) = .LIST(i) Then
    10.                            
    11.                             .RemoveItem J
    12.                            
    13.                         End If
    14.                        
    15.                     Next                    
    16.                
    17.             End With
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  4. #4
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    Irrespective of the number of entries, you are going to have to llop through the contents of both listboxes to find out if there's a match.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim FirstBox As Integer
    3. Dim SeconBox As Integer
    4. Dim bMatch As Boolean
    5. bMatch = False
    6.     For FirstBox = 0 To List1.ListCount - 1
    7.         For SeconBox = 0 To List2.ListCount - 1
    8.         If LCase(List1.List(FirstBox)) = LCase(List2.List(SeconBox)) Then bMatch = True
    9.         Next
    10.     Next
    11. If bMatch Then
    12.     MsgBox "Item exists"
    13. Else
    14.     MsgBox "No matches in the lists"
    15. End If
    16. End Sub
    Mega.
    "If at first you don't succeed, then skydiving is not for you"

  5. #5
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    GODDAM. Beaten again
    "If at first you don't succeed, then skydiving is not for you"

  6. #6
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    Actually, forget what I said, I thought you were comparing entries in 2 list boxes.
    Never mind, I will leave my code for those who want to do that

    Mega.
    "If at first you don't succeed, then skydiving is not for you"

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Mega_Man: You don't have to loop through the entries.
    VB Code:
    1. Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    2. (ByVal hwnd As Long, _
    3. ByVal wMsg As Long, _
    4. ByVal wParam As Long, _
    5. ByVal lParam As String) As Long
    6. Public Const CB_FINDSTRING = &H14C ' For a ComboBox
    7. Public Const LB_FINDSTRING = &H18F ' For a ListBox
    8.  
    9. Dim lngRetVal As Long
    10.  
    11. ' If the value is in the list then this returns its ListIndex, otherwise it returns -1
    12. lngRetVal = SendMessageString(MyListbox.hwnd, LB_FINDSTRING, -1&, "value to find")

  8. #8
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Filter duplicates without API :
    You don't have to loop through the entries.
    Now he has both...

    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width