Caret position got lost when document changing

SyntaxEditor for Silverlight Forum

Posted 12 years ago by Christel
I focused a new problem:
I found now a solution to change the input text to upper case.

void Document_TextChanging(object sender, TextSnapshotChangingEventArgs e)
if (doFireChangeEvent && editor.ActiveView.HasFocus)
// Rekursion verhindern durch neues Ändern
doFireChangeEvent = false;
string changedtoTUpper = e.NewSnapshot.Text.ToUpper();
TextChangeOptions options = new TextChangeOptions();
// Changes must occur sequentially so that we can use unmodified
// offsets while looping over the document
options.OffsetDelta = TextChangeOffsetDelta.SequentialOnly;
// Prevent the caret from moving to the end of the text change in the
// active view
options.RetainSelection = true;
bool done = editor.Document.ReplaceText(TextChangeTypes.ChangeCase, editor.ActiveView.CurrentSnapshot.TextRange, changedtoTUpper, options);
doFireChangeEvent = true;

// Cancel the FirstKeyboard-Input which would overwrite the above
e.Cancel = true;

But when I edit in the editor itself,
each time when I remove or add a letter, the caret jumps to the end of the whole text.
When I deactivate the cancel event by commenting out and removing a letter in the editor the caret retains it's position in the text BUT the editor scroll it to the beginning of the line.
Could you please help me with this suspicious behaviour?

Comments (2)

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Christel,

Correct, you are replacing the entire snapshot which is never a recommended thing in scenarios like yours because it will affect scrolling, caret, etc. You should replace only the affected snippet range instead.

But regardless, I would not recommend doing things this way, and would recommend our event sink idea in your previous thread instead until the next build is out and the auto-case conversion works.

Actipro Software Support

Posted 12 years ago by Christel
sorry that I tried to implement my own soltuion. I was proud to find it by my own. but on the second glance, it didn't work.
But your answer gives me much more information about what happened when replacing the snapshot and this helps me again!
I tried it now woth the suggested solution of the event sink and it works and is so easy to implement.
Thanks for your patience! And professional help!!!
The latest build of this product (v2018.1 build 0233) was released 3 years ago, which was after the last post in this thread.