Page 1 of 1

Advancing from field to field

Posted: Fri Jul 07, 2017 6:07 pm
by DavJans
When filling out fields with "Tab on Return" turned on as you hit Enter or Tab for that matter the cursor advances to the next field. Great :)

I am trying to manipulate this to work a little bit different like so,

Code: Select all

on closeField
   if fld "field2" is empty then
      focus on fld "field2"
   else
      if fld "field3" is empty then
         focus on fld "field3"
      else 
         if fld "field4" is empty then
            focus on fld "field4"
         else
            focus on fld "field5"
         end if
      end if
   end if
end closeField
This works great! in livecode, however when it is saved as a windows stand alone application it does not work.
Does anyone have any other Ideas for me that wight work.

Re: Advancing from field to field

Posted: Mon Jul 10, 2017 12:02 pm
by MaxV
I'd change your code to this, and put it in the card:

Code: Select all

on exitField 
 answer "Hey, the field is empty/unchanged!"
end exitField
See: http://livecode.wikia.com/wiki/ExitField
Best regards
Max

Re: Advancing from field to field

Posted: Mon Jul 10, 2017 4:43 pm
by jacque
What happens when it doesn't work? Does any field get selected or is the command ignored entirely?

Re: Advancing from field to field

Posted: Tue Jul 11, 2017 11:09 pm
by DavJans
In a standalone windows environment the script is ignored most of the time and it just advances to the next field like the script wasnt there at all. from field1 to field2 and from field2 to field3 and so on.

This code works better, but not what I want because it makes it harder to change the value in a field.

Code: Select all

on openField
      if fld "field3" is empty then
      else
         focus on fld "field4"
      end if
end openField

Re: Advancing from field to field

Posted: Wed Jul 12, 2017 8:11 pm
by jacque
This should do what you want:

Code: Select all

local sFlds = "fld1,fld2,fld3,fld4,fld5" -- use the real field names here

on tabKey
  if "field" is not in the name of the target then exit tabKey
  if target = "" then pass tabKey
  selectNextEmptyFld
end tabKey

on selectNextEmptyFld
  put the short name of the target into tCurFld
  put itemOffset(tCurFld,sFlds) into tStart
  repeat with x = tStart to the number of items in sFlds
    put item x of sFlds into tFldName
    if fld tFldName = empty then
      select text of fld tFldName
      exit repeat
    end if
  end repeat
end selectNextEmptyFld
Note that this is one case where using "the" matters. "The target" and "target" are different things.

Re: Advancing from field to field

Posted: Wed Jul 12, 2017 8:24 pm
by jacque
Actually, the above script may not be exactly what you want. What do you want it to do if the selected field is already empty? Right now it uses the normal tab behavior. Also, it doesn't wrap around, but your example didn't do that either.

If you want the selection to stay in an empty field without moving, remove the second line of the tabKey handler.

Re: Advancing from field to field

Posted: Wed Jul 12, 2017 8:32 pm
by jacque
This one wraps around and doesn't use normal auto-tab behavior:

Code: Select all

local sFlds = "fld1,fld2,fld3,fld4,fld5" -- use the real field names here

on tabKey
  if "field" is not in the name of the target then exit tabKey
  --   if target = "" then pass tabKey
  selectNextEmptyFld
end tabKey

on selectNextEmptyFld
  put the short name of the target into tCurFld
  put itemOffset(tCurFld,sFlds) into tStart
  if tStart = the number of items in sFlds and fld (item tStart of sFlds) <> "" then
    put 1 into tStart
  end if
  repeat with x = tStart to the number of items in sFlds
    put item x of sFlds into tFldName
    if fld tFldName = empty then
      select text of fld tFldName
      exit repeat
    end if
  end repeat
end selectNextEmptyFld
Maybe that will get you close.