Problem with textboxs

Hi, im new to corona and lua in general, im trying to create a calculator, but whenever i press two buttons (2 and 3 for example) the texbot doesnt display 23 but just 3

I created an handlebuttonevent function for each button, like this:

[lua]
local function handlebuttonevent1 (event)
local phase = event.phase
if “ended” == event.phase then
TextBox = “1”
end
end

Local button1 = widget.newbutton
{ left = 150
top = 50
onEvent = handlebuttonevent1} [/lua]

And i did this for all the button (1,2,3,4,5,6,7,8,9,0,/,+,-,=,%)
But whenever i press two button the first one disappears.
And how do i do calculations between numbers and put them in another textbox? Ty sorry if its all a bit confusing

You need to add to your string rather than replace it.

  TextBox = TextBox.."1"  

As for calculations, you need to sit down and first think in plain English terms (or your own language!) what a calculator needs to do. Only then can you convert that to code. For example:

You press 2 and then 3, it displays 23. So for this you need string concatenation (as above).

You press 2 and then ‘+’, and then 3, the 2 will disappear but the program needs to remember this so it can use it in the next calculation. So for this you need a variable to store the original number, and a variable to store what the next operation will be, i.e. ‘+’.

If you now press ‘=’, it takes the original number and the current number on screen, converts them to numbers - using tonumber - and depending on what operation was selected performs that calculation, and puts that result back on screen.

As you can see a calculator may seem simple but without knowing the basics of programming and lua it can get quite complex. Have you worked through any Corona examples or tutorial videos and understood them? Have you been through the api to get a feel for what sort of commands are available? 

We have seen a lot of people on these forums trying to run before they can walk and getting very frustrated because they are essentially trying to build a house without first knowing how lay bricks.

[quote=“nick_sherman,post:2,topic:150993”]

You need to add to your string rather than replace it.

TextBox = TextBox.."1" 

As for calculations, you need to sit down and first think in plain English terms (or your own language!) what a calculator needs to do. Only then can you convert that to code. For example:

You press 2 and then 3, it displays 23. So for this you need string concatenation (as above).

You press 2 and then ‘+’, and then 3, the 2 will disappear but the program needs to remember this so it can use it in the next calculation. So for this you need a variable to store the original number, and a variable to store what the next operation will be, i.e. ‘+’.

If you now press ‘=’, it takes the original number and the current number on screen, converts them to numbers - using tonumber - and depending on what operation was selected performs that calculation, and puts that result back on screen.

As you can see a calculator may seem simple but without knowing the basics of programming and lua it can get quite complex. Have you worked through any Corona examples or tutorial videos and understood them? Have you been through the api to get a feel for what sort of commands are available?

We have seen a lot of people on these forums trying to run before they can walk and getting very frustrated because they are essentially trying to build a house without first knowing how lay bricks. [/quote]
tysm i’ll try tomorrow

Hi i tried to add my string rather than replace it but it doesn’t work, everytime i get the error i linked. ù

The code is this : 

[lua]  local numericField = native.newTextField ( 100 , 60 ,220  , 50 ) 

numericField.text = "Insert Text "

–funzione che viene richiamata quando il bottone viene premuto quindi quando è nello stato “ended” 

 --funzione per il numero 1 

local function handleButtonEvent1 ( event )

  local phase = event.phase 

  if “ended” == event.phase then 

    numericField = numericField…“1”

  end 

end 

–funzione per il numero 2 

local function handleButtonEvent2 ( event )

  local phase = event.phase 

  if “ended” == event.phase then 

    numericField = numericField…“2”

  end 

end   [/lua] tysm 

numericField is the object itself, not its text value.

  numericField.text = numericField.text.."1"  

ty now it works

How do i use the function ‘tonumber’ to convert the text in actual numbers?

  local myNumber = tonumber(numericField.text)  

The information is all there in the API docs. For example a google search ‘tonumber corona’ gives you the API reference as the first result. Google is the number one tool for any programmer.

[quote=“nick_sherman,post:8,topic:150993”]

local myNumber = tonumber(numericField.text)

The information is all there in the API docs. For example a google search ‘tonumber corona’ gives you the API reference as the first result. Google is the number one tool for any programmer. [/quote]
ty i figured out how to use that properly. Now my problemi is to actually moltply or divide the numbers…