Auto Calculation (or Recalculation)

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
arkstar
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 70
Joined: Mon Dec 21, 2009 1:51 am

Auto Calculation (or Recalculation)

Post by arkstar » Fri Apr 02, 2010 10:34 pm

I have 2 cards. I can enter a number on the first card that is used as a factor in a number of different calculations on the second card. If I change the number on the first card, I want everything to recalculate on the second card without changing one of the numbers on the second card. Or put another way, I would like to the second card to recalculate everything whenever I go to it. Right now it only recalculates if I change on a number on the second card.

Any ideas?

Thanks!
Rob
Rob Glassman
ArkStar

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Auto Calculation (or Recalculation)

Post by jmburnod » Sat Apr 03, 2010 8:14 am

Rob,
If i have understand what do you need you can use

• preopencard in the script of cd 2
• the function CalculMe in the script of cd 2 of in the stack script

Code: Select all

on preopencard
   put fld "MyNumber" of cd 1  into nb1
   put fld "myFactor" of cd 1 into TheFactor
   put CalculMe(TheFactor,nb1) into fld "Myresult" of cd 2
end preopencard

function CalculMe pFactor,pNombre
   put pFactor * pNombre into rCalculMe
   return rCalculMe
end CalculMe
regards

Jean-Marc
https://alternatic.ch

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

Re: Auto Calculation (or Recalculation)

Post by dunbarx » Mon Apr 05, 2010 5:39 pm

I would do the recalculation when you are working in the first card. There are a number of messages that can be sent to the second card as soon as you are finished with the first. Best would be within the actual script of the first card. Whatever you do there, when you are done, gather the data from card 2, do your calculation, and update card 2.

Seems cleaner to me.If you need help getting data from other cards within a working script, write back.

Craig Newman

arkstar
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 70
Joined: Mon Dec 21, 2009 1:51 am

Re: Auto Calculation (or Recalculation)

Post by arkstar » Sat May 15, 2010 3:14 pm

Thanks for the responses.

Maybe I didn't explain myself, I would like everything to calculate when the card is loaded and also recalculate when you either enter a number or change a number on the card.
For example, right now, if you change the tax rate, you have to erase the charge for the item and reenter the same number for it to calculate......

Like I said, I would like the calculations to execute whenever you enter or change a number.

Thanks!

Rob
Rob Glassman
ArkStar

dsquance
Posts: 40
Joined: Mon Jan 18, 2010 2:47 am

Re: Auto Calculation (or Recalculation)

Post by dsquance » Mon May 17, 2010 5:29 am

Perhaps enterinfield would work. Some action like pressing enter will be necessary, anyhow, and something like:
on enterinfield
<whatever calculation with the new number in the specified field of cd 2>
end enterinfield

returninfield is similar or tabkey or whatever action the user applies.

Dave

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

Re: Auto Calculation (or Recalculation)

Post by dunbarx » Mon May 17, 2010 4:28 pm

I made two fields, one named "entry" and one named "results". In the script of fld "entry" I put:

Code: Select all

on rawKeyUp
    if me is a number then put 1.05 * me into fld "results"
   pass rawKeyUp
end rawKeyUpy
Is this what you wanted? The "results" field contains the original value plus 5% tax. Your call. Note that there is little checking included to make sure the data is entered on only one line, or other possible entry oddities. I did check to see if the entry was a number.

Craig Newman

arkstar
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 70
Joined: Mon Dec 21, 2009 1:51 am

Re: Auto Calculation (or Recalculation)

Post by arkstar » Wed May 19, 2010 10:35 am

Thanks for the responses as I really appreciate it, but I don't think it does what I need.
I am passing a couple of number from one card to another.
When the second card loads, the numbers I've passed do apear, but none of the calculations takes place.
I have to change one of the numbers to make the calculation work.

Example:
Card one: I pass a charge per guest from this card to card two.
Card Two: Loads, shows the charge per guest from card one. On this card, I add sales tax, and service charges then show the final total.
In order for the final total to calculate, I have to edit or reneter the charge per guest I passed from card one, then it will calculate.

Is there any way to automate this so the user will not have to do anything?

Thanks again!!

Rob
Rob Glassman
ArkStar

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Auto Calculation (or Recalculation)

Post by bn » Wed May 19, 2010 12:16 pm

Rob,
how do you trigger the calculation on the second card? Is it in the script of a field?
Maybe you could post the calculation code on the second card. Than it is easier to find a way to trigger it from the first card.

If it is in the script of the fields on the second card you could separate the calculation from the trigger (I assume it is some sort of closefield/exitfield) by putting a script into the card script of card 2 that does the error checking and calculation. That way you could fill in the field charge per guest on the second card by script and than send "calculateTotal" to card 2.

Or you could just send the trigger to the field of card two.

Suppose you trigger the recalculation on the field "charge" on card 2 by a closeField handler than you add to the handler on card 1 field "charge" that puts the new number into the field charge on card 2 a

Code: Select all

send closefield to field "charge" of card 2
That should do it.

regards
Bernd

Post Reply