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

Extract text from caret position

12 Answers 644 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Joël
Top achievements
Rank 2
Joël asked on 24 Oct 2011, 08:53 AM
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.

12 Answers, 1 is accepted

Sort by
0
Joël
Top achievements
Rank 2
answered on 26 Oct 2011, 09:55 AM
Still no idea for me ?
0
Accepted
Boby
Telerik team
answered on 27 Oct 2011, 07:50 AM
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 >>

0
Joël
Top achievements
Rank 2
answered on 27 Oct 2011, 04:00 PM
It's perfect, thanks.
0
Robert
Top achievements
Rank 1
answered on 02 May 2012, 11:35 AM
Hi,

How do I select the range of a complete span?

Thanks,

Rob
0
Robert
Top achievements
Rank 1
answered on 02 May 2012, 03:17 PM
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




0
Robert
Top achievements
Rank 1
answered on 02 May 2012, 05:06 PM
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.

0
Iva Toteva
Telerik team
answered on 04 May 2012, 04:03 PM
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 >>

0
Robert
Top achievements
Rank 1
answered on 04 May 2012, 04:18 PM
Thanks Iva.
0
Xavier
Top achievements
Rank 1
answered on 26 Nov 2012, 08:16 AM

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?
0
Iva Toteva
Telerik team
answered on 27 Nov 2012, 06:15 PM
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.

0
Xavier
Top achievements
Rank 1
answered on 28 Nov 2012, 07:50 AM
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.
0
Iva Toteva
Telerik team
answered on 30 Nov 2012, 06:31 PM
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.

Tags
RichTextBox
Asked by
Joël
Top achievements
Rank 2
Answers by
Joël
Top achievements
Rank 2
Boby
Telerik team
Robert
Top achievements
Rank 1
Iva Toteva
Telerik team
Xavier
Top achievements
Rank 1
Share this question
or