Skip to main content

Custom Editors

  • 11 minutes to read

In this help topic you will learn how to create custom editors for standalone and in-place editing (e.g., in GridControl, TreeList, and BarManager).

General Information

Creating a custom editor involves three steps:

  1. Create a custom editor class

    If you need to implement completely new functionality for your editor, you can inherit from the BaseEdit class. The BaseEdit class is the root ancestor for all data editors in the XtraEditors library. You can also inherit from the appropriate data editor (for example, DateEdit, ComboBoxEdit) if you want to extent their functionality.

  2. Create a custom repository item class to store editor-specific properties

    Each data editor in the XtraEditors library has an associated repository item. Repository items store editor-specific settings and event handlers. For example, settings for a DateEdit control are encapsulated by the RepositoryItemDateEdit class, while settings for an ImageComboBoxEdit editor are encapsulated by the RepositoryItemImageComboBox class. Repository items derived from the base RepositoryItem class.

    You must create a repository item class for your custom editor. The repository item class should override the EditorTypeName property and introduce new settings, events, or redefine the processing of default settings. If you derive your custom editor from the BaseEdit class, you should create a new repository item class. To do this, derive it from the RepositoryItem class. For example, if you create a DateEdit descendant, you should inherit the new repository item from the RepositoryItemDateEdit class.

    You should also override the custom editor’s Properties property to return the corresponding repository item.

  3. Register the custom editor and custom repository item

    Custom editors must be registered in an internal registration collection so that they can then be accessed using the PersistentRepository component or any container control (for example, GridControl, Bars, etc.). The registration code must be implemented as a static method of the repository item class.

You can create the custom editor and repository item manually, or generate them automatically using the Custom Data Editor template from the Template Gallery. To open the Custom Data Editor template, right-click your project in the Solution Explorer and select Add DevExpress Item -> New Item menu command:

CustomEditor-LaunchTemplateGallery

In the project gallery, select the Custom Data Editor template. The template allows you to select the base editor and specify additional options:

CustomEditor-TemplateGallery

Enable the following options to generate infrastructure classes for a custom editor:

  • Painter - implements API that paints the custom editor.
  • ViewInfo - calculates and stores the custom editor’s visual information.
  • PopupForm - allows you to display custom content within the custom editor’s dropdown window.

If you do not want to change the default UI rendering or behavior in your custom editor, uncheck the corresponding options.

Generally, you place the code that implements custom editors in a separate library. For instance, after compiling the library, it can be added to an application project by selecting PROJECT | Add Reference in the main menu of Visual Studio. From then on, the custom editor can be accessed by the PersistentRepository‘s Designer, or any container control’s Designer.

Note

The Designer searches for custom editors attached to the application only when the Designer is opened for the first time. If you open the Designer and add a reference to the editors library, the editor will not be displayed the next time the Designer is opened. Reload the project to see custom editors that have been added.

Custom Editor Class

  • Each custom editor must override the EditorTypeName property and return a unique name that identifies the editor. The editor’s EditorTypeName property and the repository item’s EditorTypeName property should return the same value. This value should be used when registering the custom editor in the XtraEditors library (see below).
  • If a new repository item class is created for a custom editor, you should override the custom editor’s Properties property. You should override the get method to return the Properties object type of the base class - cast to a corresponding repository item type.
  • A custom editor must also implement a static constructor that calls the registration code.
[ToolboxItem(true)]
public class CustomDateEdit : DateEdit
{
    static CustomDateEdit()
    {
        RepositoryItemCustomDateEdit.RegisterCustomDateEdit();
    }

    public CustomDateEdit()
    {
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemCustomDateEdit Properties => base.Properties as RepositoryItemCustomDateEdit;

    public override string EditorTypeName => RepositoryItemCustomDateEdit.CustomEditName;

    protected override PopupBaseForm CreatePopupForm()
    {
        return new CustomDateEditPopupForm(this);
    }
}

Tip

To display a custom editor in the Toolbox, apply the ToolboxItem(true) attribute to the custom editor class.

Custom Repository Item Class

  • Override the EditorTypeName property to return a unique name that identifies the custom editor. The name must match the name returned by the custom editor’s EditorTypeName property.
  • Implement a static constructor that calls the registration code. The registration code must be placed in a separate static method.
  • If you add new properties or events to the custom repository item, override the Assign method that copies settings from a repository item passed as the method’s parameter to the current repository item.
  • Add the [UserRepositoryItem("RegisterCustomEdit")] attribute to the custom repository item class (where “RegisterCustomEdit” is the name of the static registration method).
[UserRepositoryItem("RegisterCustomDateEdit")]
public class RepositoryItemCustomDateEdit : RepositoryItemDateEdit
{
    static RepositoryItemCustomDateEdit()
    {
        RegisterCustomDateEdit();
    }

    public const string CustomEditName = "CustomDateEdit";

    public RepositoryItemCustomDateEdit()
    {
    }

    public override string EditorTypeName => CustomEditName;

    public static void RegisterCustomDateEdit()
    {
        Image img = null;
        EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(CustomDateEdit), typeof(RepositoryItemCustomDateEdit), typeof(CustomDateEditViewInfo), new CustomDateEditPainter(), true, img));
    }

    public override void Assign(RepositoryItem item)
    {
        BeginUpdate();
        try
        {
            base.Assign(item);
            RepositoryItemCustomDateEdit source = item as RepositoryItemCustomDateEdit;
            if (source == null) return;
            //
        }
        finally
        {
            EndUpdate();
        }
    }
}

Register a Custom Editor

  • Place the code that registers a custom editor in a separate static method of the repository item class. The registration code links the custom editor with the corresponding repository item, view info, and painter objects. This information is used when creating both standalone and in-place editors.
  • Add the [UserRepositoryItem("RegisterCustomEdit")] attribute to the custom repository item class (where “RegisterCustomEdit” is the name of the static registration method).

To provide registration information on the custom editor, create the DevExpress.XtraEditors.Registrator.EditorClassInfo object. Use the following overload of the view info class’s constructor to supply registration information:

public EditorClassInfo(
    string name,            // Specifies the editor's name.
    Type editorType,        // Specifies the type of the editor class.
    Type repositoryType,    // Specifies the type of the custom repository item class.
    Type viewInfoType,      // Specifies the type of the editor's ViewInfo class.
    BaseEditPainter painter,// An instance of the editor's Painter class.
    bool designTimeVisible, // Specifies whether or not the custom editor 
                            // will be visible at design time within container controls 
                            // (for instance, when creating an in-place editor
                            // within Grid Control or XtraBars).
    Image image,            // Specifies the image that will be displayed along 
                            // with the editor's name within container controls.
    Type accessibleType     // Specifies the type of the object 
                            // that provides accessibility information.
);

Generally, viewInfoType and accessibleType parameters should to be set to ViewInfo and AccessibilityInformation types that correspond to the custom editor’s ancestor. For the painter parameter, you should to specify an instance of a corresponding Painter class.

After the EditorClassInfo has been created, it must be added to the static DevExpress.XtraEditors.Registrator.EditorRegistrationInfo.Default.Editors collection:

public class RepositoryItemCustomDateEdit : RepositoryItemDateEdit
{
    // ...
    public static void RegisterCustomDateEdit()
    {
        Image img = null;
        EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(CustomDateEdit), typeof(RepositoryItemCustomDateEdit), typeof(CustomDateEditViewInfo), new CustomDateEditPainter(), true, img));
    }
}

Note

The RichTextEdit is a simplified version of the RichEditControl. The RichTextEdit control is hidden from the Toolbox, and is not intended to be used as a standalone control. The RepositoryItemRichTextEdit object is public. You can use it to display RTF data in container controls such as GridControl and TreeList.

To display and edit RTF data in a standalone control, use RichEditControl.

Custom Dropdown Editors with Popup Forms

Dropdown editors support popup forms that allow users to select values or edit data. When defining a custom dropdown editor, you can override the PopupBaseEdit.CreatePopupForm method to supply a custom popup form. The following table lists popup form types used by DevExpress dropdown editors. You can derive your popup form from one of these classes:

Dropdown Editor Popup Form Type (a PopupBaseForm descendant)
CalcEdit PopupCalcEditForm
CheckedComboBoxEdit CheckedPopupContainerForm
ColorEdit PopupColorEditForm
ComboBoxEdit PopupListBoxForm
DateEdit PopupDateEditForm or VistaPopupDateEditForm
ImageComboboxEdit PopupImageComboBoxEditListBoxForm
ImageEdit ImagePopupForm
LookUpEdit PopupLookUpEditForm
MemoExEdit MemoExPopupForm
MRUEdit PopupMRUForm
PopupContainerEdit PopupContainerForm
TokenEdit TokenEditPopupForm
BreadCrumbEdit BreadCrumbPopupForm

Example - Custom TextEdit Descendant

This example creates a custom text editor (CustomEdit) that inherits the base functionality from the TextEdit. The RepositoryItemCustomEdit class implements a new UseDefaultMode property (for demo purposes).

using System.Drawing;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.Accessibility;

namespace DevExpress.CustomEditors {

    //The attribute that points to the registration method
    [UserRepositoryItem("RegisterCustomEdit")]
    public class RepositoryItemCustomEdit : RepositoryItemTextEdit {

        //The static constructor that calls the registration method
        static RepositoryItemCustomEdit() { RegisterCustomEdit(); }

        //Initialize new properties
        public RepositoryItemCustomEdit() {            
            useDefaultMode = true;
        }

        //The unique name for the custom editor
        public const string CustomEditName = "CustomEdit";

        //Return the unique name
        public override string EditorTypeName { get { return CustomEditName; } }

        //Register the editor
        public static void RegisterCustomEdit() {
            //Icon representing the editor within a container editor's Designer
            Image img = null;
            try {
                img = (Bitmap)Bitmap.FromStream(Assembly.GetExecutingAssembly().
                  GetManifestResourceStream("DevExpress.CustomEditors.CustomEdit.bmp"));
            }
            catch {
            }
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, 
              typeof(CustomEdit), typeof(RepositoryItemCustomEdit), 
              typeof(TextEditViewInfo), new TextEditPainter(), true, img, typeof(TextEditAccessible)));
        }

        //A custom property
        private bool useDefaultMode;

        public bool UseDefaultMode {
            get { return useDefaultMode; }
            set {
                if(useDefaultMode != value) {
                    useDefaultMode = value;                        
                    OnPropertiesChanged();
                }
            }
        }

        //Override the Assign method
        public override void Assign(RepositoryItem item) {
            BeginUpdate(); 
            try {
                base.Assign(item);
                RepositoryItemCustomEdit source = item as RepositoryItemCustomEdit;
                if(source == null) return;
                useDefaultMode = source.UseDefaultMode;
            }
            finally {
                EndUpdate();
            }
        }
    }

    [ToolboxItem(true)]
    public class CustomEdit : TextEdit {

        //The static constructor that calls the registration method
        static CustomEdit() { RepositoryItemCustomEdit.RegisterCustomEdit(); }

        //Initialize the new instance
        public CustomEdit() {
            //...
        }

        //Return the unique name
        public override string EditorTypeName { get { return 
            RepositoryItemCustomEdit.CustomEditName; } }

        //Override the Properties property
        //Simply type-cast the object to the custom repository item type
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemCustomEdit Properties {
            get { return base.Properties as RepositoryItemCustomEdit; }
        }

    }
}

The above registration method associates an icon with the custom editor (the icon was added as a resource to the project). The icon will be displayed at design time in the PersistentRepository‘s Designer and the container editor’s Designer.

To add an icon as a resource to the project, do the following:

  1. Right-click the project in the Solution Explorer and select Add | Existing Item…. Load the custom image that will be associated with the editor.
  2. Select the image added in the Solution Explorer. Its name must match the name of the editor. If not, rename the image (for example, by pressing the F2 key).
  3. Right-click the image in the Solution Explorer and select Properties. In the Properties window, set the Build Action item to Embedded Resource.

The full name used to refer to the icon is the value of the Default Namespace setting plus the name of the image. The Default Namespace must be set to the name of the namespace where the custom editor is declared. To change the Default Namespace setting, right click the project in the Solution Explorer and select Properties. In the invoked dialog, modify the Default Namespace setting (Root Namespace in the VB IDE). In the above example, it must be set to “DevExpress.CustomEditors”.

See Also

See Also