views:

598

answers:

2

Hello,

I'm trying to create a simple "smart" textbox component in Flex, and I want a function inside it that I can use outside of the component to force itself to select all text inside of it.

Inside my SmartTextbox.mxml

public function selectAll():void
{
    this.setSelection(0, this.length);
}

I also use this function when the textbox gets focus, like this.

private function onTextInput_focusIn(event:Event):void
{
     selectAll();
}

The later one, on focusIn event, is working. But if I try to call the function from outside, like:

Inside another component where texInputQuickSearch is a SmartTextBox-component.

if(searchModule.currentState == SearchModule.STATE_SEARCH) { doSearch(); searchModule.textInputQuickSearch.selectAll(); }

It won't reselect the text.

Anyone have a clue why it works like this?

/B

A: 

My first guess would be that your conditional statement is not evaluating to TRUE when you expect it to. Maybe it's a typo in your question but you have:

searchModule with a lowercase "s" compared to SearchModule with an uppercase "S"

If you're not using Flex Builder or someother debug environment, I would test it with a trace or something inside the true code block like this (which can be run from inside 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!");
}

Verify that both outputs print. If so, you at least know that doSearch() isn't getting stuck.

gmale
I use SearchModule.STATE_SEARCH because it's a static state-name.
blueblood
+1  A: 

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 );"/>
Shua
It worked! :) Thank you, mind explaining why it worked? I'm not familiar with mx_internal and the UITextField component.
blueblood