moonlander
moonlander

Reputation: 153

Syncfusion SfRichTextBoxAdv - Replicating the WPF TextRange functionality?

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 can't even 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:

var start = richTextBoxAdv.Selection.Start;
var end = richTextBoxAdv.Selection.End;

But they still update themselves when the cursor moves just like SelectionAdv.

Upvotes: 2

Views: 496

Answers (1)

Venkatesan M
Venkatesan M

Reputation: 1

Thank you for using Syncfusion products.

In 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.

http://help.syncfusion.com/cr/cref_files/wpf/sfrichtextboxadv/Syncfusion.SfRichTextBoxAdv.WPF~Syncfusion.Windows.Controls.RichTextBoxAdv.SelectionAdv~Start.html http://help.syncfusion.com/cr/cref_files/wpf/sfrichtextboxadv/Syncfusion.SfRichTextBoxAdv.WPF~Syncfusion.Windows.Controls.RichTextBoxAdv.SelectionAdv~End.html http://help.syncfusion.com/cr/cref_files/wpf/sfrichtextboxadv/Syncfusion.SfRichTextBoxAdv.WPF~Syncfusion.Windows.Controls.RichTextBoxAdv.TextPosition~GetHierarchicalIndex.html http://help.syncfusion.com/cr/cref_files/wpf/sfrichtextboxadv/Syncfusion.SfRichTextBoxAdv.WPF~Syncfusion.Windows.Controls.RichTextBoxAdv.DocumentAdv~GetTextPosition(String).html

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

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.

For further queries, kindly contact our support from following link, https://www.syncfusion.com/support/directtrac

Regards, Venkatesan.

Upvotes: 0

Related Questions