Assuming you define "word" as any string of non-whitespace characters bounded by a whitespace character (ie, space, tab, carriage return), or if in first position bounde donly rightwise and if in last position bounded only leftwise, then you would want to use the On_KeyUp event in the writing object in question (will assume it's a TextBox). I won't write the code for you but will provide pseudo-code:
private whitespaces, wordcount as Integer ' <--- Note: These are module-level vars
event_handler_begin
if [typed character is whitespace and wordcount = 0] then
wordcount = wordcount + 1
elseif [typed character is whitespace and wordcount > 0] then
if [whitespaces > 0 then]
wordcount = wordcount + 1
whitespaces = 0
else
whitespaces = 1
end if
end if
event_handler_end
COMMENTS: Notice that if a typed char is not a whitespace we don't care much about it. The whitespace is defining the word-count, not the actual words themselves. The approach to take with stuff like this is not to think of the problem the way a human naturally does (ie, we count words we see on a piece of paper or screen and say what we saw), but to ask, "How is 'word' defined?", then based on that definition, ask how you can spot when a differentiation between words is detectable at any given time in the history of the object's state. In this case it is known by the entering of whitespace chars and their relation to one another, so that is what we are concerned with.
One thing this code above does not do is handle the terminal case, for example, if I type:
"All good people turn their heads each day"
in a textbox, the count would be 7, since I am not catching the termination point of the word "day". If I had typed "All good people turn their heads each day ", then I would have gotten a count of 8. Also, if I type ". . ." have I just entered 3 words? No, of course not, but the code above would say so (as long as I handled the terminal case; see below).
So what you must also handle for is the terminal case and the only way to know it has been reached is to add a count value when the user indicates he or she is done typing, most likely by clicking off the box (use the object's lost focus event). If the application you are building though is keeping the word count limited, then when a user enters a whitespace and causes the count to go above a certain number, then you cancel the key event and revert the text. This requires more coding as well, and use of the previous value property of the object, for example, or use of string functions like Left() applied to the text in the text entry object you are using. Another thing is that this code does not handle deletes and backspaces; what happens when I hit backspace or select and delete a word of set of words? Now things have gotten really fun.
As you can see, this is not as easy as it first looks, as with all event-oriented, user-centric programming. The matter of keeping track of the state of the application and its data (both on-screen and in databases) is always more complicated than it first appears and so it is better to come up with a state-management strategy ahead of time. Just be prepared to have that strategy become either useless or make exceptions to it, as in an app of any size, the chances of that happening are about 99.9999%. There is also making sure your definitions for things like "word" are accurate to the purpose at hand; in some cases, you might want a string like " . " to be considered a 'word' and at other times, not. The devil is in the details.
Having said that an I am certain thoroughly discouraged you, all i can say is, Good Luck!
Send me your code -- and I can fix it
127 views
Usually answered in minutes!
×