Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with textboxs
Started by filippo66627 Jan 18 2020 01:41 AM

8 replies to this topic

Best Answer nick_sherman , 18 January 2020 - 03:20 AM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

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:

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}


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

[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,960 posts
  • Corona SDK

  Best Answer

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.


  • SGS likes this

[TOPIC: post.html]
#3

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

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.
tysm i'll try tomorrow

[TOPIC: post.html]
#4

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

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.

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 : 

  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   
tysm 

  • filippo66627 likes this

[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,960 posts
  • Corona SDK

numericField is the object itself, not its text value.

 

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

 

 



[TOPIC: post.html]
#6

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK


numericField is the object itself, not its text value.

numericField.text = numericField.text.."1"
 
ty now it works

[TOPIC: post.html]
#7

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK


numericField is the object itself, not its text value.

numericField.text = numericField.text.."1"
How do i use the function 'tonumber' to convert the text in actual numbers?

[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,960 posts
  • Corona SDK

 
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.



[TOPIC: post.html]
#9

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

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.
ty i figured out how to use that properly. Now my problemi is to actually moltply or divide the numbers..


[topic_controls]
[/topic_controls]