Skip to main content
Known Participant
June 24, 2010
Question

Assign stage.focus to text field

  • June 24, 2010
  • 2 replies
  • 2450 views

I want to have it so that when text is entered into an input text field, the cursor advances automatically to the next text field.   The text fields are nameed box1, box2 and so on, and are custom classes called Box. So far I have:

var nextBoxNum:int = boxNum + 1;                                     // identifies the next text field
var nextBoxName:String = "box" + String(nextBoxNum);  // identifies the name of the next text field

??
stage.focus = //the next box                                               // assigns the focus to the next text field

How do I get from a string variable to the stage.focus?

This topic has been closed for replies.

2 replies

Kartik_Mehta
Inspiring
June 25, 2010

I think you have to type cast the textfield name like this

var nextBoxName:TextField = "box"+String(nextBoxNum) as TextField

stage.focus = nextBoxName

try this and let me know the results

Known Participant
June 28, 2010

var nextBoxNum:int = boxNum + 1;
   var nextBox:TextField = "box"+String(nextBoxNum) as TextField
   stage.focus = nextBox;
   trace(nextBox);

I tried it like this (the variable names make more sense this way) and nothing happened.  There were no error messages and the trace returned a null.

Inspiring
June 25, 2010

Sorry, it makes no sense to me. What is your rule of moving from one TextField to another?

Known Participant
June 25, 2010

The user will be entering data into a series of text fields.  When the user hits enter on the keyboard, I want the cursor to advance to the next field on the stage.  My question is how to tell the program what the next text field will be.  So, if the current event target is box1, for example, how can I assign box2 to the stage focus.

Inspiring
June 25, 2010

On computers the conventional keyboard shortcut to move from text field to another is tab while return/enter has functionality of inserting line breaks or button (submit among others) clicks. In Flash input text fields can be (and are by default) tab enabled.

If you want to change that and use enter key as a prompt to go to another text field, you will need to:

1. Add keyboard event listener

2. Read if enter key is pressed

3. Create rules of what next field is and write a custom logic that implements this. For instance, you can keep references to the text fields in an array and advance user based on the array index.

4. Keep track of what field is currently in focus.

Kartik demonstrated code that allows setting focus to a particular text field.