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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

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

Post by MichaelBluejay » Sat Oct 12, 2019 8:36 pm

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
Last edited by MichaelBluejay on Sun Oct 13, 2019 5:21 pm, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Sun Oct 13, 2019 3:30 pm

Looks like Michael is hooked. :D

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”