Sunday, 1 February 2009

Flex 2, setting caret (cursor) in TextArea component

When you append text to TextArea component, that doesn't fit TextArea window, you want to move caret (cursor) to the end of the text, but it doesn't happen automaticly. There is a little work around to solve this problem:

Lets assume we have a function that adds text to existing text in TextArea component:

public function appendText(str:String):void {
textAreaComp.text = textAreaComp.text+str;
callLater(setCaretToLastLine);
}

We call to the following function:

private function setCaretToLastLine():void {
textAreaComp.verticalScrollPosition = textAreaComp.maxVerticalScrollPosition;
}

Тhe "setCaretToLastLine()" function moves scroller to the bottom of TextArea component by pointing to the last line of the text. The function called by callLater() because when updating text property of TextArea, "update event" dispatched and could be handled after "setCaretToLastLine()" function, causing maxVerticalScrollPosition property to hold not updated value.

No comments:

Post a Comment