splitting values and putting it in a text box in AS3

2.1k Views Asked by At

I enter a set of 4 values in one text box and i display it in a splitted way in 4 other small text boxes using this code:

array.push(Number(t1.text));
array.push(Number(t2.text));
array.push(Number(t3.text));
array.push(Number(t4.text));

b2.addEventListener(MouseEvent.CLICK, act2);
//ACTION OF THE THE FIRST BUTTON CLICK
function act1(event:MouseEvent):Array 
{
    var input:String = tt.text;
    array = input.split(" ");
    t1.text=array[0];
    t2.text=array[1];
    t3.text=array[2];
    t4.text=array[3];
}

But now I need to know how to do the same thing for any dynamic value that is entered.

Say I have a text box tt1 and a button b1. When I enter any value (say 6) this number of text boxes are created (6 new text boxes with names t0,t1....t5)

I have another text box tt2 and a button b2. When i enter a set of values in it (say 10,66,33,45,2,4) I need these values to get displayed in those text boxes t0,t1,t2..

Is this possible?

2

There are 2 best solutions below

0
On

This snippet will create your textfields

// this will hold our new textfields
var textfields:Array = new Array();

function handleCreateFields(e:Event):void {
    // assuming your textfield for the number of fields to be created 
    // is named tfNumfields
    var count:int = parseInt(tfNumfields.text);

    for( var i:int = 0; i < count; i++){
        var tf:TextField = new TextField();
        tf.x = 100;
        tf.y = 100 + i * 30;
        addChild(tf);
    }
}

This snippet will split the contents of your textfield into multiple textfields

function handleCreateFields(e:Event):void {
    // assuming your textfield for the values is named tfValues
    var values:Array = tfValues.split(" ");

    for( var i:int = 0; i < textfields.length && i < values.length; i++){
        textfields[i].text = values[i];
    }
}
0
On

It is possible:

b1.addEventListener(MouseEvent.CLICK, createTextFields);
b2.addEventListener(MouseEvent.CLICK, showValues);

var textFields:Array = [];

function createTextFields(event:MouseEvent):void
{
    var quantity:uint = uint(tt1.text);
    for (var i:int = 0; i < quantity; i++)
        textFields.push(createTextField(i));
}
function showValues(event:MouseEvent):void
{
    const SEPARATOR:String = " ";  // or "," or whatsoever
    var values:Array = tt2.text.split(SEPARATOR);
    for (var i:int = 0; i < values.length; i++)
        textFields[i].text = values[i];
}
function createTextField(i:int):TextField
{
    // create the text field and add it to the stage if you want
}

I recommend you to check if the number of values entered is the number of text fields you've just created.