views:

1327

answers:

2

I am populating a textfield programmatically and scrolling to the maxScrollH as the new text is added so the user can see the progression of text. This works fine, until I click the TextField, which sets scrollH back to 0 and places the caret in the text at the equivalent position.

textField.setSelection( text.length, text.length ); //sets the caretIndex/selection to the end
textField.scrollH = textField.maxScrollH; //scrolls to max

this is the code that I am using to scroll when the textField text property is updated. I've tried adding a listener to the click event on the textField, which works in a way, but causes a visible jump.

override protected function createChildren() : void
{
    super.createChildren();
    textField.addEventListener(MouseEvent.CLICK, handleTextFieldClick, false, 0, true);
}

protected function handleTextFieldClick(event:MouseEvent):void
{
    textField.scrollH = currentTextFieldScrollPosition; //stored scrollH value
    trace(textField.scrollH);
}

My guess is that there is a scroll position being calculated or stored someplace that I can't find.

+1  A: 

The flex TextInput sets the selection of the textField:

/**
 *  @private
 *  Gets called by internal field so we draw a focus rect around us.
 */
override protected function focusInHandler(event:FocusEvent):void
{
    if (event.target == this)
        systemManager.stage.focus = TextField(textField);

    var fm:IFocusManager = focusManager;

    if (editable && fm)
    {
        fm.showFocusIndicator = true;
        if (textField.selectable &&
            _selectionBeginIndex == _selectionEndIndex)
        {
            textField.setSelection(0, textField.length);
        }
    }

    [...]

overriding this in my component fixes this problem:

 override protected function focusInHandler(event:FocusEvent):void
 {
  super.focusInHandler(event);
  textField.scrollH = currentTextFieldScrollPosition;
 }
Joel Hooks
I am having the same AS3 issue (UIScrollbar in a TextArea resetting its position when focus on the textarea is gained and then lost), but focusInHandler never seems to be called at all for me to override it. I also don't get the first code block--is this what focusInHandler does normally? What are you trying to do different with it? The [...] confuses me--if it's not default code for focusinhandler, why are you truncating it? I'm relatively new to AS3.
Rujiel
A: 

Turns out you need to make an onclick handler for the window with the UIScrollbar, and then add a focus out event listener for it that passes the scrollbar location at the time of the onclick to the focus. http://stackoverflow.com/questions/3964288/actionscript-3-maintaining-textarea-uiscrollbar-position-on-loss-of-focus-in-fla

Rujiel