Question

Undoing a ReplaceText operation selects the text

Posted 2 years ago by Avatar Kasper
Hi guys,

A quick question: When I perform a ReplaceText operation, like this:

editor.Document.ReplaceText(TextChangeTypes.Custom, property.StartOffset, property.EndOffset - property.StartOffset, newCode);

and then undo it (a simple Ctrl+Z), the original text (replaced by the operation) is selected in the editor, despite the fact that no text was selected before. I'm not sure why, and this is not the behavior of SE for WinForms. I don't do anything else besides the line of code shown above, so you should be able to see the problem easily. Is this some sort of option that I can disable or is it a bug?

[Modified at 01/25/2012 09:50 AM]

Comments (4)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Kasper,

I did a test in the WinForms editor and it works the same, and always has been the same as far as I remember.

In WPF, there is a way you can change the selection on undo. Basically if you manually create a text change and set its SelectionPositionRange to something, that will be used on undo. Here is some sample code that shows this:
var doc = control.editor.Document;
var change =  doc.CreateTextChange(TextChangeTypes.Custom);
change.ReplaceText(control.editor.Caret.Offset, 1, "test");
change.SelectionPositionRange = new TextPositionRange(control.editor.Caret.Position);
change.Apply();

Actipro Software Support
Posted 2 years ago by Kasper
Hi,

>I did a test in the WinForms editor and it works the same, and always has been the same as far as I remember.

With the WinForms edition, I passed the DocumentModificationOptions.RetainSelection flag, but I had of course already tried that for WPF with no luck (and no change in behavior, I think?). So, is this difference by design? I'm not really sure why anyone would want a piece of text re-selected just because of an undo operation, and this is not how Visual Studio does it either. In VS, if I search/and replace a word, jump to the next occurrence and then undo the previous replace, the text is simply changed back, while preserving my current cursor position. That's what I want :)

With your solution, I would have to change the behavior of the built-in Undo action, I suppose? I would be more comfortable if it just worked out-of-the-box, to be honest.
Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Kasper,

Ok, I didn't know you had used the RetainSelection flag in the WinForms version. Yes when I tried it with that set in WPF, it didn't behave the same.

It appears that flag wasn't being persisted into the undo history. This has been fixed for the next maintenance release.

Actipro Software Support
Posted 2 years ago by Kasper
Excellent, thank you very much. I'm looking forward to it :)
Information The latest build of this product (2014.1 build 0601) was released 29 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.