Get all Inline (Spans) from a mouse hi-light selection, and delete them ?

6 posts, 2 answers
  1. Barry
    Barry avatar
    27 posts
    Member since:
    Aug 2011

    Posted 21 Apr Link to this post

    Hi,

    I have a RadRichTextBox and users have the ability to insert special "[SPAN TAGS]" from another control that acts as placeholders for some backend code. When the user uses the mouse and selects some text from my single line RadRichTextBox, I need to capture all the Spans within the selected range and delete them from the document.

    Example:

    There is an alert from [UNIT] which is at [LOCATION].

    If the user selects: IT] which is at [LOCA

    ...I need to remove the 2 spans, and the rest of the text from their selection.

     

    Barry

  2. Tanya
    Admin
    Tanya avatar
    584 posts

    Posted 26 Apr Link to this post

    Hi Barry,

    If I properly understand your scenario, you can directly use the Delete() method exposed by the RadRichTextBox and RadDocumentEditor classes. The methods of these classes modify the document based on the selection, so Delete() will remove the content selected by the user.

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Barry
    Barry avatar
    27 posts
    Member since:
    Aug 2011

    Posted 26 Apr in reply to Tanya Link to this post

    CustomSubjectRichTextBox.Delete(false);

    ...it deleted the highlighted text, but not their parent spans. I need all the spans owned by the text that is hi-lighted, and deleted. So if the word "MYSPAN" is highlighted as "PAN"...I need the entire span of "MYSPAN" deleted, not just three letters.

    B.

  4. Answer
    Tanya
    Admin
    Tanya avatar
    584 posts

    Posted 28 Apr Link to this post

    Hello Barry,

    I didn't understand your requirements from the initial post - please, excuse me for that.

    A Span can contain several sentences, depending on the structure of the content. From your description seems like you need to delete just the whole word(s) instead of only the selected letters. If so, you can attach to the CommandExecuting event and modify the selection to include the whole words before executing the delete action:
    private void RadRichTextBox_CommandExecuting(object sender, CommandExecutingEventArgs e)
    {
        if (e.Command is DeleteCommand)
        {
            DocumentPosition start = new DocumentPosition(this.radRichTextBox.Document);
            start.MoveToPosition(this.radRichTextBox.Document.Selection.Ranges.First.StartPosition);
            start.MoveToCurrentWordStart();
     
            DocumentPosition end = new DocumentPosition(this.radRichTextBox.Document);
            end.MoveToPosition(this.radRichTextBox.Document.Selection.Ranges.Last.EndPosition);
            end.MoveToCurrentWordEnd();
     
            this.radRichTextBox.Document.Selection.SetSelectionStart(start);
            this.radRichTextBox.Document.Selection.AddSelectionEnd(end);
        }
    }

    In case you actually need to delete the whole span, no matter how many words it contains, you can use an approach pretty similar to the one described above - you should obtain the selected layout boxes and their first and last associated inlines:
    private void RadRichTextBox_CommandExecuting(object sender, CommandExecutingEventArgs e)
    {
        if (e.Command is DeleteCommand)
        {
            var selectedBoxes = this.radRichTextBox.Document.Selection.GetSelectedBoxes();
     
            DocumentPosition start = new DocumentPosition(this.radRichTextBox.Document);
            start.MoveToStartOfDocumentElement(selectedBoxes.First().AssociatedInline);
     
            DocumentPosition end = new DocumentPosition(this.radRichTextBox.Document);
            end.MoveToEndOfDocumentElement(selectedBoxes.Last().AssociatedInline);
     
            this.radRichTextBox.Document.Selection.SetSelectionStart(start);
            this.radRichTextBox.Document.Selection.AddSelectionEnd(end);
        }
    }


    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. Barry
    Barry avatar
    27 posts
    Member since:
    Aug 2011

    Posted 28 Apr in reply to Tanya Link to this post

    Hi...thanks for the additional info. May I trouble you for a complete example ?...including the actual "delete" ?...in case I'm doing it a different way than what is required (or you are implying) ?

     

    Example for reference:

    "There is an alert from unit [NAME] which is located at [LOCATION]"

    - If the user selects "ME] which is located at [LOCA" then "[NAME] which is located at [LOCATION]" is deleted.

    - If the user selects "ert fro" then "alert from" is deleted..

    - If the user backspaces into "[LOCATION]" then the entire word is deleted.

     

    Thanks,

    Barry

  6. Answer
    Tanya
    Admin
    Tanya avatar
    584 posts

    Posted 03 May Link to this post

    Hi Barry,

    I am afraid that we don't have a similar example and, since the case is pretty custom, we are not considering to create such at this point.

    You can use the snippet from my previous reply. To do so, first, you will need to attach to the CommandExecuting event of RadRichTextBox. The code will be executed when the DeleteCommand is triggered. In other words, when the users try to delete content from the document. 

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top