Back to Devexpress

How to: Specify Default Document Formatting in the Rich Text Editor

windowsforms-5813-controls-and-libraries-rich-text-editor-examples-formatting-how-to-specify-default-document-formatting.md

latest4.8 KB
Original Source

How to: Specify Default Document Formatting in the Rich Text Editor

  • Mar 15, 2023
  • 2 minutes to read

You can use one of the following approaches to specify default document formatting.

Set Default Formatting to the RichEditControl

The static RichEditControlCompatibility.DefaultFontSize and RichEditControlCompatibility.DefaultFontName properties set the default font settings for all RichEditControl instances in the application. Specify these properties before initialization of all controls, in the Main method, as illustrated in the following code.

Important

The RichEditControl uses document themes to retrieve default document font information. As such, the RichEditControlCompatibility.DefaultFontName property longer affects the default document font.

Set the RichEditControlCompatibility.UseThemeFonts property to false when starting the application to restore the previous behavior in all instances of the RichEdit components. Set the RichEditBehaviorOptions.UseThemeFonts property to false before loading a new document to disable themes for a specific component.

View Example

csharp
using DevExpress.XtraRichEdit;

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    RichEditControlCompatibility.DefaultFontSize = 8;
    RichEditControlCompatibility.DefaultFontName = "Tahoma";
    Application.Run(new Form1());
}
vb
<STAThread> _
Shared Sub Main()
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    DevExpress.XtraRichEdit.RichEditControlCompatibility.DefaultFontSize = 8
    DevExpress.XtraRichEdit.RichEditControlCompatibility.DefaultFontName = "Tahoma"
    Application.Run(New Form1())
End Sub

Set Default Formatting to the Specific Document

For the document loaded in the RichEditControl, you can specify default formatting characteristics individually using the Document.DefaultCharacterProperties and Document.DefaultParagraphProperties settings. These settings override the RichEditControlCompatibility settings.

View Example

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

private void RichEditControl_EmptyDocumentCreated(object sender, EventArgs e) {

    Document document = richEditControl.Document;
    document.DefaultCharacterProperties.ForeColor = Color.Red;
    document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center;
    document.AppendText("Document created at " + DateTime.Now.ToLongTimeString());
}
vb
Private Sub RichEditControl1_EmptyDocumentCreated(ByVal sender As Object, ByVal e As EventArgs)
    richEditControl1.Document.DefaultCharacterProperties.ForeColor = Color.Red
    richEditControl1.Document.DefaultParagraphProperties.Alignment = DevExpress.XtraRichEdit.API.Native.ParagraphAlignment.Center
    richEditControl1.Document.AppendText("Document created at " & Date.Now.ToLongTimeString())
End Sub

To copy default document formatting from one document to another, use the Assign method.

The code snippet below copies default document formatting characteristics from the document loaded in the richEditControl2 control to the document contained in the richEditControl1 :

csharp
richEditControl1.Document.DefaultCharacterProperties.Assign(richEditControl2.Document.DefaultCharacterProperties);
richEditControl1.Document.DefaultParagraphProperties.Assign(richEditControl2.Document.DefaultParagraphProperties);
vb
richEditControl1.Document.DefaultCharacterProperties.Assign(richEditControl2.Document.DefaultCharacterProperties)
richEditControl1.Document.DefaultParagraphProperties.Assign(richEditControl2.Document.DefaultParagraphProperties)