ArgumentException using Document.ReplaceText when last character is selected

SyntaxEditor for Windows Forms Forum

The latest build of this product (v2018.1 build 0341) was released 9 months ago, which was before this thread was created.
Posted 7 months ago by Stefan Koell
Version: 18.1.0341
Platform: .NET 4.7
Environment: Windows 10 (64-bit)


I stumbled across the following issue. I use code like this:

int SelectionStart = textBox.SelectedView.Selection.StartOffset;

int SelectionLen = textBox.SelectedView.Selection.Length;

textBox.Document.ReplaceText(DocumentModificationType.Replace, new TextRange(SelectionStart, SelectionStart + SelectionLen), textToReplace);

When the last character is selected, I get an ArgumentException.

I should also mention that the textToReplace is longer than one character (for example 10 characters) but if I select the single character before the last one, the method works just fine.



[Modified 7 months ago]

Comments (2)

Answer - Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA

Hi Stefan,

I think the problem here is that if you reverse selected the text, the "start offset" will be the last offset in the selection and the "end offset" (where the caret is) will be the first offset.  That then brings your text range out of bounds.

If you use FirstOffset instead of StartOffset, it should work.  Or even better, use this:


Actipro Software Support

Posted 7 months ago by Stefan Koell

Thanks, that works much better!

Add Comment

Please log in to a validated account to post comments.