Set the text cursor position in desktoptextarea in textchanged event

My TextChanged event handler has three lines of code:

(curPosition=textarea.SelectionStart) ’ save the current text cursor position

(textarea.text=processTheText(textarea.text) ’ process and replace the text

(textarea.SelectionStart = curPosition) ’ replace the text cursor to the saved position

I have confirmed the value of curPosition does not change, yet the text cursor always appears at the end of the text in the textarea.

Is there something I should do after resetting the text cursor position to make it stay where I want?

This line triggers the TextChanged event before you finish executing the code in the TextChanged event.
textarea,text=processTheText(textarea.text)

Because you’ve replaced the text, the cursor position is at the end when it starts this all over again.

Create a variable in the TestArea window called isChanging, and set it to false

in the text changed event, add the code:

If isChanging Then
  Return // prevents exectution of TextChanged event while code is changing the text.
End If


isChanging = True

curPosition = me.SelectionStart

me.Text = processTheText(me.Text)

me.SelectionStart = curPosition

isChanging = false

Works perfectly! Thank you very much - I have been working on the cursor problem for days while reading umpteen forum responses regarding textarea cursor issues. I am very glad you read my entry and I thank you for your time and response. Maybe the hair I’ve pulled out will grow back … :wink: .