Getting position of selected text within current span

5 posts, 0 answers
  1. Elizabeth
    Elizabeth avatar
    3 posts
    Member since:
    Apr 2013

    Posted 05 Nov 2014 Link to this post

    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
      
  2. Boby
    Admin
    Boby avatar
    595 posts

    Posted 10 Nov 2014 Link to this post

    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.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Elizabeth
    Elizabeth avatar
    3 posts
    Member since:
    Apr 2013

    Posted 10 Nov 2014 in reply to Boby Link to this post

    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
  5. Boby
    Admin
    Boby avatar
    595 posts

    Posted 11 Nov 2014 Link to this post

    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.

     
  6. Elizabeth
    Elizabeth avatar
    3 posts
    Member since:
    Apr 2013

    Posted 11 Nov 2014 Link to this post

    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready