|
-
Jan 10th, 2007, 06:45 PM
#1
Thread Starter
Lively Member
[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:
' Find the window.
lHwnd = FindWindow(vbNullString, "vegric")
' Find the chat box.
lHwnd = FindWindowEx(lHwnd, ByVal 0&, "RichTextWndClass", vbNullString)
'allocate a 255 byte string; the first two bytes indicate the length
strBuff = Chr$(255) & String$(256, vbNullChar)
'now this gets the last line of the textbox
Call SendMessage(lHwnd, EM_GETLINE, SendMessage(lHwnd, EM_GETLINECOUNT, 0, ByVal 0&) - 2, ByVal StrPtr(strBuff))
'convert the string to Unicode
sWindowText = StrConv(strBuff, vbUnicode)
MsgBox sWindowText
'below instead of saying 'if sWindowText = "hello"', I want to say something like if sWindowText CONTAINS "hello"
If sWindowText = "hello" Then
MsgBox ("The word 'hello' is in this sentence")
End If
Thanks,
Brad
-
Jan 10th, 2007, 06:51 PM
#2
Hyperactive Member
Re: How to read certain words in a textbox
VB Code:
If InStr(sWindowText, "hello") > 0 Then
'Do stuff
End If
Or if you don't want the case of the text to matter...
VB Code:
If InStr(LCase$(WindowText), "hello") > 0 Then
'Do stuff
End If
-
Jan 10th, 2007, 06:51 PM
#3
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:
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:
Dim arrSplit() as String
Dim intCounter as Integer
arrSplit = Split(sWindowText, " ")
For intCounter = 0 To Ubound(arrSplit)
If arrSplit(intCounter) = "hello" Then
MsgBox ("The word 'hello' is in this sentence")
End If
Next intCounter
-
Jan 10th, 2007, 06:57 PM
#4
PowerPoster
Re: How to read certain words in a textbox
 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.
-
Jan 10th, 2007, 07:08 PM
#5
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.
-
Jan 10th, 2007, 10:39 PM
#6
Thread Starter
Lively Member
Re: [RESOLVED] How to read certain words in a textbox
Thank you. I'm ok now.
-
Jan 10th, 2007, 11:28 PM
#7
Re: How to read certain words in a textbox
 Originally Posted by BrendanDavis
Or if you don't want the case of the text to matter...
VB Code:
If InStr(LCase$(WindowText), "hello") > 0 Then
'Do stuff
End If
Or
VB Code:
InStr(1, WindowText, "hello", vbTextCompare)
-
Jan 10th, 2007, 11:47 PM
#8
Hyperactive Member
Re: How to read certain words in a textbox
 Originally Posted by DigiRev
Or
VB Code:
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.
-
Jan 10th, 2007, 11:51 PM
#9
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:
InStr(LCase$(WindowText), "hello")
' rather than
InStr(1, WindowText, "hello", vbTextCompare)
-
Jan 10th, 2007, 11:55 PM
#10
Hyperactive Member
Re: How to read certain words in a textbox
 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.
-
Jan 10th, 2007, 11:56 PM
#11
Hyperactive Member
Re: [RESOLVED] How to read certain words in a textbox
 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:
InStr(LCase$(WindowText), "hello")
' rather than
InStr(1, WindowText, "hello", vbTextCompare)
'Tis the way I've always done it. Just seems more "at home" to me
-
Jan 11th, 2007, 04:47 AM
#12
PowerPoster
Re: How to read certain words in a textbox
 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.
-
Jan 11th, 2007, 04:51 AM
#13
PowerPoster
Re: How to read certain words in a textbox
 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.
-
Jan 11th, 2007, 12:19 PM
#14
Re: How to read certain words in a textbox
Much simpler would be to append spaces to the string to check. 
VB Code:
InStr(LCase$(" " & WindowText & " "), " hello ")
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|