New to Telerik UI for WinForms? Download free 30-day trial

Formatting API

If you want to format the content of RadRichTextEditor at run time, you have to use the API exposed by RadRichTextEditor. This is essential, as the main purpose of RadRichTextEditor is to allow the users to format their input via UI. The UI should call the respective API methods of the control.

Changing the Text Formatting

RadRichTextEditor exposes methods that change the style of the selected text or the paragraph. When a method is called, the respective style is applied to the selected text. If there is no selection available, the style is applied to the word in which the caret is located.

Here is an example of a toggle button that upon checking should make the selection or the current word bold. In the handler of the Click event of RadToggleButton, the ToggleBold() method of RadRichTextEditor is called.


private void togglebutton_Click(object sender, EventArgs e)
{
    this.radRichTextEditor.ToggleBold();
}

Private Sub togglebutton_Click(ByVal sender As Object, ByVal e As EventArgs)
    Me.radRichTextEditor.ToggleBold()
End Sub

Using the active editor

RadRichTextEditor supports headers and footers. They are represented through separate instances of RadRichTextEditor. When a document has headers and footers you can use the ActiveDocumentEditor property of RadRichTextEditor to get the editor instance where the caret is currently situated.

You can find more about the Header and Footer functionality in this article.

The following example inserts the word "text" at the CaretPosition.


this.radRichTextEditor.Insert("text");

Me.radRichTextEditor.Insert("text")

Creating a DocumentFragment

One of the common uses of the API is creating and inserting a DocumentFragment. Currently, you can create a fragment in two ways:

  • through DocumentFragment's constructor;

  • through selection.

Both approaches can be used to insert content at the caret position with the InsertFragment method:


this.radRichTextEditor.InsertFragment(fragment);

Me.radRichTextEditor.InsertFragment(fragment)

Using the constructor of DocumentFragment

If you create a fragment in this way, it will end with a new paragraph. This is convenient when you want to separate the inserted fragment and end it with a new line. Furthermore, in this way if the last paragraph is in a list, it will appear properly in the new document.

DocumentFragment fragmentFromDocument = new DocumentFragment(this.radRichTextEditor.Document);

Dim fragmentFromDocument As New DocumentFragment(Me.radRichTextEditor.Document)

This is also the suggested approach when merging several documents into one.

Using the selection

If you choose to use the document selection when creating a DocumentFragment, there will be no additional paragraph after the fragment.

DocumentFragment fragmentFromSelection = this.radRichTextEditor.Document.Selection.CopySelectedDocumentElements();

Dim fragmentFromSelection As DocumentFragment = Me.radRichTextEditor.Document.Selection.CopySelectedDocumentElements()

See Also

In this article