Adding numbers in text fields

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jnorth01
Posts: 2
Joined: Tue Dec 01, 2009 9:08 am

Adding numbers in text fields

Post by jnorth01 » Tue Dec 01, 2009 9:11 am

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

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Re: Adding numbers in text fields

Post by malte » Tue Dec 01, 2009 11:06 am

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

jnorth01
Posts: 2
Joined: Tue Dec 01, 2009 9:08 am

Re: Adding numbers in text fields

Post by jnorth01 » Tue Dec 01, 2009 12:15 pm

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?

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Re: Adding numbers in text fields

Post by malte » Wed Dec 02, 2009 8:32 pm

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

Post Reply