PeterM
PeterM

Reputation: 2614

How to control cursor (carat) position in TextInput in Flex 4.5

I need to handle diagraphs and then convert them on the fly to the proper unicode representation. For example when the user types in:

Sx

My app needs to replace it with:

Ŝ

Now, I've been able to do the replacement no problem. The issue though is that once I've done the replacement, the cursor goes to the beginning of the textbox rather than the end. As I'm trying to update the user's text on the fly, this obvious doesn't work.

How can I get it so that once I replace the text in the TextInput box, the cursor is on the right hand side rather than the left?

Upvotes: 5

Views: 7133

Answers (4)

Wes
Wes

Reputation: 408

For the people coming here for the solution for the Spark textInput, this is the way:

textInput.selectRange(textInput.text.length, textInput.text.length);

Upvotes: 0

deontologician
deontologician

Reputation: 2824

The setSelection method is how you set the cursor

textInput.setSelection(textInput.text.length, textInput.text.length);

You can get the current beginning of the selection with TextInput.selectionAnchorPosition and the end of the selection with TextInput.selectionAnchorPosition

Upvotes: 2

PeterM
PeterM

Reputation: 2614

Found a solution.

All you have to do is instead of updating the whole text, wipe the current content and then use:

textInput.appendText()

Hopefully this will help someone else :)

Upvotes: 6

Todd Moses
Todd Moses

Reputation: 11039

Take a look at this SO Question: How do you programmatically move the caret of a Flex TextArea to the end?

If you are using a textArea then this will work (from the selected answer):

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

Upvotes: 1

Related Questions