Trying a custom handler for several math fields
J. Landman Gay
jacque at hyperactivesw.com
Fri Sep 13 02:04:46 EDT 2013
On 9/12/13 9:17 PM, Vaughn Clement wrote:
> 1 Issue: on the card the different sets of field controls would only update
> within the set of fields in the script. If a user edited one of the fields
> that set would update. What I needed was to have all the fields update
> regardless of what set they below too.
> I assume that this can be done by passing the handler call up to the card
> level?
That's right. If a handler isn't found, the message travels through the
message hierarchy until another object catches it.
> My questions are as follow:
>
> 1. Can a custom handler work like "on doMath" and pass script to make it
> work from the card level?
Yes. That's the foundation of the whole system. Most of my generic code
always goes in the card or the stack. Rule of thumb is to put shared
code at the level that is closest to the objects that use it. If only
one object uses the handler, put it into the object. If many objects on
a single card use the handler, then it goes in the card. If many cards
use the it, it goes in a background group or the stack script. You can
also make whole stacks into libraries that will share code across
several stacks.
> 2. The on doMath handler at the card level; can it include all of the
> different math scripts in that one handler so they all update at the same
> time from any field edited in the sets of fields needing updates.
Hard to say, it depends on what you're doing. If you mean, the doMath
handler needs to update all the fields at once, then sure, put it all in
the same handler. If you mean that different fields need to use
different math functions, then you might want to break out the pieces
into their own handlers. Tell us what you want to do exactly and we can
advise.
> 3. Can I also include the field format command.
Sure.
> 4. My consideration is that the fields were not updating as a complete
> group and that will confuse the user when they now need to re-enter the
> other fields to trigger the related changes from the other edits?
Scripts don't have to go into the fields. However, each field may want
to trigger the card handler if a change in any of them needs to update
them all. You can do that a couple of ways. Each field can call the
doMath handler, or more efficiently, just put a closeField handler into
the card script. Any field that closes will trigger a closefield
message, which will fall through to the card (provided the field doesn't
have one) and the card's closefield handler can call your doMath
handler. So, one handler in the card can manage all the fields.
>
> Last concern: I was able to get the format to add the decimal places, but I
> needed to have the money symbol in the field also. When the typed in the
> field the math script won't run. The same thing happened when I used a
> comma for the divider of hundreds. So that kills the scripts. I did not
> find anything that tells you how to solve this? I tried the dictionary for
> answers but nothing was doing it!
Text chunking is LiveCode's biggest strength. This will remove a dollar
sign from the front of a string. It assumes the string is in a variable
named "tString":
if char 1 of tString = "$" then delete char 1 of tString
Or you can use "replace" for a more generic solution:
replace "$" with empty in tString
Use "replace" on the commas too, to get rid of them. Then do the math.
Then put them back:
put "$" before tString
Adding commas back in is a little trickier if you haven't learned the
language yet. I have a handler for it somewhere, let me know if you want
it. But maybe give it your own shot first.
--
Jacqueline Landman Gay | jacque at hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
More information about the use-livecode
mailing list