|
-
Feb 11th, 2004, 10:19 AM
#1
Thread Starter
Addicted Member
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:
Private Sub txtManualAdd_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
cmdManualAdd.Value = True
End If
End Sub
Private Sub cmdBack_Click()
' Move one item from right to left
If lstRight.ListIndex >= 0 Then
lstLeft.AddItem lstRight.Text
lstRight.RemoveItem lstRight.ListIndex
End If
End Sub
Private Sub cmdBackAll_Click()
' Move all items from right to left.
Do While lstRight.ListCount
lstLeft.AddItem lstRight.List(0)
lstRight.RemoveItem 0
Loop
End Sub
Private Sub cmdManualAdd_Click()
' Verifies that the driver string begins with PCL
If Not Left$(LCase(txtManualAdd.Text), 5) = "PCL" Then
MsgBox "The driver string you are attempting to add is not PCL" _
, vbCritical, "Invalid Driver String"
Else
lstRight.AddItem txtManualAdd.Text
End If
End Sub
Private Sub cmdMove_Click()
' Move one item from left to right.
If lstLeft.ListIndex >= 0 Then
lstRight.AddItem lstLeft.Text
lstLeft.RemoveItem lstLeft.ListIndex
End If
End Sub
Private Sub cmdMoveAll_Click()
' Move all items from left to right
Do While lstLeft.ListCount
lstRight.AddItem lstLeft.List(0)
lstLeft.RemoveItem 0
Loop
End Sub
Private Sub lstLeft_DblClick()
' Simulate a click on the Move button.
cmdMove.Value = True
End Sub
Private Sub lstRight_DblClick()
' Simulate a click on the Bacl button.
cmdBack.Value = True
End Sub
-
Feb 11th, 2004, 11:04 AM
#2
Frenzied Member
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.
-
Feb 11th, 2004, 11:14 AM
#3
VB Code:
Private Function DoesValueExist(ByVal pstrValue As String) As Boolean
Dim blnFound As Boolean
Dim lngIndex As Long
For lngIndex = 0 To List1.ListCount - 1
If List1.List(lngIndex) = pstrValue Then
blnFound = True
Exit For
End If
Next lngIndex
DoesValueExist = blnFound
End Function
Woof
-
Feb 11th, 2004, 11:47 AM
#4
Frenzied Member
Originally posted by Wokawidget
VB Code:
Private Function DoesValueExist(ByVal pstrValue As String) As Boolean
Dim blnFound As Boolean
Dim lngIndex As Long
For lngIndex = 0 To List1.ListCount - 1
If List1.List(lngIndex) = pstrValue Then
blnFound = True
Exit For
End If
Next lngIndex
DoesValueExist = blnFound
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.
-
Feb 11th, 2004, 01:02 PM
#5
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:
Option Explicit
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
Private Const LB_FINDSTRINGEXACT = &H1A2
' These 3 are here in case you want to do the same thing
' with a combobox or if you only want to look for a partial match
Private Const CB_FINDSTRINGEXACT = &H158
Private Const CB_FINDSTRING = &H14C
Private Const LB_FINDSTRING = &H18F
Private Sub Command1_Click()
Dim lngRetVal As Long
lngRetVal = SendMessageString(List1.hWnd, LB_FINDSTRINGEXACT, -1&, strSomeValue)
If lngRetVal > -1& Then
' It's already in the list and lngRetVal is the ListIndex
Else
' It's not in the list
End If
End Sub
-
Feb 11th, 2004, 01:20 PM
#6
Or you could use a big hash table, and check the primary key...
-
Feb 12th, 2004, 03:32 AM
#7
vb6 does havew HashTables...that's .NET my muppety friend
-
Feb 12th, 2004, 05:45 AM
#8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|