PDA

Click to See Complete Forum and Search --> : RE: Add to listbox only if word is not there


Troy Mac
Nov 16th, 1999, 05:19 PM
Thanks all for your help on this one. I have one more question. The way I am doing this is very slow on some computers. i.e P133 w/64 MB of ram. Running on my AMD-K62350 I don't see the slowness though. Is there a better way? Maybe filling an array with the list then checking the array to seee if the word is in it? I have been playing with this to no avail. Any help would be appreciated. Here is what I have so far. It does work but you can actually see the list moving while it is searching it. This is OK for a small list but I expect the list to get into the hundreds.

Private Sub CmdAdd_Click()
Dim FileNumber
Dim NewWord As String
Dim CheckWord As Integer
Dim I As Integer
'ADD_Click
FileNumber = FreeFile
NewWord = TxtWord.Text
For I = 0 To LstWord.ListCount - 1
LstWord.ListIndex = I
Counter = Counter + 1
'inlist = LstWord.Text
If LstWord.List(I) = NewWord Then
MsgBox "The word " & NewWord & " is already in the list"
TxtWord.Text = ""
TxtWord.SetFocus
Exit Sub
Else
If Counter >= LstWord.ListCount Then
Open App.Path & "\ana.txt" For Append As #FileNumber
Print #FileNumber, NewWord
LstWord.AddItem NewWord
TxtWord = ""
TxtWord.SetFocus
Close #FileNumber
LstWord.Refresh
LblTotWords.Caption = "Total Number of Words in the List" & _
" " & LstWord.ListCount
End If
End If

Next

End Sub

------------------
Troy MacPherson
Customer Suport Software Analyst
t_macpherson@yahoo.com

Serge
Nov 16th, 1999, 06:46 PM
It is very easy to do using API (actually a lot less code then you have)


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2


Public Function IfItemExists(pList As ListBox, pstrItem As String) As Boolean
Dim lRet As Long

lRet = SendMessage(pList.hwnd, LB_FINDSTRINGEXACT, -1, ByVal pstrItem)
If lRet > -1 Then IfItemExists = True
End Function




Usage: IfItemExists ListBox, StringToSearchFor

Example:

If IfItemExists(List1, "MyListItem") Then
MsgBox "Item Exists."
Else
List1.AddItem "MyListItem"
End If



Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)


[This message has been edited by Serge (edited 11-17-1999).]

MartinLiss
Nov 16th, 1999, 09:17 PM
You can also use a Collection.


' Put this in your form's Declarations
Private MyCollection As New Collection
'--------------------
' Then when the listbox is initially loaded
' do something like this where the first string
' is the collection value, and the second
' string is its index value
List1.AddItem "one"
MyCollection.Add "one", "one"
List1.AddItem "two"
MyCollection.Add "two", "two"
List1.AddItem "three"
MyCollection.Add "three", "three"
'-------------
' Then to determine if a new value is in the list
Dim sDummy As String
Dim bFound As Boolean

On Error Resume Next
'If the text is in the collection, Err = 0, otherwise it's not
sDummy = MyCollection.Item(Text1.Text)
bFound = (Err = 0)
If bFound Then
Err.Clear
MsgBox "Already in list"
Else
List1.AddItem Text1.Text
MyCollection.Add Text1.Text, Text1.Text
End If



------------------
Marty

Serge
Nov 16th, 1999, 09:24 PM
I think that collection here would create an overhead, since he's trying to save the List to the file anyway. But in general, I do, agree that collection is very convinient approach. Good choice Marty.


Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

Yonatan
Nov 17th, 1999, 12:15 AM
I expanded Aaron's code just a little bit:

Option Explicit


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


Private Const LB_FINDSTRINGEXACT = &H1A2


Public Function ItemExists(pList As ListBox, ByVal pstrItem As String) As Boolean
Dim lRet As Long
lRet = SendMessage(pList.hwnd, LB_FINDSTRINGEXACT, -1, ByVal pstrItem)
If lRet > -1 Then
ItemExists = True
pList.ListIndex = lRet
End If
End Function

Usage:

Dim MyString As String
MyString = InputBox("Enter string to add to the ListBox:")
If Len(MyString) = 0 Then Exit Sub ' Or Function, whatever
If ItemExists(MyString) Then
' Do nothing... The item has been highlighted in the ListBox
Else
' Add the item and highlight it
List1.AddItem MyString
List1.ListIndex = List1.NewIndex
End If

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69

Serge
Nov 17th, 1999, 12:19 AM
Maybe I should change my name to Aaron......lol

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

Yonatan
Nov 17th, 1999, 12:48 AM
Oh... Oops. <No Comment>

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69

Troy Mac
Nov 17th, 1999, 04:54 PM
Great job guys! That all works. However, I never doubted you guys. I don't know why I stopped visiting this site for so long I always get great answers to my questions.. Thanks again

Troy



------------------
Troy MacPherson
Customer Suport Software Analyst
t_macpherson@yahoo.com