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

4 posts, 1 answers
  1. Mihajlo
    Mihajlo avatar
    27 posts
    Member since:
    Aug 2018

    Posted 29 Aug 2018 Link to this post

    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.

  2. Mihajlo
    Mihajlo avatar
    27 posts
    Member since:
    Aug 2018

    Posted 29 Aug 2018 in reply to Mihajlo Link to this post

    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);

     

  3. Mihajlo
    Mihajlo avatar
    27 posts
    Member since:
    Aug 2018

    Posted 29 Aug 2018 Link to this post

    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);

     

  4. Answer
    Tanya
    Admin
    Tanya avatar
    773 posts

    Posted 31 Aug 2018 Link to this post

    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 onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top