R – Actionscript 3 TextField scrollH property gets set to 0 on click

actionscript-3apache-flexscrolltextfield

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.

Best Answer

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;
    }
Related Topic