Creating a Search Field

J. Landman Gay jacque at hyperactivesw.com
Sat Apr 26 12:36:09 EDT 2008


Gregory Lypny wrote:
> Hello everyone,
> 
> I tried to write a handler for one of those nifty search fields that you 
> find in Mac programs like Mail, where it pulls up found records as you 
> type a search phrase.  I came up with the following.  (Incidentally, I 
> do not use the filter command because it is much slower than "repeat for 
> each" on big data sets.)
> 
> on keyDown
>   put me into searchString
>   get fld "Data"
>   repeat for each line thisLine in it
>     if thisLine contains searchString then put thisLine & return after hits
>   end repeat
>   if the last character of hits is return then delete the last character 
> of hits
>   put hits into fld "Browse List"
>   put "Found" && recordCount(hits,"f") into fld "Number of Records"  -- 
> a hit count function
>   pass keyDown
> end keyDown
> 
> It does the trick, but has a number of glitches that I'd like to fix.  
> The main one is that it pulls up records that do not contain the search 
> string.  For example, if I type "mit" (without quotes), it will 
> correctly pull up records that contain Mitchell; however, if I then type 
> another letter, say "p", it will still pull up the same records that 
> contain Mitchell even though none of these has the string "mitp", 
> although some do contain "mit" in one spot and somewhere else a "p" and 
> others do not contain "p" at all.  I must not be understanding the 
> contains operator.
> 
> The other thing that tells me I haven't gotten it right is that the hit 
> list should get smaller the more characters I type and bigger as I 
> delete them.  Well, most of the time the first character I type (e.g., 
> just an "m") turns up nothing; I have to type a second character to get 
> it going.  And keyDown does not seem to respond to the Delete key.
> 
> Any suggestions would be most appreciated.

The problem is that the newly-typed letter isn't in the field when you 
retrieve the field contents in the first line; it doesn't get placed in 
the field until the last line where you pass keydown. That means you 
will always be testing agains the previous contents of the field, rather 
than the new contents.

One way to fix it is to change the beginning of the handler like this:

on keydown pKey
   put me & pKey into searchString

That way you will always have the latest entry.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list