Page 1 of 1

How to get a list field to select what's typed

Posted: Sat Oct 12, 2019 8:36 pm
by MichaelBluejay
Here's a tip for others in the same boat, especially since I didn't find any other good premade examples from a Google search. (They probably exist, I just didn't find them from a cursory search. Hopefully this post will show up in a cursory search for others in the future. I did find one example in a cursory search, but it used a lot of code.)

Anyway, given some limitations of option menus, I decided to create a list field that would work like an option menu. The first task was moving the selection to whatever the user typed. I was amazed that I could get something that seemed to work with just 3 lines of code:

Code: Select all

local typedChars

on keyup theKey
   put theKey after typedChars
   select line lineoffset(typedChars,me) of me
end keyup
And then adding a timeout, to empty out the typedChars for the next use, or to allow the user to start typing over from scratch, takes only two extra lines:

Code: Select all

local typedChars, timeout

on keyup theKey
   if the seconds - timeout > 1 then put empty into typedChars
   put theKey after typedChars
   select line lineoffset(typedChars,me) of me
   put the seconds into timeout
end keyup
But, as I said in another thread, before of false positives with lineoffset! If my field contains these two lines:

Chase Checking
Checks to Deposit


...then typing "che" will *not* go to the second line, since lineoffset matches the 2nd word of the 1st line.

So, as a complete solution, matching only the beginning of lines:

Code: Select all

local typedChars, timeout

on keyup theKey
   if the seconds - timeout > 1 then put empty into typedChars
   put theKey after typedChars
   lock screen -- greatly speeds things up on fields with lots of lines
   repeat with x = 1 to the number of lines of me
      if line x of me begins with typedChars then set the hilitedline of me to x
   end repeat
   put the seconds into timeout
end keyup

Re: How to get a list field to select what's typed

Posted: Sun Oct 13, 2019 3:30 pm
by dunbarx
Looks like Michael is hooked. :D

Craig