|
-
Jul 19th, 2006, 06:56 AM
#1
Thread Starter
Addicted Member
How to search a textbox for listbox items.(Solve)
Hi, I know how to search a listbox for an item on a texbox, but now I need to search a textbox for any for items locoted in a listbox.
so lets say if a listbox has, one, two, three
and in the textbox there is a sentence as such
"the number one"
then the program should msgbox word found.
thanks
Last edited by locohacker; Jul 19th, 2006 at 08:07 AM.
Reason: solve
-
Jul 19th, 2006, 07:05 AM
#2
Re: How to search a textbox for listbox items.
You should be using Instr$() function. Something like this
VB Code:
If InStr$(Text1.Text, List1.List(1)) > 0 Then
MsgBox "List Item is present in the text box"
End if
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Jul 19th, 2006, 07:06 AM
#3
Re: How to search a textbox for listbox items.
Not all that many ways to go:
VB Code:
Dim strText As String, lngA As Long
' store into temporary string variable for faster access
strText = Text1.Text
' loop!
For lngA = 0 To List1.ListCount - 1
If InStr(strText, List1.List(lngA), vbTextCompare) > 0 Then
MsgBox "Found!"
Exit For
End If
Next lngA
If lngA = List1.ListCount Then MsgBox "Not found!"
Shuja Ali: you shouldn't use $ character within InStr, because the return value is a Long and if you use $, the return value is first converted to a string and then to something else for numeric comparison.
-
Jul 19th, 2006, 07:06 AM
#4
Re: How to search a textbox for listbox items.
well here's one way it could be done:
VB Code:
Dim N As Long
For N = 0 To List1.ListCount
If InStr(Text1.Text, List1.List(N)) Then _
MsgBox "Found: " & List1.List(N)
Next N
but what approach you actually take depends entirely on the specific circumstances of what you want to do. For example: do you want whole / partial words? is there punctuation?
you might want to also consider searching for Regular Expressions.
-
Jul 19th, 2006, 07:42 AM
#5
Thread Starter
Addicted Member
Re: How to search a textbox for listbox items.
Okie I try this one first 
VB Code:
Dim strText As String, lngA As Long
' store into temporary string variable for faster access
strText = Text6.Text
' loop!
For lngA = 0 To List5.ListCount - 1
If InStr(strText, List5.List(lngA), vbTextCompare) > 0 Then
MsgBox "Found!"
Exit For
End If
Next lngA
If lngA = List5.ListCount Then MsgBox "Not found!"
but i get a type mistmatch error on this line
VB Code:
If InStr(strText, List5.List(lngA), vbTextCompare) > 0 Then
let me check the others and see
Thanks guys
-
Jul 19th, 2006, 07:44 AM
#6
Re: How to search a textbox for listbox items.
 Originally Posted by Merri
Shuja Ali: you shouldn't use $ character within InStr, because the return value is a Long and if you use $, the return value is first converted to a string and then to something else for numeric comparison.
the VB IDE won't even let you do it - it automatically removes the $ for you.
-
Jul 19th, 2006, 07:53 AM
#7
Thread Starter
Addicted Member
Re: How to search a textbox for listbox items.
Aigh I try using
VB Code:
Dim N As Long
For N = 0 To List5.ListCount
If InStr(Text6.Text, List5.List(N)) Then _
MsgBox "Found: " & List5.List(N)
Next N
it almost works perfect, but it somehow also looks for empty spaces, I think cause it also sends a message of Found: like that without any words

ah I looking to match either words or phrases
-
Jul 19th, 2006, 07:56 AM
#8
Re: How to search a textbox for listbox items.
You must do ListCount - 1 and also check if the value is above 0, because -1 is reserved for error messages.
-
Jul 19th, 2006, 07:57 AM
#9
Re: How to search a textbox for listbox items.
in post #7 (and in my orig post #4) it should be List5.ListCount - 1.
In fact, you should have got an error - oh well
I think you'd actually be much better off using Regular Expressions.
-
Jul 19th, 2006, 08:00 AM
#10
Re: How to search a textbox for listbox items.
 Originally Posted by Merri
Shuja Ali: you shouldn't use $ character within InStr, because the return value is a Long and if you use $, the return value is first converted to a string and then to something else for numeric comparison.
I usually write $ for almost all the string functions (this is a habit I have rright from Qbasic days), and this is what you get when you write code without VB IDE. :confusion:
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Jul 19th, 2006, 08:07 AM
#11
Thread Starter
Addicted Member
Re: How to search a textbox for listbox items.
Thanks guys now it works great i ended up using
VB Code:
Dim N As Long
For N = 0 To List5.ListCount - 1
If InStr(Text6.Text, List5.List(N)) Then _
MsgBox "Found: " & List5.List(N)
Next N
Thanks
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
|