Skip to main content

BaseListBoxControl.AddEnum(Type, Boolean) Method

Adds new items that represent elements of the specified enumeration to the control, allowing you to specify whether enumeration constant names or integer values are assigned to the item values.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.2.dll

Declaration

public void AddEnum(
    Type enumType,
    bool addEnumeratorIntegerValues
)

Parameters

Name Type Description
enumType Type

The type of the required enumeration.

addEnumeratorIntegerValues Boolean

true if enumeration underlying integer values are assigned to the item values; otherwise, false.

Remarks

The AddEnum method retrieves an array of the constants in the specified enumeration. Based on the retrieved constants, the ListBoxItem objects are created:

  • the item Value property (see ListBoxItem.Value) — is set to the corresponding enumeration constant name or constant value of the enumeration underlying integer type, depending on the addEnumeratorIntegerValues parameter;
  • the item Description property (see CheckedListBoxItem.Description and ImageListBoxItem.Description) — is set to the value that is automatically generated based on the return value of the ToString() method invoked on the corresponding enumeration constant. You can also specify a custom method that provides an item description using the BaseListBoxControl.AddEnum<TEnum> method overload with the displayTextConverter parameter.

The created items are added to the Items (see BaseCheckedListBoxControl.Items and BaseImageListBoxControl.Items) collection.

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