How do you programmatically move the caret of a Flex TextArea to the end?
Asked Answered
P

6

6

I'm trying to move the caret in a Flex TextArea to the end after appending some text from my code. I've looked through the reference documentation for TextArea and its underlying TextField but it appears there is no method provided to handle this.

One approach I've tried is to set focus to the text area and dispatch a KeyUp KeyboardEvent with the event's key code set to the "End" key, but this doesn't work.

Any ideas on how to do this?

Thanks.

Plagiarize answered 2/2, 2009 at 7:8 Comment(0)
I
9

Try this

textArea.selectionBeginIndex = textArea.length;
textArea.selectionEndIndex = textArea.length;
Irruption answered 2/2, 2009 at 7:24 Comment(1)
Great! This works perfectly, thanks. All I have to do after this is textArea.setFocus()Plagiarize
K
4

For people looking for the Spark component way to do this, Flex 4.5, use selectRange(anchorIndex, activeIndex)

Kinaesthesia answered 21/2, 2012 at 22:8 Comment(0)
D
2

To set the caret at any position in a textArea all u need to do is

textArea.setSelection(beginIndex, endIndex);

if u set the beginIndex & endIndex to the same value (in your case textArea.text.length) the caret will be placed at that positon. If you set it to different values, text in that range will be highlighted.

Didi answered 17/7, 2009 at 10:54 Comment(0)
M
0

I believe you can directly set the textarea's scrollbar with

verticalScrollPosition : Number
textArea.verticalScrollPosition(i);
Middleman answered 13/2, 2009 at 19:31 Comment(0)
R
0

@Paul Stewart verticalScrollPosition is a property not a method so you have to use it similar to a field, like:

var newPosition:NUmber = 1;
textArea.verticalScrollPosition = newPosition;

The advantage of using it over a selectionBeginIndex/selectionEndIndex is there you do not have to set a foucus.

Reduplicative answered 17/3, 2010 at 13:48 Comment(0)
F
0

Simply add the following code after adding a text to the TextArea:

textArea.verticalScrollPosition = textArea.maxVerticalScrollPosition;
Fabrication answered 24/3, 2012 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.