Page 1 of 1

Adding numbers in text fields

Posted: Tue Dec 01, 2009 9:11 am
by jnorth01
Hey, I was wondering if you are able to add numbers in seperate text fields and then be displayed in another text field?
Sort of like a table set up? And how would I go about doing this?

Thanks

Re: Adding numbers in text fields

Posted: Tue Dec 01, 2009 11:06 am
by malte
This is pretty easy.

Given you have 3 fields named: numberOne, numberTwo and theResult and maybe a button to calculate the result (you could also use field messages. The button is just for the example)

In the button:

Code: Select all

on mouseUp
  if the text of fld "numberOne" is a number and the text of fld "numberTwo" is a number then
    put fld "numberOne" + fld "numberTwo" into fld "theResult"
  else
    put "Incorrect data into fld "theResult"
  end if
end mouseUp
It is a bit more wordy then necessary. I just wrote "the text of" to show you what actually happens. If you want it automagically when the user changes the data, look at the keyDown message and the closeField and exitField messages in the dictionary, or ask for more examples please.

All the best,

Malte

Re: Adding numbers in text fields

Posted: Tue Dec 01, 2009 12:15 pm
by jnorth01
Thanks that helps heaps, but what if i wanted to continue to add to the total number,
say using 8 text fields to create the total?

Re: Adding numbers in text fields

Posted: Wed Dec 02, 2009 8:32 pm
by malte
In that case find clever names for your fields e.g.

field1
field2
...
field8

and repeat is your friend (no error checking here, you need to add it)

snippet:

Code: Select all

local theResult
repeat with i=1 to 8
  add field ("field"&i) to theResult
end repeat
answer theResult
Cheers,

Malte