Skip to main content
A newer version of this page is available. .

ListBoxItemCollection.AddEnum(Type, Boolean) Method

Populates this ListBoxItemCollection with items generated from values of the specific enumeration. Allows generated items to store numeric enumeration values instead of enumeration constant names. This overload should be used only for the ListBoxItemCollection class descendants.

Namespace: DevExpress.XtraEditors.Controls

Assembly: DevExpress.XtraEditors.v19.2.dll

Declaration

public void AddEnum(
    Type enumType,
    bool addEnumeratorIntegerValues
)

Parameters

Name Type Description
enumType Type

A type of enumeration whose values should be used to generate ListBoxItemCollection items.

addEnumeratorIntegerValues Boolean

true if values for generated items should store numeric enumerator values ; false if these values should store constant enumerator names instead. This parameter is not in effect when the method is called for ListBoxControl objects, since their child items do not have the Value-Description property pair.

Remarks

Many DevExpress editors and controls provide the capability to fetch enumeration values and generate items using these values. To do this, use the required AddEnum/AddEnum<T> method overload. These overloads provide various parameters.

  • addEnumeratorIntegerValues

    Typically, an editor item has the Value and Description properties. An item value is passed to the editor’s BaseEdit.EditValue property, while an item description is shown to end-users on screen. By default, when you populate an editor from an enumeration, these two values are the same. If the addEnumeratorIntegerValues parameter is set to true, item values will store numeric constants related to enumeration values, not their constant names.

  • displayTextConverter

    This parameter allows you to specify a delegate that encapsulates a method which modifies item descriptions. Note that you can do the same thing by declaring Description attributes before enumeration values as shown below.

    public enum Actions {
        Add = 2,
        Remove = 4,
        New = 8,
        Save = 16,
        [Description("Save All")]
        SaveAll = 32,
        None
    }
    
  • skipComposite

    When set to true, this parameter prevents the editor from generating items that correspond to enumeration values with composite bit-flags. Refer to the Non-exclusive members and the Flags attribute article section to learn more.

  • skipNone

    This parameter allows you to skip enumeration values that do not have manually assigned numeric values.

The figure below illustrates various DevExpress editors populated from a sample 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