Save a text range that updates it's position when document text changes

I'm trying to let the user make selections in the SfRichTextBoxAdv and save these selections (text ranges) to a collection. The thing is i also want the text ranges to update their start/end positions when text is added or deleted before them. i.e: If text is added before the text range and pushes it forward, then the start/end indexes need to be incremented accordingly.

In the regular WPF RichTextBox i was able to do this using TextRange.
With TextRange, i can save the selected text as a TextRange variable. Then if some text is added before the TextRange and pushes it forward in the document, the start and end TextPointer properties change accordingly.

With SfRichTextBoxAdv, when i save the selected text as a SelectionAdv variable, the variable updates every time the cursor moves in the document to the current cursor location (current selection). So i cant event store the selection in a variable, because it changes all the time as the cursor moves in the document. I tried storing the selection using the two TextPosition variables: richTextBoxAdv.Selection.Start and richTextBoxAdv.Selection.End, and storing them in my own variables, But they still update themselves when the cursor moves just like SelectionAdv.

7 Replies

VM Venkatesan Mani Syncfusion Team December 26, 2017 12:45 PM UTC

Hi Jimmi,

Thank you for contacting Syncfusion support.

In our SfRichTextBoxAdv control, ‘Start’ and ‘End’ properties of ‘SelectionAdv’ denotes the current cursor position. Hence, it will be updated automatically whenever the cursor is moved. However, you can store the current text position using ‘GetHierarchicalIndex’ property of ‘TextPosition’ class which will return a string. Later you can retrieve the text position using stored hierarchical index by using ‘GetTextPosition(string hierarchicalIndex)’ method of ‘DocumentAdv’ class. Kindly refer our class reference documentation from following link. 
  

We have prepared a simple sample to demonstrate to preserve the selection in a button click and method to get the text position from the hierarchical index. 

Sample Code(C#): 
List<SelectionRangeExt> selectionRanges = new List<SelectionRangeExt>(); 
/// <summary> 
/// Saves the selection start and end indexes into collection. 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    SelectionRangeExt range = new SelectionRangeExt(); 
    range.Start = richTextBoxAdv.Selection.Start.GetHierarchicalIndex; 
    range.End = richTextBoxAdv.Selection.End.GetHierarchicalIndex; 
    selectionRanges.Add(range); 
} 
/// <summary> 
/// Gets the TextPosition from the given hierarchical index. 
/// </summary> 
/// <param name="hierarchicalIndex">The hierarchical index.</param> 
/// <returns>The <see cref="TextPosition"/> instance.</returns> 
public TextPosition GetTextPoistion(string hierarchicalIndex) 
{ 
    if (hierarchicalIndex == null) 
        return null; 
    return richTextBoxAdv.Document.GetTextPosition(hierarchicalIndex); 
} 

Sample link:
Sample.zip.

The hierarchal index of the text position is static value and it will not be updated when the text is added or modified before the text position.

Regards,
Venkatesan M. 



JD Jimmi Day December 26, 2017 02:33 PM UTC

"The hierarchal index of the text position is static value and it will not be updated when the text is added or modified before the text position."

^ That's exactly my issue, i want to keep track of the position of the selected text, even if the text before it was modified. So i can later on insert text into the selection. Isn't there any way i can accomplish this functionality?


VM Venkatesan Mani Syncfusion Team December 27, 2017 01:40 PM UTC

Hi Jimmi,

Thank you for your update.

At present, SfRichTextBoxAdv does not support dynamic update of TextPosition instance. We have used TextPosition instance only for holding the static positions of selection start and end.  
Could you please share your complete requirement (in detail) with SfRichTextBoxAdv? So that, we can analyze in line with your requirement and provide our suggestion for direct solutions using SfRichTextBoxAdv control.
 
Regards,
Venkatesan M. 



JD Jimmi Day replied to Venkatesan Mani December 27, 2017 03:42 PM UTC

Hi Jimmi,

Thank you for your update.

At present, SfRichTextBoxAdv does not support dynamic update of TextPosition instance. We have used TextPosition instance only for holding the static positions of selection start and end.  
Could you please share your complete requirement (in detail) with SfRichTextBoxAdv? So that, we can analyze in line with your requirement and provide our suggestion for direct solutions using SfRichTextBoxAdv control.
 
Regards,
Venkatesan M. 


Hi, thanks for the response!

Well, the app is kind of an event manager. Every event has some details inside of it (i.e date, description, name, location, etc..). I want to use the SfRichTextBoxAdv to let the user make text report templates that can be used for every event. I want the user to be able to select different sections in the document and save their location/position to some kind of collection. Later these sections will be replaced with details from the event. I want those positions to stay true even if the user is editing the document after saving the selections.

For example a template could be:

----------------------------------EXAMPLE-------------------------------------
Dear client,

The event named name will be taken place in location in date.
Event details: details.
----------------------------------EXAMPLE-------------------------------------

The parts colored red are selections the user can make and save to some kind of list.

For now, i managed to do this using somekind of markdown mechanisem. so the user can wrap the replacable sections in some character and then i locate them using the document search feature. But it's not ideal.

Thanks for your support!



VM Venkatesan Mani Syncfusion Team January 2, 2018 09:47 AM UTC

Hi Jimmi,

Thank you for your update.

As mentioned earlier, SfRichTextBoxAdv does not support dynamic update of TextPosition instance. The dynamic update of TextPosition instance implementation within SfRichTextBoxAdv control will impact performance based on the live instances of TextPosition class. So, we did not consider implementing the dynamic update of TextPosition instances within SfRichTextBoxAdv control. 
As a workaround, you can achieve your requirement reliably using find and replace functionality of SfRichTextBoxAdv control with slight modification in your template content. 

Please find the steps to achieve your requirement: 
1. Redefine your template content with unique placeholders (Set some protocols), which will be replaced with event data. 
For example: 
Dear client, 
The event named <<Event1:name>> will be taken place in <<Event1:location>> in <<Event1:date>>
Event details: <<Event1:details>>
2. Use the find and replace functionality of SfRichTextBoxAdv control, to replace with respective event data. 

We have prepared a sample to demonstrate the same. Please find the sample from following link and let us know if this helps you.
Sample link:
Sample.zip

Please let us know if you have any other questions.

Regards,
Venkatesan M. 



JD Jimmi Day replied to Venkatesan Mani January 2, 2018 06:01 PM UTC

Hi Jimmi,

Thank you for your update.

As mentioned earlier, SfRichTextBoxAdv does not support dynamic update of TextPosition instance. The dynamic update of TextPosition instance implementation within SfRichTextBoxAdv control will impact performance based on the live instances of TextPosition class. So, we did not consider implementing the dynamic update of TextPosition instances within SfRichTextBoxAdv control. 
As a workaround, you can achieve your requirement reliably using find and replace functionality of SfRichTextBoxAdv control with slight modification in your template content. 

Please find the steps to achieve your requirement: 
1. Redefine your template content with unique placeholders (Set some protocols), which will be replaced with event data. 
For example: 
Dear client, 
The event named <<Event1:name>> will be taken place in <<Event1:location>> in <<Event1:date>>
Event details: <<Event1:details>>
2. Use the find and replace functionality of SfRichTextBoxAdv control, to replace with respective event data. 

We have prepared a sample to demonstrate the same. Please find the sample from following link and let us know if this helps you.
Sample link:
Sample.zip

Please let us know if you have any other questions.

Regards,
Venkatesan M. 


That's exactly what i ended up doing.. Thanks for the help!



VM Venkatesan Mani Syncfusion Team January 3, 2018 04:15 AM UTC

Hi Jimmi, 

Thank you for your update.  

Please let us know if you require any further assistance. We will be happy to assist you.  

Regards, 
Venkatesan M. 


Loader.
Up arrow icon