Extract text from caret position

13 posts, 1 answers
  1. Joël
    Joël avatar
    32 posts
    Member since:
    Sep 2011

    Posted 24 Oct 2011 Link to this post

    Hello, I need to get the text under the caret, my position has to check a text format and retrieve it. This format is "Code, delimiter, keywords"

    Example: "CODE1;,;CODE2;?;CODE3"

    When the user presses F11, a function must retrieve the text regardless of the position of the caret in the text (beginning, middle, end)

    I tried, but every time I have only pieces of text, I can not get the entire text.
  2. Joël
    Joël avatar
    32 posts
    Member since:
    Sep 2011

    Posted 26 Oct 2011 Link to this post

    Still no idea for me ?
  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Boby
    Admin
    Boby avatar
    595 posts

    Posted 27 Oct 2011 Link to this post

    Hello Laurent,
    You can use the GetSelectedText method, for example:
    DocumentPosition pos = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
     
    pos.MoveToCurrentWordStart();
    this.radRichTextBox.Document.Selection.SetSelectionStart(pos);
     
    pos.MoveToCurrentWordEnd();
    this.radRichTextBox.Document.Selection.AddSelectionEnd(pos);
     
    string selectedText = this.radRichTextBox.Document.Selection.GetSelectedText();

    Don't hesitate to contact us if you have other questions.


    Greetings,
    Boby
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Joël
    Joël avatar
    32 posts
    Member since:
    Sep 2011

    Posted 27 Oct 2011 Link to this post

    It's perfect, thanks.
  6. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 02 May 2012 Link to this post

    Hi,

    How do I select the range of a complete span?

    Thanks,

    Rob
  7. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 02 May 2012 Link to this post

    I've tried the following code, however the loop doesn't stop at the start of the span, it keeps on looping all of the way through the document which isn't what I want it to do.

    //Select text inserted
    DocumentPosition startPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
    DocumentPosition endPosition = new DocumentPosition(startPosition);
     
    startPosition.MoveToCurrentWordStart();
     
    //Loop back through span words - ** needs to stop at end of span **
    do
    {
        MessageBox.Show(startPosition.GetCurrentWord().ToString() + " END: " + endPosition.GetCurrentWord().ToString());
        int pos = startPosition.GetCurrentPositionInSpan();
     
    } while (startPosition.MoveToPreviousWordStart());

    I even tried the GetCurrentPositionInSpan method but it doesn't do anything. The index value returned is always 0.

    Any idea how I can stop the loop when the start of the span has been found?

    Thanks,

    Rob




  8. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 02 May 2012 Link to this post

    I have managed to figure out a temporary work around. Essentially I loop through the span backwards until I build up a string that matches the string of the text I have just inserted.

    //Insert text from selected ListBox item
    radRichTextBox.Document.Insert(((Products)radListBox.SelectedItem).Name, radRichTextBox.Document.Style);
    //Setup/Get positions of text inserted (end position of inserted text)
    DocumentPosition startPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
    DocumentPosition endPosition = new DocumentPosition(startPosition);
    //Move start position to the start of last word
    startPosition.MoveToCurrentWordStart();
    //Loop back through current span words - ** needs to stop at end of span **
    do
    {
    //Select text in document based on start & end positions
    this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
    this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
    //Check if selected document text matches the selected autocomplete listbox item text
    //****** THIS MAY NEED CHANGING WHEN TEXT IS ENTERED BY KEYBOARD AND NOT SELECTED IN LISTBOX AUTOCOMPLETE *****
    if (this.radRichTextBox.Document.Selection.GetSelectedText() == ((Products)radListBox.SelectedItem).Name)
    {
    break;
    }
    } while ((startPosition.MoveToPreviousWordStart()));

    If anyone has any better suggestions on how to programmatically select all text within a specific span then I'm all ears.

    Thanks.

  9. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 04 May 2012 Link to this post

    Hi Rob,

    As discussed in the support ticket you have opened on the same topic, a whole span can be selected in the following way:
    DocumentPosition startPosition = new DocumentPosition(this.editor.Document.CaretPosition);
    DocumentPosition endPosition = new DocumentPosition(startPosition);
    startPosition.MoveToInline(startPosition.GetPreviousInlineBox().AssociatedInline.FirstLayoutBox as InlineLayoutBox, 0);
      
    this.editor.Document.Selection.SetSelectionStart(startPosition);
    this.editor.Document.Selection.AddSelectionEnd(endPosition);

    I am just posting it here in case anyone else stumbles across the same problem.

    Regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  10. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 04 May 2012 Link to this post

    Thanks Iva.
  11. Xavier
    Xavier avatar
    2 posts
    Member since:
    Oct 2012

    Posted 26 Nov 2012 Link to this post


    var richText = sender as ILRichTextBox;
    var pos = new DocumentPosition(richText.Document.CaretPosition);
     
    pos.MoveToCurrentWordStart();
    richText.Document.Selection.SetSelectionStart(pos);
     
    pos.MoveToCurrentWordEnd();
    richText.Document.Selection.AddSelectionEnd(pos);
     
    var textSearch = richText.Document.Selection.GetSelectedText();

    After installing
    version 2012.3.1023.40, the code shown no longer works, the function always returns empty. Is that there would be a new way?
  12. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 27 Nov 2012 Link to this post

    Hi Laurent,

    I tried the code from the snippet with the latest version and a few previous ones. The only difference I found is when the caret is positioned at the end of a paragraph. Can you confirm this is the case on your end, too?

    Greetings,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  13. Xavier
    Xavier avatar
    2 posts
    Member since:
    Oct 2012

    Posted 28 Nov 2012 Link to this post

    Yes, it is. My users enter their text and can replace a code entered by a complete text, he do not back the word, it is necessary that this function is the word when it comes to typing.
  14. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 30 Nov 2012 Link to this post

    Hello Laurent,

    I am not sure if it is a bug, because normally the "current" word/inline box at a position is the word that starts at this position. In this case, the "current" word/inline box would be the paragraph-end. Therefore, move to current word start would technically mean move to the start of the paragraph-end. In that regard, the current behavior would be the more correct one compared to the behavior of the previous version.

    In any case, you can decide for yourself which word should be selected when the caret is at the end of a word. For example, the following code will select the word that the caret is in. Then, if the caret is at the beginning or the end of a word, it will still be selected:

    var pos = new DocumentPosition(richText.Document.CaretPosition);
     
    if (pos.GetCurrentInlineBox() is FormattingSymbolLayoutBox)
    {
        pos.MoveToPrevious();
    }
    pos.MoveToCurrentWordStart();
    richText.Document.Selection.SetSelectionStart(pos);
     
    pos.MoveToCurrentWordEnd();
    richText.Document.Selection.AddSelectionEnd(pos);
     
    var textSearch = richText.Document.Selection.GetSelectedText();

    I hope this helps.

    Greetings,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
UI for WPF is Visual Studio 2017 Ready