Caret offset and substring

SyntaxEditor for Windows Forms Forum

Posted 9 years ago by Alessandro Sorcinelli
I've some problem with caret offset and substring function.
I need to replace some old control in an old editor.

In this editor I have a function that insert text in the middle of his content.
This function use the current caret position and the String.substring method.
I would like to replicate this base funcionality with SyntaxEditor. I know that there are multiple
other ways to insert code, but I need to use the old funcion.

What I'm trying to do is very very simple:

Dim pos As Integer = editor.Caret.Offset
Dim txt As String = "<sample text>"
editor.Text = editor.Text.Substring(0, pos) + txt + editor.Text.Substring(pos)

The old editor (a simple RichTextBox) works fine with this code.
With SyntaxEditor I have a strange behavior.

I'm using HTML syntax file and I have the seguent sample file:

<script runat="server">
private sub TestFunction()

end sub

<BR />

If I move the caret before the <br/> and call the previous code lines, my sample string is inserted in the middle of the </script> tag.
Is it bug?
Is it possible to correct this behaviour?

Thanks, Alessandro

[Modified at 11/17/2009 01:10 PM]

Comments (1)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA

Internally we remove carriage returns at line ends so there are only line feed characters, which helps speed up parsing. However the editor.Text property puts the CR characters back in. So that is why things are out of sync in the code you pasted.

Regardless that is a very poor way performance-wise to do an insert because you are building a complete string of the document twice and doing a complete replacement of the text. Instead just call the editor.Document.InsertText method at the offset you want and pass it your text string. You can also clear the under stack if you need to after your insert (setting editor.Text does that automatically).

Actipro Software Support

The latest build of this product (v2018.1 build 0341) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.