Results 1 to 8 of 8

Thread: Checking for Duplicates

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244

    Checking for Duplicates

    Code below, I have two list boxes. On the left is a list of driver strings and the right is the box that you can use the cmdMove or cmdMoveAll to add items to it. Additionally, there is a manual add text box to add items to the right listbox.

    My question is, is there a way to validate and check for duplicates when attempting to add a new one manually or when you refresh the list on the left (which replenishes the list) from adding it again, or letting them add it (disappears from the left) but does not create duplicates on the right?

    Thanks,

    Jim

    VB Code:
    1. Private Sub txtManualAdd_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeyReturn Then
    3.         cmdManualAdd.Value = True
    4.     End If
    5. End Sub
    6.  
    7. Private Sub cmdBack_Click()
    8.     ' Move one item from right to left
    9.     If lstRight.ListIndex >= 0 Then
    10.         lstLeft.AddItem lstRight.Text
    11.         lstRight.RemoveItem lstRight.ListIndex
    12.     End If
    13. End Sub
    14.  
    15. Private Sub cmdBackAll_Click()
    16.     ' Move all items from right to left.
    17.     Do While lstRight.ListCount
    18.         lstLeft.AddItem lstRight.List(0)
    19.         lstRight.RemoveItem 0
    20.     Loop
    21. End Sub
    22.  
    23. Private Sub cmdManualAdd_Click()
    24.     ' Verifies that the driver string begins with PCL
    25.     If Not Left$(LCase(txtManualAdd.Text), 5) = "PCL" Then
    26.         MsgBox "The driver string you are attempting to add is not PCL" _
    27.         , vbCritical, "Invalid Driver String"
    28.     Else
    29.         lstRight.AddItem txtManualAdd.Text
    30.     End If
    31. End Sub
    32.  
    33. Private Sub cmdMove_Click()
    34.     ' Move one item from left to right.
    35.     If lstLeft.ListIndex >= 0 Then
    36.         lstRight.AddItem lstLeft.Text
    37.         lstLeft.RemoveItem lstLeft.ListIndex
    38.     End If
    39. End Sub
    40.  
    41. Private Sub cmdMoveAll_Click()
    42.     ' Move all items from left to right
    43.         Do While lstLeft.ListCount
    44.             lstRight.AddItem lstLeft.List(0)
    45.             lstLeft.RemoveItem 0
    46.         Loop
    47. End Sub
    48.  
    49. Private Sub lstLeft_DblClick()
    50.     ' Simulate a click on the Move button.
    51.     cmdMove.Value = True
    52. End Sub
    53.  
    54. Private Sub lstRight_DblClick()
    55.     ' Simulate a click on the Bacl button.
    56.     cmdBack.Value = True
    57. End Sub

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Can't you just loop through the items in the target list box and check if the variable you're adding is already there?
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    VB Code:
    1. Private Function DoesValueExist(ByVal pstrValue As String) As Boolean
    2. Dim blnFound  As Boolean
    3. Dim lngIndex   As Long
    4.    For lngIndex = 0 To List1.ListCount - 1
    5.       If List1.List(lngIndex) = pstrValue Then
    6.          blnFound = True
    7.          Exit For
    8.       End If
    9.    Next lngIndex
    10.    DoesValueExist = blnFound
    11. End Function
    Woof

  4. #4
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Originally posted by Wokawidget
    VB Code:
    1. Private Function DoesValueExist(ByVal pstrValue As String) As Boolean
    2. Dim blnFound  As Boolean
    3. Dim lngIndex   As Long
    4.    For lngIndex = 0 To List1.ListCount - 1
    5.       If List1.List(lngIndex) = pstrValue Then
    6.          blnFound = True
    7.          Exit For
    8.       End If
    9.    Next lngIndex
    10.    DoesValueExist = blnFound
    11. End Function
    Woof
    Hehe, exactly what I meant =).
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    It's not necessary or even desirable (since it takes longer) to loop through the entries in a listbox in order to see if a value is there.

    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    3.                       (ByVal hWnd As Long, _
    4.                        ByVal wMsg As Long, _
    5.                        ByVal wParam As Long, _
    6.                        ByVal lParam As String) As Long
    7. Private Const LB_FINDSTRINGEXACT = &H1A2
    8. ' These 3 are here in case you want to do the same thing
    9. ' with a combobox or if you only want to look for a partial match
    10. Private Const CB_FINDSTRINGEXACT = &H158
    11. Private Const CB_FINDSTRING = &H14C
    12. Private Const LB_FINDSTRING = &H18F
    13.  
    14. Private Sub Command1_Click()
    15.  
    16.     Dim lngRetVal As Long
    17.    
    18.     lngRetVal = SendMessageString(List1.hWnd, LB_FINDSTRINGEXACT, -1&, strSomeValue)
    19.    
    20.     If lngRetVal > -1& Then
    21.         ' It's already in the list and lngRetVal is the ListIndex
    22.     Else
    23.         ' It's not in the list
    24.     End If
    25.                    
    26. End Sub

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Or you could use a big hash table, and check the primary key...

  7. #7

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Havew??? A HashTable is not limited by any language you stupid badger.......just class it up, and you can use it when ever you need it. Do the same with all the structures, like Binary Three, Heap and so on, and you will be a happy man...


    PS: Never used VB.Net before...

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