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

Set selected text background color

5 Answers 242 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
RaTT
Top achievements
Rank 1
RaTT asked on 21 Jun 2010, 05:46 PM
Hi.
How can one programmaticaly select some text in RadRichTextBox and then change the selection's background color?

Best regards, Rubin Attack.

5 Answers, 1 is accepted

Sort by
0
Alex
Telerik team
answered on 22 Jun 2010, 10:02 AM
Hi RaTT,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
RaTT
Top achievements
Rank 1
answered on 22 Jun 2010, 10:30 AM
Thanks a lot. And is it possible to set the DocumentPosition depending on the offset? For example, i need to select 5 characters starting from the 4-th character. So first I have to set DocumentPosition to the 4-th char. How can i do it?
0
Alex
Telerik team
answered on 22 Jun 2010, 12:13 PM
Hello RaTT,

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);
}

Greetings,
Alex
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
RaTT
Top achievements
Rank 1
answered on 22 Jun 2010, 03:26 PM
Hello, again. And is there a way to determine, what is the selection's start or selection's end absolute position (int) in the document? For example I have a text with a title, and I know the title's length. Also I have some selection, and I need to determine, if the part of the title is selected or not. Or there are other ways to do it?
0
Alex
Telerik team
answered on 24 Jun 2010, 09:14 AM
Hi RaTT,

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.

Regards,
Alex
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
RaTT
Top achievements
Rank 1
Answers by
Alex
Telerik team
RaTT
Top achievements
Rank 1
Share this question
or