|
-
Nov 16th, 1999, 06:19 PM
#1
Thread Starter
Hyperactive Member
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
[email protected]
-
Nov 16th, 1999, 07:46 PM
#2
It is very easy to do using API (actually a lot less code then you have)
Code:
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:
Code:
If IfItemExists(List1, "MyListItem") Then
MsgBox "Item Exists."
Else
List1.AddItem "MyListItem"
End If
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
[This message has been edited by Serge (edited 11-17-1999).]
-
Nov 16th, 1999, 10:17 PM
#3
You can also use a Collection.
Code:
' 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
-
Nov 16th, 1999, 10:24 PM
#4
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
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 17th, 1999, 01:15 AM
#5
Guru
I expanded Aaron's code just a little bit:
Code:
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: [email protected]
ICQ: 19552879
AIM: RYoni69
-
Nov 17th, 1999, 01:19 AM
#6
Maybe I should change my name to Aaron......lol
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 17th, 1999, 01:48 AM
#7
Guru
Oh... Oops. <No Comment>
------------------
Yonatan
Teenage Programmer
E-Mail: [email protected]
ICQ: 19552879
AIM: RYoni69
-
Nov 17th, 1999, 05:54 PM
#8
Thread Starter
Hyperactive Member
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
[email protected]
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
|