Auto-calculate from Text Fields

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dragontologist
Posts: 3
Joined: Sat Jul 17, 2010 8:02 pm

Auto-calculate from Text Fields

Post by dragontologist » Sat Jul 17, 2010 8:11 pm

Hi all; I've been working in Revolution for a while now and have been unable to find a command for a simple auto-calculate action. Currently, I have a "calculate" button that has to be manually pressed after entering in an integer (it displays the number divided by 3). I would like it to AUTOMATICALLY update the second field when someone enters an integer in the first field (also acceptable for it to calculate when the selection changes or they tab to the next field). I have tried a few things and been unable to come up with the solution:

on revSelectedObjectChanged
on revSelectionChanged

Any help would be appreciated!

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Auto-calculate from Text Fields

Post by bangkok » Sat Jul 17, 2010 9:42 pm

Here you go.

The idea is to work with keyDown plus a regular expression to accept only digits, and another handler to handle the special case of backspace key

Code: Select all

on KeyDown theKey
   if matchText(theKey,"[0-9]") then 
      put thekey after me
      put me/3 into fld "theResult"
   else
      beep
   end if
end KeyDown
Attachments
TEST.zip
(670 Bytes) Downloaded 239 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: Auto-calculate from Text Fields

Post by dunbarx » Wed Jul 21, 2010 8:49 pm

A more general solution, so that one can enter data in the middle of a number string and also not get waylaid if you enter a "Q", for example, is this in the field script:

Code: Select all

on rawkeyUp var
   if numToChar(var) is in ".0123456789" or var = 65288 then
      put me/3 into fld "gg"
   end if
end rawkeyUp
   
on keyDown var
    if  var is in ".0123456789" then pass keyDown
end keyDown
Craig Newman

Post Reply