Skip to main content
Participating Frequently
October 12, 2013
Answered

Adding up text boxes

  • October 12, 2013
  • 1 reply
  • 1517 views

I'm a student leanring action script and my assignment is to make a an application that calculates something. So...

I have 9 input text boxes that should all add up into a dynamic text box. Code below...

var work:Number = parseInt(work_txt.text);

var rnr:Number = parseInt(rnr_txt.text);

var exerciseB:Number = parseInt(exerciseB_txt.text);

var exerciseM:Number = parseInt(exerciseM_txt.text);

var campTent:Number = parseInt(campTent_txt.text);

var campRv:Number = parseInt(campRv_txt.text);

var food:Number = parseInt(food_txt.text);

var twt:Number = parseInt(twt_txt.text);

var partying:Number = parseInt(partying_txt.text);

var total400:Number = work + rnr + exerciseB + exerciseM +

campTent + campRv + food + twt + partying;

stage.addEventListener(Event.ENTER_FRAME, checkTotal)      <------- I used an enter frame so the dynamic text box wil update as the user types numbers in.

function checkTotal(e:Event){

   

    if(work_txt.text==""){                   <----- All of these are to put a 0 in the text boxes so the app calulates a 0 instead of nothing, having it come out as NaN.

        work=0;

    }

    if(rnr_txt.text==""){

        rnr=0;

    }

    if(exerciseB_txt.text==""){

        exerciseB=0;

    }

    if(exerciseM_txt.text==""){

        exerciseM=0;

    }

    if(campTent_txt.text==""){

        campTent=0;

    }

    if(campRv_txt.text==""){

        campRv=0;

    }

    if(food_txt.text==""){

        food=0;

    }

    if(twt_txt.text==""){

        twt=0;

    }

    if(partying_txt.text==""){

        partying=0;

    }

total400_txt.text = String(total400);             <-------- total400_txt is my dynamic text box that is the total, the other boxes are all of the values the user types in.

   

}

Thank you in advance for you help! Cheers!

This topic has been closed for replies.
Correct answer Ned Murphy

They don't put a 0 in the textfield because you are not telling them to.  You are just assigning a 0 to the variable, but the variable is not being assigned to the textfield.

You need to make sure you process all of the textfields and variables before you do the totaling.  If you change a value in one of the textfields then you first need to determine if the textfield needs to be assigned a zero, then assign the textfield value to the variable, then do the totaling.

You probably want to restrict the textfields to only allow number values to be entered.

1 reply

Ned Murphy
Legend
October 12, 2013

You have to do the assignment/totaling each time something changes (in your checkTotal function).  The lines in the beginning...

var work:Number = parseInt(work_txt.text);

var rnr:Number = parseInt(rnr_txt.text);

var exerciseB:Number = parseInt(exerciseB_txt.text);

var exerciseM:Number = parseInt(exerciseM_txt.text);

var campTent:Number = parseInt(campTent_txt.text);

var campRv:Number = parseInt(campRv_txt.text);

var food:Number = parseInt(food_txt.text);

var twt:Number = parseInt(twt_txt.text);

var partying:Number = parseInt(partying_txt.text);

var total400:Number = work + rnr + exerciseB + exerciseM +

campTent + campRv + food + twt + partying;

only calculate the value once for each line at the start of the processing, it does not continuously adjust them.

Also, you should be using a Event.CHANGE event for the textfields rather than an Event.ENTER_FRAME... saves unnecessary continuous processing

Herdie27Author
Participating Frequently
October 12, 2013

Thank you that really helps!

Another thing that unfortunatly doesn't work are my if statments...

  

if(campTent_txt.text==""){

        campTent=0;

    }

)

They don't put a 0 in the text box. I have tried (campTent_txt.text = "0";), which works but once someone changes the text box to nothing I get NaN in the dynamic text box.

Ned Murphy
Ned MurphyCorrect answer
Legend
October 12, 2013

They don't put a 0 in the textfield because you are not telling them to.  You are just assigning a 0 to the variable, but the variable is not being assigned to the textfield.

You need to make sure you process all of the textfields and variables before you do the totaling.  If you change a value in one of the textfields then you first need to determine if the textfield needs to be assigned a zero, then assign the textfield value to the variable, then do the totaling.

You probably want to restrict the textfields to only allow number values to be entered.