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

Getting position of selected text within current span

4 Answers 601 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Elizabeth
Top achievements
Rank 1
Elizabeth asked on 06 Nov 2014, 12:00 AM
Hi,

In the RadRichTextBox I'm working with, I am listening to the SelectionChanged event when the user selects a piece of a string. I need to determine, within the span.Text in which the string is located, what is the position (index) within the Span.Text of the selected string.

So, for example, if the span.Text contains "This is a piece of text"

and the user selects "piece"

I want to write code that returns "10", which is the index of "piece" within "This is a piece of text"

I have tried various properties that I hoped would get me this number - eg

var range = t.Document.Selection.Ranges.First;
if (range != null)
{

       string curSpanText = t.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.Text;
       string s = t.Document.Selection.GetSelectedText();
       int start = range.StartPosition.GetCurrentPositionInSpan();
       int end = range.EndPosition.GetCurrentPositionInSpan();
}

I hoped that "start" above would give me what I needed but it doesn't.

s gets me the string I had selected. curSpanText is the entire span's Text property. How do I find the position of s within curSpanText?

Thanks
  

4 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 10 Nov 2014, 11:58 AM
Hello Elizabeth,
The name of the GetCurrentPositionInSpan method is misleading, as it actually returns the index in the current SpanLayoutBox.
Following is the code of the method which gets the index of a DocumentPosition in the current span:
private int GetIndexInCurrentSpan(DocumentPosition position)
{
    int positionCounter = 0;
 
    var currentSpanBox = position.GetCurrentSpanBox();
    if (currentSpanBox != null)
    {
        foreach (SpanLayoutBox spanLayoutBox in currentSpanBox.AssociatedSpan.GetAssociatedLayoutBoxes().Cast<SpanLayoutBox>())
        {
            if (spanLayoutBox == currentSpanBox)
            {
                positionCounter += position.GetCurrentPositionInSpan();
                break;
            }
            else
            {
                positionCounter += spanLayoutBox.Text.Length;
            }
        }
    }
 
    return positionCounter;
}

Could you also share more details about your scenario? We may be able to suggest better approach, as the mapping between position index and position in text is not always straight-forward - for example if there are images, tables, annotations (comments, bookmarks, etc.) or similar elements.

Regards,
Boby
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Elizabeth
Top achievements
Rank 1
answered on 10 Nov 2014, 12:40 PM
Hi Bobby - that method may in fact do exactly what I need! Where do I get the "position" parameter from?

More information: This is an otherwise simple case in which I am programmatically highlighting a piece of selected text for a particular purpose within a span in a paragraph (no images, no nothing). I want to use the text to 'divide the span into 3 parts: before the text, the text, and after the text. However, the restriction is that if a piece of text appears multiple times I need to identify the instance the user selected. So, knowing the index within the string would do it.

I would like to try your method if I know how to send in the position needed in the parameter.

Thanks!

Elizabeth
0
Boby
Telerik team
answered on 11 Nov 2014, 05:26 PM
Hi Elizabeth,
DocumentPosition is the class used in RadDocument.CurrentPosition and start/end of the selection ranges ('range.StartPosition'); you can obtain it from there or create it with one of its constructors.

To your scenario: new span is automatically split from the existing one on editing operations where needed. Classes like RadRichTextBox and RadDocumentEditor use the current Selection for the editing operations, so for example the following code will create new span(s) from the selected text and will set their HighlightColor property:
// This could also be the RadRichTextBox which contains the 'document' instance.
RadDocumentEditor editor = new RadDocumentEditor(document);
  
// This changes the highlight only for the selection (document.Selection).
editor.ChangeHighlightColor(Colors.AliceBlue);


Regards,
Boby
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Elizabeth
Top achievements
Rank 1
answered on 11 Nov 2014, 06:00 PM
Thanks very much, Boby! Both of these were very helpful. The code sample you sent before worked well for me and I'll try the other as well. Much appreciated.
Tags
RichTextBox
Asked by
Elizabeth
Top achievements
Rank 1
Answers by
Boby
Telerik team
Elizabeth
Top achievements
Rank 1
Share this question
or