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

Highlight selected text and insert inline ui object

1 Answer 217 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 05 Jan 2011, 05:31 PM
I am trying to highlight selected text in the radrichtextbox. I was looking at an example from back in June and it appears that it is not working for me. Now I understand the method ChangeTextColor has been changed to ChangeTextHighlightColor. While the forums post says this code highlights the selected text.This code selects the current line and highlights the whole thing not the selected text. Let me know if I am doing something wrong but this code does not work for me. In addition to this requirement. I also have a requirement to insert an inline ui object. Now I can get this inserted at the end of the selection or where the caret is currently but I cannot get this inline object inserted at the beginning of the selection.  

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

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 07 Jan 2011, 11:35 AM
Hello Chris,

 The code block you have pasted actually clears the selection and highlights the line that the caret is currently positioned in.
(I assume that by 

this.radRichTextBox.ChangeTextBackColor(Colors.Green);
you mean 
this.radRichTextBox.ChangeTextHighlightColor(Colors.Green);
as there is no ChangeTextBackColor(Color color) method in RadRichTextBox's API.)
If you want to change the highlight color of the selected text, the following line is sufficient, as this method operates on the selected text:
this.radRichTextBox.ChangeTextHighlightColor(Colors.Green);

You can also use the command that RadRichTextBoxRibbonUI exposes and bind the command as shown in our demo:

<telerik:HighlightColorPicker AutomaticColor="Transparent" Height="22" NoColorText="No color" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeFontHighlightColorCommand}" SelectedColor="Yellow" Width="36" />
(Note that the whole RichTextBoxRibbonUI's DataContext is bound to the Commands property of RadRichTextBox.)

The InsertInline(Inline inline) method of RadRichTextBox inserts the inline at the current CaretPosition. If there is selected text, it is replaced. You can, however, manipulate the CaretPosition and the selection in code-behind to adapt this behavior to match your needs.
if (!this.radRichTextBox.Document.Selection.IsEmpty)
{
    this.radRichTextBox.Document.CaretPosition.MoveToPosition(this.radRichTextBox.Document.Selection.Ranges.First.StartPosition);
    this.radRichTextBox.Document.Selection.Clear();
}
InlineUIContainer containter = new InlineUIContainer(new Button(), new Size(75, 23));
this.radRichTextBox.InsertInline(containter);

If you have any other questions, do not hesitate to contact us again.

Kind regards,
Iva
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
RichTextBox
Asked by
Chris
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or