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

荒凉一梦 提交于 2019-12-19 07:18:08

问题


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.


回答1:


Try this

textArea.selectionBeginIndex = textArea.length;
textArea.selectionEndIndex = textArea.length;



回答2:


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




回答3:


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.




回答4:


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

verticalScrollPosition : Number
textArea.verticalScrollPosition(i);



回答5:


@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.




回答6:


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

textArea.verticalScrollPosition = textArea.maxVerticalScrollPosition;


来源:https://stackoverflow.com/questions/502414/how-do-you-programmatically-move-the-caret-of-a-flex-textarea-to-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!