Skip to main content

RepositoryItemTokenEdit.AddEnum(Type, Boolean) Method

Populates the editor with tokens created based on values of an enumeration. Allows the created tokens to store the enumeration’s underlying integer constants instead of named constants.

Namespace: DevExpress.XtraEditors.Repository

Assembly: DevExpress.XtraEditors.v23.2.dll

NuGet Package: DevExpress.Win.Navigation

Declaration

public void AddEnum(
    Type enumType,
    bool addEnumeratorIntegerValues
)

Parameters

Name Type Description
enumType Type

The type of an enumeration that is used to create tokens.

addEnumeratorIntegerValues Boolean

true if the created tokens should store the enumeration’s underlying integer constants; false if tokens should store named constants.

Remarks

You can populate the TokenEdit with items created based on an enumeration. Call the control’s AddEnum method and use the following parameters to specify how to create and display items:

  • addEnumeratorIntegerValues

    Token items expose the Description and Value properties that specify the description displayed to users and the underlying value. When a user selects an item, the value is assigned to the editor’s EditValue property.

    An enumeration is a set of named constants of the underlying integral numeric type. When you populate an editor with items based on an enumeration, an item’s Description and Value properties are set to the corresponding enumeration value’s named constant. Set the addEnumeratorIntegerValues parameter to true to assign to the Value property the corresponding integer value of the underlying type.

    Note

    An exception is thrown if you set the AddEnum(Type, Boolean) method’s addEnumeratorIntegerValues parameter to true while the EditValueType option is set to Enum. Set the EditValueType option to String or List, or set the addEnumeratorIntegerValues parameter to false.

  • skipComposite

    Enable this parameter to prevent items from being created based on enumeration constants that are combinations of others. Refer to the Non-exclusive members and the Flags attribute article section to learn more.

  • skipNone

    Enable this parameter to prevent items from being created based on enumeration constants that have no integer value assigned explicitly.

The figure below illustrates DevExpress editors populated with items based on an enumeration.

Editors Populated From Enumerators

Example

The following example shows how to populate the CheckedListBoxControl and ImageListBoxControl with items using the BaseListBoxControl.AddEnum and BaseListBoxControl.AddEnum<TEnum> methods.

The image below shows the result of executing the code in the example. It is assumed that the controls are dropped on the form at design time.

BaseListBoxControl_AddEnum

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public enum Day {
        // The Description attribute can be used
        // to provide descriptions for items.
        [Description("Saturday")]
        Sat = 7,
        Sun = 1,
        Mon,
        Tue,
        Wed,
        Thu,
        Fri
    };
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        public Form1() {
            InitializeComponent();
            // Populates the CheckedListBoxControl with items
            // generated based on the Day enumeration constants.
            // The second parameter set to true specifies
            // item values to be set to enumeration constant
            // integer values instead of constant names.
            checkedListBoxControl1.AddEnum(typeof(Day), true);

            // Populates the ImageListBoxControl with items
            // generated based on the Day enumeration constants.
            // The method parameter specifies the delegate 
            // that encapsulates the method providing
            // custom item descriptions.
            imageListBoxControl1.AddEnum<Day>(new Converter<Day,string>(MyConverter));
        }

        // Returns a custom string based on the specified day.
        public static string MyConverter(Day d) {
            Type dayType = d.GetType();
            Type dayUnderlyingType = Enum.GetUnderlyingType(dayType);
            return Convert.ChangeType(d, dayUnderlyingType).ToString() + ". " + d.ToString();
        }
    } 
}
See Also