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

This example demonstrates how you can get the selected text in code, and modify character format attributes, such as CharacterPropertiesBase.FontName, CharacterPropertiesBase.FontSize, CharacterPropertiesBase.ForeColor, CharacterPropertiesBase.BackColor, etc.

Use the Document.Selection property to obtain the DocumentRange object specifying an end-user selection. Then, call the SubDocument.BeginUpdateCharacters method for the specified range, modify the properties of the returned CharacterProperties object, and call the SubDocument.EndUpdateCharacters method to finalize the modification.

Note

Make sure that the SubDocument.BeginUpdateCharacters method always has its corresponding SubDocument.EndUpdateCharacters method to ensure proper operation of the control.

The result is shown below:

FontFormattingExample

using System.Windows;
using System.Drawing;
using DevExpress.XtraRichEdit;
using DevExpress.Office.Utils;
using DevExpress.XtraRichEdit.API.Native;

Document doc = richEditControl1.Document;
DocumentRange range = doc.Selection;
CharacterProperties cp = doc.BeginUpdateCharacters(range);
cp.FontName = "Comic Sans MS";
cp.FontSize = 18;
cp.ForeColor = Color.Yellow;
cp.BackColor = Color.Blue;
cp.Underline = UnderlineType.DoubleWave;
cp.UnderlineColor = Color.White;
doc.EndUpdateCharacters(cp);