Skip to main content

How to: Access the Rich Text Editor Controls

  • 6 minutes to read

This topic describes how to access and customize the following controls in XAF applications with the Office Module:

Platform

Control

Purpose

WinForms

RichEditControl

Used to show Rich Text documents

ASP.NET Core Blazor

DxRichEdit

Used to show Rich Text documents

ASP.NET Web Forms

ASPxRichEdit

Used to show Rich Text documents

ASP.NET Web Forms

ASPxUploadControl

Used to upload Rich Text documents on a server

Access the WinForms-Specific Control (RichEditControl)

Follow the steps below to access the RichEditControl and enable the Spell Check function.

  1. Create a custom ViewController in the WinForms module project (MySolution.Module.Win). If your solution does not contain this project, add the Controller to the WinForms application project (MySolution.Win).
  2. Add the DevExpress.SpellChecker.v23.2.dll and DevExpress.XtraSpellChecker.v23.2.dll assemblies to the project references.
  3. Override the OnActivated method and handle the RichTextServiceController.CustomizeRichEditControl event. In the event handler, create and set up the SpellChecker object and pass this object to the SpellChecker property.

File:
MySolution.Win\Controllers\WinRichEditController.cs in solutions without the WinForms-specific module project. MySolution.Module.Win\Controllers\WinRichEditController.cs(.vb) in solutions with the WinForms-specific module project.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Win;
using DevExpress.XtraRichEdit;
using DevExpress.XtraSpellChecker;

public partial class WinRichEditController : ViewController {
// ...
    protected override void OnActivated() {
        base.OnActivated();
        RichTextServiceController controller = Frame.GetController<RichTextServiceController>();
        if(controller != null) {
            controller.CustomizeRichEditControl += Controller_CustomizeRichEditControl;
        }
    }
    private void Controller_CustomizeRichEditControl(object sender, CustomizeRichEditEventArgs e) {
        SpellChecker spellChecker = new SpellChecker();
        spellChecker.SetSpellCheckerOptions(e.RichEditControl, new OptionsSpelling());
        spellChecker.SpellCheckMode = SpellCheckMode.AsYouType;
        e.RichEditControl.SpellChecker = spellChecker;
    }
}

Note

The English dictionary is the default for the Spell Checker. Refer to the Dictionaries topic to see how to change the dictionary.

Access ASP.NET Web Forms-Specific Controls

Access the ASPxRichEdit Control

Follow the steps below to access the ASPxRichEdit control and enable the Spell Check function.

  1. Create a custom ViewController in the ASP.NET Web Forms module project (MySolution.Module.Web).
  2. Add the DevExpress.Web.ASPxSpellChecker.v23.2.dll assembly to the project references.
  3. Override the OnActivated method and handle the ASPxRichTextPropertyEditor.ControlCreated event. In the event handler, create and set up the ASPxSpellCheckerOpenOfficeDictionary object and add this object to the Dictionaries collection. Set the Enabled property to true to enable the Spell Checker component.

File: MySolution.Module.Web\Controllers\WebRichEditController.cs(.vb).

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Web;
using DevExpress.Web.ASPxRichEdit;
using DevExpress.Web.ASPxSpellChecker;
using System;
using System.Globalization;
// ...
public class WebRichEditController : ViewController<DetailView> {
    protected override void OnActivated() {
        base.OnActivated();
        foreach(ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
            if(editor.ASPxRichEditControl != null) {
                CustomizeRichEditControl(editor.ASPxRichEditControl);
            } else {
                editor.ControlCreated += Editor_ControlCreated;
            }
        }
    }
    private void Editor_ControlCreated(object sender, EventArgs e) {
        ASPxRichEdit richEdit = ((ASPxRichTextPropertyEditor)sender).ASPxRichEditControl;
        CustomizeRichEditControl(richEdit);
    }
    private void CustomizeRichEditControl(ASPxRichEdit richEdit) {
        ASPxSpellCheckerOpenOfficeDictionary dictionary = new ASPxSpellCheckerOpenOfficeDictionary();
        dictionary.GrammarPath = "~/App_Data/Dictionaries/en_US.aff";
        dictionary.DictionaryPath = "~/App_Data/Dictionaries/en_US.dic";
        dictionary.Culture = new CultureInfo("en-US");
        dictionary.CacheKey = "enDic";
        richEdit.Settings.SpellChecker.Dictionaries.Add(dictionary);
        richEdit.Settings.SpellChecker.Enabled = true;
        richEdit.Settings.SpellChecker.SuggestionCount = 4;
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        foreach(ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
            editor.ControlCreated -= Editor_ControlCreated;
        }
    }
}

Access the ASPxUploadControl

Follow the steps below to access the ASPxUploadControl and set the maximum size of the file to be uploaded to the server.

  1. Create a custom ViewController in the ASP.NET Web Forms module project (MySolution.Module.Web).
  2. In the overridden OnActivated method, handle the ASPxSpreadsheetPropertyEditor.ControlCreated event.
  3. In the event handler, use the MaxFileSize property to specify the maximum size of the file to be uploaded to the server.

File: MySolution.Module.Web\Controllers\WebRichEditUploadController.cs(.vb).

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Web;
using DevExpress.Web;
using System;
// ...
public class WebRichEditUploadController : ViewController<DetailView> {
    protected override void OnActivated() {
        base.OnActivated();
        foreach (ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
            if (editor.ASPxUploadControl != null) {
                CustomizeRichEditUploadControl(editor.ASPxUploadControl);
            }
            else {
                editor.ControlCreated += Editor_ControlCreated;
            }
        }
    }
    private void Editor_ControlCreated(object sender, EventArgs e) {
        ASPxUploadControl richEditUploadControl = ((ASPxRichTextPropertyEditor)sender).ASPxUploadControl;
        CustomizeRichEditUploadControl(richEditUploadControl);
    }
    private void CustomizeRichEditUploadControl(ASPxUploadControl uploadControl) {
        uploadControl.ValidationSettings.MaxFileSize = 104857600;
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        foreach (ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
            editor.ControlCreated -= Editor_ControlCreated;
        }
    }
}

Access the ASP.NET Core Blazor-Specific Control’s Component Model (DxRichEditModel)

Follow these steps to access the DxRichEditModel component model. A component model is the representation of a Blazor component in code. When you modify the model, the underlying component reflects these changes. For more information about component models, refer to the following topic: Underlying Controls and Components Behind UI Elements (ASP.NET Core Blazor).

  1. Create a custom ViewController in the ASP.NET Core Blazor module project (MySolution.Module.Blazor). If your solution does not contain this project, add the Controller to the application project (MySolution.Blazor.Server).
  2. Override the OnActivated method and handle the RichTextPropertyEditor.ControlCreated event.
  3. In the event handler, access the DevExpress.ExpressApp.Office.Blazor.Components.Models.DxRichEditModel component model and specify its ViewType property to change a document view layout type (see ViewType).

File:
MySolution.Blazor.Server\Controllers\BlazorRichEditController.cs in solutions without the ASP.NET Core Blazor-specific module project. MySolution.Module.Blazor\Controllers\BlazorRichEditController.cs in solutions with the ASP.NET Core Blazor-specific module project.

using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Blazor.Components.Models;
using DevExpress.ExpressApp.Office.Blazor.Editors;
using DevExpress.ExpressApp.Office.Blazor.Editors.Adapters;
//...
public class BlazorRichEditController : ViewController<DetailView> {
    protected override void OnActivated() {
        base.OnActivated();
        foreach(RichTextPropertyEditor editor in View.GetItems<RichTextPropertyEditor>()) {
            if(editor.Control != null) {
                CustomizeRichEditComponentModel(((RichTextEditorComponentAdapter)editor.Control).ComponentModel);
            }
            else {
                editor.ControlCreated += Editor_ControlCreated;
            }
        }
    }
    private void Editor_ControlCreated(object sender, EventArgs e) {
        CustomizeRichEditComponentModel(((RichTextPropertyEditor)sender).ComponentModel);
    }
    private void CustomizeRichEditComponentModel(DxRichEditModel richEditModel) {
        richEditModel.ViewType = DevExpress.Blazor.RichEdit.ViewType.Simple;
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        foreach(RichTextPropertyEditor editor in View.GetItems<RichTextPropertyEditor>()) {
            editor.ControlCreated -= Editor_ControlCreated;
        }
    }
}
See Also