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, theRichEditControlCompatibility.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 tofalse
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.
Files to Look At
- Form1.cs (VB: Form1.vb)
- Program.cs (VB: Program.vb)
Documentation
Example Code
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);
}
}
}
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());
}
}
}