Skip to main content

How to: Change Formatting of Selected Text

  • 2 minutes to read

This code sample demonstrates how you can obtain and modify selected text.

The Document.Selection property obtains the selected text range.

Call the SubDocument.BeginUpdateCharacters method for the specified range, modify the CharacterProperties object properties, and subsequently call the SubDocument.EndUpdateCharacters method to finalize the modification.

The BeginUpdateDocument - EndUpdateDocument method pair allows you to update the SubDocument that contains selected text range.

The following code snippet changes the font and the color of selected text on a button click:


View Example

using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
using DevExpress.XtraBars;

private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
    // Obtain selected range
    DocumentRange range = richEditControl.Document.Selection;

    // Start the update
    SubDocument document = range.BeginUpdateDocument();

    // Obtain character properties
    CharacterProperties cp = document.BeginUpdateCharacters(range);
    cp.FontName = "Comic Sans MS";
    cp.FontSize = 18;
    cp.ForeColor = Color.Blue;
    cp.BackColor = Color.Snow;
    cp.Underline = UnderlineType.DoubleWave;
    cp.UnderlineColor = Color.Red;

    // Finalize modifications