Groups | Blog | Home
all groups > flash actionscript > march 2007 >

flash actionscript : Coding question - TextInput components


jlucchesi
3/11/2007 8:53:00 PM
Hi -
I need to make a movieclip that will add up 2 columns of numbers the user
will enter and then calculate the percent of one to the other from the totals.

So I have a stage full of TextInput components, 20 in all, and I want to
restrict the input to 0-9 on all of them. There's GOT to be a simpler way to
do this than what I'm doing - coding each one individually like this....

myTextInput_1.restrict = "0-9";

Please help me stop this madness.

TIA some expert assistance,
JL

P.S. - If anyone has something like this built or can point me to a tutorial,
I'd appreciate it. In the meantime I'm muddling through on my own.

kglad
3/11/2007 10:15:12 PM
if your textinput's have instance names myTextInput_1, myTextInput_2 etc, you can use:



for(var i=1;i<=20;i++){
this["myTextInput_"+i].restrict="0-9";
jlucchesi
3/11/2007 10:30:51 PM
kglad -
Bingo!
That's exactly what I needed. You caught me just in time too.

I can't seem to find any samples/tutorials/offerings on how to let people add
up a list of numbers. I found some calculators but none have helped me get off
the dime. If you know of any I'd appreciate the lead.

and thanks for this help today.

JL



jlucchesi
3/12/2007 12:00:00 AM
kglad - You've been great hangin in with me on this but I can't figure out what
I'm doing wrong because I'm still not getting anything.

Does it matter whether I'm using TextInput Components or text fields made with
the Text Tool?

?


kglad
3/12/2007 12:42:27 AM
if you want to add the numbers in myTextInput_1,...,myTextInput_2,...myTextInput_10, you can use:



tiSum=0;
for(var i=1;i<=20;i++){
tiSum+=Number(this["myTextInput_"+i].text);
jlucchesi
3/12/2007 1:59:16 AM
I'm not sure I understand what you mean by using that of code.

Should I be using TextInput components or just make text fields and make them
Input Text ?

I tried the code expecting the "tiSum" box to show the total. THen I put it
into a button like attached. I also shortened the text input instance names.

I appreciate your help but Im not sure what you're telling me to do.
JL

btn_AddEmUp.onRelease = function(){
tiSum=0;
for(var i=1;i<=20;i++){
tiSum+=Number(this["tPay"+i].text);
}

}
kglad
3/12/2007 2:13:56 AM
the code in your last message will work if your button is on the same timeline
as your textfields, your textfields have instance names tPay1, tPay2 etc AND
your button is a true (NOT movieclip) button.

if you're using a movieclip button, instead of use "this" in the onRelease
handler use this._parent.
jlucchesi
3/12/2007 2:53:30 AM
kglad -
I had to start over...the file was messy. I took out all the code for
tabIndex and the restrict. I put 2 TextInput components on the stage and a
button component and used the attached code. I got my trace but nothing in
tiSum. I also tried it with "this.DoIt", "this._parent.DoIt" just to be safe.
No totals.
Any ideas?
JL

this.btn_DoIt.onRelease = function(){

tiSum=0;
for(var i=1;i<=2;i++){
tiSum+=Number(this["tPay"+i].text);

}

trace("clicked");
}
kglad
3/12/2007 3:24:26 AM
try:



this.btn_DoIt.onRelease = function(){

tiSum=0;
for(var i=1;i<=2;i++){
tiSum+=Number(this._parent["tPay"+i].text);

}

trace("clicked");
jlucchesi
3/12/2007 3:31:53 AM
kglad
3/12/2007 5:13:48 AM
oops, if tiSum is a textfield on the same timeline as btn_DoIt and your
textinputs, use:



this.btn_DoIt.onRelease = function(){

this._parent.tiSum=0;
for(var i=1;i<=2;i++){
this._parent.tiSum+=Number(this._parent["tPay"+i].text);

}

trace("clicked");
}
kglad
3/12/2007 2:50:29 PM
jlucchesi
3/12/2007 3:39:19 PM
That would be great.

http://www.newpixelcity.com/irs3/simplifyCal.html

I can't wait to see what dumb thing I've been missing.

kglad
3/12/2007 5:08:44 PM
tiSum is the instance name of a textfield. you can't assign a number to a
textfield. you could assign the text property of tiSum to be the input
textfield numbers but then you would get string addition.

to remedy, rename your textfield that will display the sum to tSum. you can
then use:



this.btn_DoIt.onRelease = function() {
this._parent.tiSum = 0;
for (var i = 1; i<=3; i++) {
this._parent.tiSum += Number(this._parent["tPay"+i].text);
}
tSum.text = this._parent.tiSum;
};

// or even simpler:

this.btn_DoIt.onRelease = function() {
tiSum = 0;
for (var i = 1; i<=3; i++) {
tiSum += Number(this._parent["tPay"+i].text);
}
tSum.text = tiSum;
};
jlucchesi
3/13/2007 12:52:09 AM
kglad -
Both of those worked great. Both worked exactly the same.

I can't thank you enough for working this out for me and for all the quick responses.

kglad
3/13/2007 1:54:36 AM
AddThis Social Bookmark Button