Results 1 to 11 of 11

Thread: How to search a textbox for listbox items.(Solve)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    160

    Resolved 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

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: How to search a textbox for listbox items.

    You should be using Instr$() function. Something like this
    VB Code:
    1. If InStr$(Text1.Text, List1.List(1)) > 0 Then
    2.     MsgBox "List Item is present in the text box"
    3. End if
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: How to search a textbox for listbox items.

    Not all that many ways to go:
    VB Code:
    1. Dim strText As String, lngA As Long
    2.     ' store into temporary string variable for faster access
    3.     strText = Text1.Text
    4.     ' loop!
    5.     For lngA = 0 To List1.ListCount - 1
    6.         If InStr(strText, List1.List(lngA), vbTextCompare) > 0 Then
    7.             MsgBox "Found!"
    8.             Exit For
    9.         End If
    10.     Next lngA
    11.     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.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to search a textbox for listbox items.

    well here's one way it could be done:
    VB Code:
    1. Dim N As Long
    2.    
    3.     For N = 0 To List1.ListCount
    4.         If InStr(Text1.Text, List1.List(N)) Then _
    5.             MsgBox "Found: " & List1.List(N)
    6.     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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    160

    Re: How to search a textbox for listbox items.

    Okie I try this one first
    VB Code:
    1. Dim strText As String, lngA As Long
    2.     ' store into temporary string variable for faster access
    3.     strText = Text6.Text
    4.     ' loop!
    5.     For lngA = 0 To List5.ListCount - 1
    6.         If InStr(strText, List5.List(lngA), vbTextCompare) > 0 Then
    7.             MsgBox "Found!"
    8.             Exit For
    9.         End If
    10.     Next lngA
    11.     If lngA = List5.ListCount Then MsgBox "Not found!"
    but i get a type mistmatch error on this line
    VB Code:
    1. If InStr(strText, List5.List(lngA), vbTextCompare) > 0 Then

    let me check the others and see
    Thanks guys

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to search a textbox for listbox items.

    Quote 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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    160

    Re: How to search a textbox for listbox items.

    Aigh I try using
    VB Code:
    1. Dim N As Long
    2.     For N = 0 To List5.ListCount
    3.         If InStr(Text6.Text, List5.List(N)) Then _
    4.             MsgBox "Found: " & List5.List(N)
    5.     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

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  10. #10
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: How to search a textbox for listbox items.

    Quote 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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    160

    Re: How to search a textbox for listbox items.

    Thanks guys now it works great i ended up using
    VB Code:
    1. Dim N As Long
    2.     For N = 0 To List5.ListCount - 1
    3.         If InStr(Text6.Text, List5.List(N)) Then _
    4.             MsgBox "Found: " & List5.List(N)
    5.     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
  •  



Click Here to Expand Forum to Full Width