Skip to main content
December 11, 2008
Answered

set focus on incrementally named text fields

  • December 11, 2008
  • 2 replies
  • 451 views
I am creating many (several hundred) text fields via actionscript. I incrementally name them and place them on the stage and then access them later on to set focus.
I utilize create a textfield object:
var txtbox:TextField = new TextField();

and then use an incremented name to the text field to identify it later on:

var txtname:String = txtn+txtcnt; // txtcnt is an incremented number variable. txtn is a string variable.
txtbox.name = txtname;

I add an eventlistener to the stage. The event listener works fine, and has a companion function.
I can identify the text field I want to access once the moving is running via its name.
Then I use that .name property to attempt to set focus:

stage.focus = "txt88";

Again, "txt88" is the .name of the text field.

What am I doing wrong?

ypeError: Error #1034: Type Coercion failed: cannot convert "txt88" to flash.display.InteractiveObject.
at MethodInfo-200()


Also, i have tried to use the incremented variable in this step, but get an error msg assigning the textfield variable name this way.
var txtname:TextField = new TextField();
This topic has been closed for replies.
Correct answer
Brilliant!
That did it! I appreciate your help. This was a major stumbling block and I've been through help files and books with no luck. (Had it working great in the AS2 version!)
Anyway, you're a lifesaver. Thanks.

2 replies

Ned Murphy
Legend
December 15, 2008
Try... stage.focus = TextField(getChildByName(txtname));
Correct answer
December 15, 2008
Brilliant!
That did it! I appreciate your help. This was a major stumbling block and I've been through help files and books with no luck. (Had it working great in the AS2 version!)
Anyway, you're a lifesaver. Thanks.
kglad
Community Expert
Community Expert
December 11, 2008
use getChildByName() to convert your strings (like "txt88") to a display object (your textfield).
December 15, 2008
I'm missing something obvious.

I can get the .name property of the textfield that I want to change focus to using event listeners.
I'm trying to use stage.focus to put focus on the text field I'm targeting. I know the display index of the field I want to action to move to. I know it's .name property. When I use stage.focus = textboxname, I get a coercion error msg.
I've tried stage.focus=getChildByName(textboxname); also with no luck in changing focus, and again with coercion error.
Any idea what I'm doing wrong?