Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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:

FontFormattingExample

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
    document.EndUpdateCharacters(cp);
    range.EndUpdateDocument(document);
}