ArgumentException using Document.ReplaceText when last character is selected

SyntaxEditor for Windows Forms Forum

Posted 5 years ago by Stefan Koell
Version: 18.1.0341
Platform: .NET 4.7
Environment: Windows 10 (64-bit)
Avatar

Hi,

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.

Regards,

Stefan

[Modified 5 years ago]

Comments (2)

Answer - Posted 5 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

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:

editor.SelectedView.Selection.TextRange


Actipro Software Support

Posted 5 years ago by Stefan Koell
Avatar

Thanks, that works much better!

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.