Hi, I've just tried to get the following to work:
There's a dynamic textfield on stage which holds some text. I wanted to achieve a "Select all the text as soon as the user (single-)clicks on it" - this surely is a bit unorthodox, but there was a reason for that. At first I didn't think of any problems that could occur, but as usual I was surprised by Flash again.
So whoever on this earth comes to this situation - you're not alone. Just read on..
To sum it up I just want to tell you, that the following code does not work (as I supposed it to do):
-
titleTF.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
-
-
function onFocusIn(event:FocusEvent):void {
-
event.target.setSelection(0, event.target.text.length);
-
}
Instead you've got to use a slight delay before calling the setSelection method, otherwise the TF's internal FocusEvent (set cursor) comes after the selection and resets it. So there won't be any text selected at all. Here's a working solution on that:
-
titleTF.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
-
titleTF.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
-
-
function onFocusIn(event:FocusEvent):void {
-
setTimeout(event.target.setSelection, 50, 0, event.target.text.length);
-
}
-
-
function onFocusOut(event:FocusEvent):void {
-
event.target.setSelection(0,0);
-
}
Not sure if anyone will ever need this :) But we'll see..



July 29th, 2010
Marvin Blase
Posted in
Tags:
besides it's really not too useful it's good to know. :-)
thanks
useful tip and I will use it
Thanks, exactly what I was looking for!!