How can I make a function to select everyone in a Flex textbox?
I'm trying to create a simple "smart" textbox component in Flex, and I need a function inside it that I can use outside of the component to force myself to select all the text inside it.
Inside mine SmartTextbox.mxml
:
public function selectAll():void
{
this.setSelection(0, this.length);
}
I also use this function when the textbox gets focus, like:
private function onTextInput_focusIn(event:Event):void
{
selectAll();
}
Later, at the focusIn event, works. But if I try to call the function from outside, like:
Inside another component, where texInputQuickSearch
is the SmartTextBox component.
if(searchModule.currentState == SearchModule.STATE_SEARCH)
{
doSearch();
searchModule.textInputQuickSearch.selectAll();
}
It will not reselect the text.
Why does it work like this?
You need to do something similar to this ...
AS3:
import mx.core.UITextField;
private function initializeHandler( event:Event ):void{
var ti:TextInput = event.currentTarget as TextInput;
var tf:UITextField = ti.mx_internal::getTextField();
tf.alwaysShowSelection = true;
ti.setFocus();
}
private function setSelection( start:int, end:int ):void{
txtName.selectionBeginIndex = start;
txtName.selectionEndIndex = end;
}
MXML:
<mx:TextInput id="txtName"
initialize="initializeHandler( event );"/>
My first guess was that your conditional statement does not evaluate to TRUE when you expect it. It might be a typo in your question, but you have:
searchModule with lowercase "s" versus SearchModule with uppercase "S"
If you are not using Flex Builder or another debugging environment, I would test it with a trace or something inside a genuine code block (which can be run from the FLASH IDE):
if(searchModule.currentState == SearchModule.STATE_SEARCH) {
trace("made it here...I'm in");
doSearch();
searchModule.textInputQuickSearch.selectAll();
trace("you should have seen it select!");
}
Make sure both outputs print. If so, you at least know that doSearch () doesn't get stuck.