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

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

5 Answers 24 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Barry
Top achievements
Rank 1
Barry asked on 21 Apr 2017, 05:46 PM

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

5 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 26 Apr 2017, 10:41 AM
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.
0
Barry
Top achievements
Rank 1
answered on 26 Apr 2017, 07:24 PM

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.

0
Accepted
Tanya
Telerik team
answered on 28 Apr 2017, 03:02 PM
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.
0
Barry
Top achievements
Rank 1
answered on 28 Apr 2017, 05:25 PM

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

0
Accepted
Tanya
Telerik team
answered on 03 May 2017, 02:17 PM
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.
Tags
RichTextBox
Asked by
Barry
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Barry
Top achievements
Rank 1
Share this question
or