Results 1 to 14 of 14

Thread: [RESOLVED] How to read certain words in a textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2002
    Posts
    121

    Resolved [RESOLVED] How to read certain words in a textbox

    Hi, I'm trying to read text in a window, like an AOL window, and I want to be able to read and pick out certain words in the textbox. I'm able to read the whole sentence, but I can't pick out any words in the sentence. Here is my code:

    VB Code:
    1. ' Find the window.
    2. lHwnd = FindWindow(vbNullString, "vegric")
    3.    
    4. ' Find the chat box.
    5. lHwnd = FindWindowEx(lHwnd, ByVal 0&, "RichTextWndClass", vbNullString)
    6.    
    7. 'allocate a 255 byte string; the first two bytes indicate the length
    8. strBuff = Chr$(255) & String$(256, vbNullChar)
    9.  
    10. 'now this gets the last line of the textbox
    11. Call SendMessage(lHwnd, EM_GETLINE, SendMessage(lHwnd, EM_GETLINECOUNT, 0, ByVal 0&) - 2, ByVal StrPtr(strBuff))
    12.  
    13. 'convert the string to Unicode
    14. sWindowText = StrConv(strBuff, vbUnicode)
    15. MsgBox sWindowText
    16. 'below instead of saying 'if sWindowText = "hello"', I want to say something like if sWindowText CONTAINS "hello"
    17. If sWindowText = "hello" Then
    18. MsgBox ("The word 'hello' is in this sentence")
    19. End If

    Thanks,
    Brad

  2. #2
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: How to read certain words in a textbox

    VB Code:
    1. If InStr(sWindowText, "hello") > 0 Then
    2. 'Do stuff
    3. End If

    Or if you don't want the case of the text to matter...

    VB Code:
    1. If InStr(LCase$(WindowText), "hello") > 0 Then
    2. 'Do stuff
    3. End If

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to read certain words in a textbox

    If you want to see if a string contains certain text, you can use the Like operator, eg:
    VB Code:
    1. If sWindowText Like "*hello*" Then
    ..but that will find it within other words too (such as "shello" ?!).

    Alternatively you can split the text into an array, and look at each item, eg:
    VB Code:
    1. Dim arrSplit() as String
    2. Dim intCounter as Integer
    3.  
    4.   arrSplit = Split(sWindowText, " ")
    5.   For intCounter = 0 To Ubound(arrSplit)
    6.     If arrSplit(intCounter) = "hello" Then
    7.       MsgBox ("The word 'hello' is in this sentence")
    8.     End If
    9.   Next intCounter

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to read certain words in a textbox

    Quote Originally Posted by si_the_geek
    ...but that will find it within other words too (such as "shello" ?!)
    I think you might have been thinking of the musical instrument, the Chello :-)

    Or maybe Hell On Earth but without the spaces? :-P

    Should suggest he searches for "* hello *" with the spaces if he doesn't want the partial matches
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to read certain words in a textbox

    As yes, Chello would have been a much better example!

    The problem with searching for spaces is that the word might be at the start/end of the string, which would need extra checking - the Split version covers the issue more cleanly.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2002
    Posts
    121

    Re: [RESOLVED] How to read certain words in a textbox

    Thank you. I'm ok now.

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to read certain words in a textbox

    Quote Originally Posted by BrendanDavis
    Or if you don't want the case of the text to matter...

    VB Code:
    1. If InStr(LCase$(WindowText), "hello") > 0 Then
    2. 'Do stuff
    3. End If
    Or

    VB Code:
    1. InStr(1, WindowText, "hello", vbTextCompare)


  8. #8
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: How to read certain words in a textbox

    Quote Originally Posted by DigiRev
    Or

    VB Code:
    1. InStr(1, WindowText, "hello", vbTextCompare)

    Ah, true. But mine is shorter! lol

    :P

    P.S. Why the hell would you want to split the entire thing by spaces and look up each word? That involves creating an unnecessary array, and it's a whole lot slower. If all you need to know is if a certain word exists within a given string, InStr will tell you if the word is in there quickly and easily. You could even replace "hello" with " hello " to avoid the whole "Hell On Earth w/o spaces" problem, lol.

    P.P.S. Whoever corrected the "shello" mistake by stating that it was "chello," you should know that it's actually spelled "cello" and thus would not be a complication in this particular instance.
    Last edited by BrendanDavis; Jan 10th, 2007 at 11:51 PM.

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

    Re: [RESOLVED] How to read certain words in a textbox

    InStr's Text Compare is slow compared to it's Binary Compare, so you'd probably be better off doing
    VB Code:
    1. InStr(LCase$(WindowText), "hello")
    2. ' rather than
    3. InStr(1, WindowText, "hello", vbTextCompare)

  10. #10
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: How to read certain words in a textbox

    Quote Originally Posted by si_the_geek
    As yes, Chello would have been a much better example!

    The problem with searching for spaces is that the word might be at the start/end of the string, which would need extra checking - the Split version covers the issue more cleanly.
    In a large string, that would be a whole lot of work for nothing, when all you'd need to do is check for " hello" or " hello ", et cetera. Two lines of quick-executing code Vs. several lines of slower code makes a BIG difference when you start getting into strings or text files larger than a few bytes.

  11. #11
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] How to read certain words in a textbox

    Quote Originally Posted by bushmobile
    InStr's Text Compare is slow compared to it's Binary Compare, so you'd probably be better off doing
    VB Code:
    1. InStr(LCase$(WindowText), "hello")
    2. ' rather than
    3. InStr(1, WindowText, "hello", vbTextCompare)
    'Tis the way I've always done it. Just seems more "at home" to me

  12. #12
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to read certain words in a textbox

    Quote Originally Posted by BrendanDavis
    P.P.S. Whoever corrected the "shello" mistake by stating that it was "chello," you should know that it's actually spelled "cello" and thus would not be a complication in this particular instance.
    Yeah, I know...it's said "chello" but spelled cello :-)

    I couldn't think of any word beginning or ending with "hello", the same problem si had but I was more inventive with my answer :-)

    Not that it is important, but there IS a Chello out there...it's an ISP :-)
    http://www.reference.com/browse/wiki/Chello ...not that I've actually heard of them (although the name seems familiar now)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  13. #13
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to read certain words in a textbox

    Quote Originally Posted by si_the_geek
    The problem with searching for spaces is that the word might be at the start/end of the string, which would need extra checking - the Split version covers the issue more cleanly.
    Simple enough to do. When "hello" is found in the string, the program sends the location to a function that confirms that it is a valid result and returns 1 if so, and 0 if it finds characters before or after that aren't spaces or aren't null (as in the beginning and end of the string)...or maybe another way that could check for beginning/end like the actual start point of the word or start+len equalling the length of the text etc. This isn't rocket science :-)

    I would *like* to use the split way but it means checking each word individually...using instr alone and having a verification for the info would fix the problem :-P
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to read certain words in a textbox

    Much simpler would be to append spaces to the string to check.
    VB Code:
    1. InStr(LCase$(" " & WindowText & " "), " hello ")
    Quote Originally Posted by BrendanDavis
    In a large string, that would be a whole lot of work for nothing, when all you'd need to do is check for " hello" or " hello ", et cetera. Two lines of quick-executing code Vs. several lines of slower code makes a BIG difference when you start getting into strings or text files larger than a few bytes.
    Agreed, if just searching for one word then InStr would be faster, even with the extra checks.

    However, for multiple words it is likely to be more efficient to use the array in combination with a Select Case.

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