Link to home
Start Free TrialLog in
Avatar of boblandess
boblandessFlag for United States of America

asked on

Click button and increse numbers in TextField

I have done a search but may not be asking the correct way.  I would like to be able to click on a button and it increases the number in a TextField.  Example, I press button and the TextField changes from 100 to 101.
Avatar of CyanBlue
CyanBlue
Flag of United States of America image

Something like this???

CyanBlue
someButton.onPress = function ()
{
   yourTextField.text = parseInt(yourTextField.text)++;
}

Open in new window

Avatar of boblandess

ASKER

I am getting the error message listed below.
Left side of assignment operator must be variable or property.
 
This your code modified with my instance names inserted.
this.bezel01.btn_NXT01.onPress = function ()
{
   myTextField2.text = parseInt(myTextField2.text)++;
}
Avatar of rascalpants
use this...

this.bezel01.btn_NXT01.onPress = function ()
{
  myTextField2.text = Number( myTextField2.text )++;
}


maybe that will work for you...

rp
Change this line...
       myTextField2.text = parseInt(myTextField2.text)++;

to these lines...
       var n:Number = parseInt(myTextField2.text);
       myTextField2.text = n++;

CyanBlue
rascalpants I still get the same error message with the mod to your code.

CyanBlue I got it to work using your code below.

var n:Number = parseInt(myTextField2.text);
this.bezel01.btn_NXT01.onPress = function ()
{
       myTextField2.text = n++;
}  

One thing I did not mention, because I did not realize that it would matter, is that the TextField is not be pre-populated.  The user enters a number into the TextField and then may use the btn_NXT01 button to advance the number that appears.  The TextField data is saved to a SLO when exiting the frame.  When I click the btn_NXT01 button it fills the myTextField2 with NaN

//fills myTextField2 with data input to myTextFieldOne
this.LLS01_btn.onPress = function(){
 _root.myTextField2.text = _root.myTextFieldOne.text
  _root.myTextFieldOne.text = _root.myTextFieldBlank.text
};

//code for saving TextField data
this.return_btn.onPress = function(){
 editSatcomPresets_so.data.TextField2 = myTextField2.text;
  editSatcomPresets_so.data.TextField8 = myTextField8.text;
   gotoAndStop("dat", 15);
}

//fills TextField data with data from editSatcomPresets shared object
stop();
editSatcomPresets_so = SharedObject.getLocal("editSatcomPresets_so");
 myTextField2.text = editSatcomPresets_so.data.TextField2
  myTextField8.text = editSatcomPresets_so.data.TextField8

onEnterFrame = arr;
onEnterFrame = setTextFormatGlobal;
var n:Number = parseInt(myTextField2.text);
 
this.bezel01.btn_NXT01.onPress = function ()
{
       myTextField2.text = n++;
} 
 
 
 
this.LLS02_btn.onPress = function(){
if ( _root.editSatcomPreset01Screen01.fivekDed25kDed._currentframe < 2) {_root.editSatcomPreset01Screen01.fivekDed25kDed.gotoAndStop(2);
}
else{_root.editSatcomPreset01Screen01.fivekDed25kDed.gotoAndStop(1);
}
}
 
 
this.LLS03_btn.onPress = function(){
if ( _root.editSatcomPreset01Screen01.chanFreq01._currentframe < 2) {_root.editSatcomPreset01Screen01.chanFreq01.gotoAndStop(2);
}
else{_root.editSatcomPreset01Screen01.chanFreq01.gotoAndStop(1);
}
}
 
 
this.LLS01_btn.onPress = function(){
	   _root.myTextField2.text = _root.myTextFieldOne.text
   _root.myTextFieldOne.text = _root.myTextFieldBlank.text
};
 
 
this.LLS04_btn.onPress = function(){
	   _root.myTextField8.text = _root.myTextFieldOne.text
   _root.myTextFieldOne.text = _root.myTextFieldBlank.text
};
 
 
this.RLS01_btn.onPress = function(){
	_root.myTextField2.text = _root.myTextFieldBlank.text
	_root.myTextField8.text = _root.myTextFieldBlank.text
};
 
//saves textField data to editSatcomPresets_so
this.return_btn.onPress = function(){
	editSatcomPresets_so.data.TextField2 = myTextField2.text;
	editSatcomPresets_so.data.TextField8 = myTextField8.text;
	gotoAndStop("dat", 15);
}
 
 
//fills textField data with data from editHfPresets shared object
stop();
editSatcomPresets_so = SharedObject.getLocal("editSatcomPresets_so");
myTextField2.text = editSatcomPresets_so.data.TextField2
myTextField8.text = editSatcomPresets_so.data.TextField8

Open in new window

there error you are getting must be becaues you have not named an instance properly.

also, you have modified the code, so that all you are doing is increasing a value that is only set at runtime...

if you want to increment a number that is in a text box, you need to take the value of that number inside of the onPress function and increment it there.


rp
I'm very certain the instance name is correct.   If I change the line of your code, myTextField2.text = parseInt(myTextField2.text)++; ,  to do something else like go to another frame it works which is one of the reasons I am certain that the instance names are correct.
I beleive the reason your code is not working is that the left side of the assignment operator must be variable or property and not an instance name.
Using CyanBlues code it does work if the textField is already populated.  
can you trace out myTextField2.text in the onPress function...  before the increment?

trace( myTextField2.text  );

and what do you get?


rp
Both lines should be inside the onPress handler, and that code should work as long as your textField myTextField2 exists on stage when you click the button even if you are populating the textField with the script...

CyanBlue
Why not test if the value is set when the user clicks? If not, display some kind of alert or do nothing.
this.bezel01.btn_NXT01.onPress = function ()
{
	var t = myTextField2.text;
	if (t.length > 0)
	{
		var n:Number = parseInt(t);
		myTextField2.text = n++;
	}
	else
	{
		trace("Oops, number is not set");
	}
} 

Open in new window

jockejocke,
I inserted your code and when there is nothing in myTextField2 it returns "Oops, number is not set".  When I enter a number into myTextField2 it does not advance the number nor does it return "Oops, number is not set".  
ASKER CERTIFIED SOLUTION
Avatar of jockejocke
jockejocke
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works exactly as needed.  Thanks for the help.  That made my day.