This is a migrated thread and some comments may be shown as answers.

How to select inserted Span, or position just after it?

3 Answers 98 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Mihajlo
Top achievements
Rank 1
Mihajlo asked on 29 Aug 2018, 10:39 AM

Add a button in RichTextEditor demo project, and this code in its event handler:

var editor = new RadDocumentEditor(radRichTextEditor1.Document);
var startA = new DocumentPosition(editor.Document);
var endA = new DocumentPosition(editor.Document);
 
Span a = new Span("A");
editor.InsertInline(a);
 
startA.MoveToStartOfDocumentElement(a);
endA.MoveToEndOfDocumentElement(a);
 
System.Diagnostics.Debug.Assert(startA != endA);

 

Why this last line asserts? I would like to set a DocumentPosition to the end of newly added "A" text (or just select it), but don't know how.

3 Answers, 1 is accepted

Sort by
0
Mihajlo
Top achievements
Rank 1
answered on 29 Aug 2018, 02:36 PM

To answer my own question, everything revolves around Document.CaretPosition. This is the code that selects what was just inserted:

var editor = new RadDocumentEditor(radRichTextEditor1.Document);
 
var startA = new Telerik.WinForms.Documents.DocumentPosition(editor.Document);
startA.MoveToFirstPositionInDocument();
 
Span a = new Span("A");
 
editor.Document.Selection.Clear();
editor.Document.CaretPosition.MoveToPosition(startA);
editor.InsertInline(a);
 
System.Diagnostics.Debug.Assert(startA != editor.Document.CaretPosition);
 
editor.Document.Selection.AddSelectionStart(startA);
editor.Document.Selection.AddSelectionEnd(editor.Document.CaretPosition);

 

0
Mihajlo
Top achievements
Rank 1
answered on 29 Aug 2018, 03:17 PM

And here is the code if you want to insert/select something at any caret position. Had to use AnchorToCurrentBoxIndex, because startA for some reason "moves" with caret if caret is on the end of the word, so nothing is selected. I wish there was some resource that I could read/watch, so that I don't spend a few hours for every extra bit of functionality.

 

var editor = new RadDocumentEditor(radRichTextEditor1.Document);
 
var startA = new Telerik.WinForms.Documents.DocumentPosition(editor.Document.CaretPosition);
startA.AnchorToCurrentBoxIndex();
 
Span a = new Span("A");
 
editor.Document.Selection.Clear();
editor.InsertInline(a);
 
startA.RestorePositionFromBoxIndex();
editor.Document.Selection.AddSelectionStart(startA);
editor.Document.Selection.AddSelectionEnd(editor.Document.CaretPosition);

 

0
Accepted
Tanya
Telerik team
answered on 31 Aug 2018, 06:59 AM
Hi Mihajlo,

I am happy to hear that you managed to achieve the desired behavior. I completely agree that the API and documentation on the topic should be improved and we are currently working on the task, so you can expect updates in the next releases.

Regards,
Tanya
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextEditor
Asked by
Mihajlo
Top achievements
Rank 1
Answers by
Mihajlo
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or