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

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


How do I select the range of a complete span?
Thanks,
Rob

//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

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


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?
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?
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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.