5 Answers, 1 is accepted
You can use AddSelectionStart and AddSelectionEnd methods of the RadDocument's Selection property to modify the current selection. For navigating through the document use the DocumentPosition class - it has various move methods. Finally for setting the highlight color of the current selection you can use the ChangeTextBackColor method of the RadRichTextBox. Here is how all of this comes together in a button event which highlights the current line:
private
void
HighLight_Click(
object
sender, RoutedEventArgs e)
{
this
.richTextBox.Document.Selection.Clear();
DocumentPosition position =
new
DocumentPosition(
this
.richTextBox.Document.CaretPosition);
position.MoveToCurrentLineStart();
this
.richTextBox.Document.Selection.AddSelectionStart(position);
position.MoveToCurrentLineEnd();
this
.richTextBox.Document.Selection.AddSelectionEnd(position);
this
.richTextBox.ChangeTextBackColor(Colors.Green);
}
I hope this is helpful.
Regards,
Alex
the Telerik team
You can use the MoveToNext method of DocumentPosition to move the position to the next character. Here is the modified example that selects only the first 5 characters of the line:
private
void
HighLight_Click(
object
sender, RoutedEventArgs e)
{
this
.richTextBox.Document.Selection.Clear();
DocumentPosition position =
new
DocumentPosition(
this
.richTextBox.Document.CaretPosition);
position.MoveToCurrentLineStart();
this
.richTextBox.Document.Selection.AddSelectionStart(position);
for
(
int
i = 0; i < 5; i++)
{
position.MoveToNext();
}
this
.richTextBox.Document.Selection.AddSelectionEnd(position);
this
.richTextBox.ChangeTextBackColor(Colors.Green);
}
Alex
the Telerik team
You can use DocumentSelection to get text of the editor. For example here is how you get the text of the first paragraph (and its length in characters):
DocumentPosition position =
new
DocumentPosition(
this
.richTextBox.Document);
DocumentSelection selection =
new
DocumentSelection(
this
.richTextBox.Document);
selection.AddSelectionStart(position);
position.MoveToLastPositionInParagraph();
selection.AddSelectionEnd(position);
string
text = selection.GetSelectedText();
You can also easily compare position using operators. For example here is how you can check, if position is inside selection:
private
bool
IsPositionInSlection(DocumentPosition position, DocumentSelection selection)
{
if
(selection.IsEmpty)
{
return
false
;
}
foreach
(var range
in
selection.Ranges)
{
if
(position >= range.StartPosition && position < range.EndPosition)
{
return
true
;
}
}
return
false
;
}
Finally, if you want to determine if part of the title is selected you can create section over the title and check if this selection overlaps with of the current selection in document (Selection property of the RadDocument class).
You can check our help articles for the RadRichTextBox here.
Let us know if you need further assistance.
Alex
the Telerik team