Example E1398
Visible to All Users

How to Specify Default Document Formatting

This example illustrates two methods to specify default document formatting. The project contains two RichEditControl instances that use different default formatting options.

The main RichEditControl uses parameters specified by the Document.DefaultCharacterProperties and Document.DefaultParagraphProperties options in the RichEditControl.DocumentLoaded event handler.

The New Document button executes the CreateNewDocument method that, in turn, raises the EmptyDocumentCreated event. In the event handler, the document is generated with another set of default settings specified by the Document.DefaultCharacterProperties and Document.DefaultParagraphProperties properties.

The Show New Editor button invokes the other RichEditControl instance that uses default settings specified by the RichEditControlCompatibility.DefaultFontName and RichEditControlCompatibility.DefaultFontSize properties on application startup.

Warning

Starting with v19.2, the RichEditControl uses document themes to retrieve default document font information. As such, the RichEditControlCompatibility.DefaultFontName property will no longer affect 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.

The Load Text Document button loads a document to both RichEditControl instances. As a result, the document has different format options in each instance.

result

Files to Look At

Documentation

Example Code

DefaultDocumentSettingsExample/Form1.cs(vb)
C#
using DevExpress.XtraEditors; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using DevExpress.XtraBars; using DevExpress.XtraBars.Ribbon; using System.Linq; namespace DefaultDocumentSettingsExample { public partial class Form1 : RibbonForm { public Form1() { //set the default font for all RichEditControls in the application RichEditControlCompatibility.UseThemeFonts = false; RichEditControlCompatibility.DefaultFontSize = 12; RichEditControlCompatibility.DefaultFontName = "Times New Roman"; InitializeComponent(); richEditControl1.EmptyDocumentCreated += RichEditControl1_EmptyDocumentCreated; richEditControl1.DocumentLoaded += RichEditControl1_DocumentLoaded; } private void RichEditControl1_DocumentLoaded(object sender, EventArgs e) { //set the default font for the document loaded in the main RichEditControl Document document = richEditControl1.Document; document.DefaultCharacterProperties.FontName = "Arial"; document.DefaultCharacterProperties.FontSize = 16; } private void RichEditControl1_EmptyDocumentCreated(object sender, EventArgs e) { //change the formatting for the newly-created document Document document = richEditControl1.Document; document.DefaultCharacterProperties.ForeColor = Color.Red; document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center; document.AppendText("Document created at " + DateTime.Now.ToLongTimeString()); } private void barBtnShowMoreForm_ItemClick(object sender, ItemClickEventArgs e) { RichEditForm frm = new RichEditForm(); frm.Show(); } private void barBtnLoadDoc_ItemClick(object sender, ItemClickEventArgs e) { foreach (var form in Application.OpenForms) { if (form is XtraForm) { RichEditControl richEditControl = (form as XtraForm).Controls.OfType<RichEditControl>().FirstOrDefault(); if (richEditControl != null) richEditControl.LoadDocument("richtext.txt"); } } } private void barBtnNewDoc_ItemClick(object sender, ItemClickEventArgs e) { richEditControl1.CreateNewDocument(true); } } }
DefaultDocumentSettingsExample/Program.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace DefaultDocumentSettingsExample { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.