Flex60460
Flex60460

Reputation: 993

AS3 set focus on specific component

I have a form with several component: datagrid, textArea, text input... For each component FocusIn Event is available.

var objTarget:String;
     protected function memo_focusInHandler(event:FocusEvent):void
            {
                objTarget=event.currentTarget.id;       
}

With memo_focusInHandler, I know which has focus.

My goal is to backup last focus objet, and re open Windows with focus on this object. I try to do this:

objTarget.setfocus(); 

But it doesn't work. Could you help to found the best way to reach my goal.

Upvotes: 0

Views: 1830

Answers (3)

Flex60460
Flex60460

Reputation: 993

I found a solution:

this[objTarget].selectRange(this[objTarget].text.length, this[objTarget].text.length);
    
    this[objTarget].setFocus();

Upvotes: 0

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

There is no need (that you've shown) to work with the string id reference. It would much simpler (and slightly more efficient) to work directly with the object reference.

var objTarget:Object; // Object instead of type :String

protected function memo_focusInHandler(event:FocusEvent):void {
    objTarget = event.currentTarget;  //instead of the currentTarget's id property, assign the current target itself       
}

Then, when you want to reset focus, you can do:

if(objTarget is TextInput || objTarget is TextArea){  //make sure it's a text input or text area first - optional but recommended if you don't like errors
    objTarget.selectRange(objTarget.text.length, objTarget.text.length); //set cursor to the end
    objTarget.setFocus(); //focus the text input/area
}

Upvotes: 2

Nbooo
Nbooo

Reputation: 865

String is not a display object, thus it can't be in focus. The representation of string on the stage is a TextField.

In as3 you can set focus to the desired target by using stage method:

stage.focus = myTarget;

Please see the corresponding documentation section: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#focus

Upvotes: 4

Related Questions