windowsforms-5890-controls-and-libraries-rich-text-editor-examples-formatting-how-to-change-formatting-of-selected-text.md
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:
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);
}
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Drawing
Imports DevExpress.XtraBars
Private Sub barButtonItem1_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
' Obtain selected range
Dim range As DocumentRange = richEditControl.Document.Selection
' Start the update
Dim document As SubDocument = range.BeginUpdateDocument()
' Obtain character properties
Dim cp As CharacterProperties = 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)
End Sub