Skip to main content

BaseListBoxControl.AddEnum<TEnum>(Converter<TEnum, String>, Boolean) Method

Adds new items that represent elements of the specified enumeration to the control. Allows you to customize item descriptions and specify whether underlying integer values are assigned to the item values.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.2.dll

Declaration

public void AddEnum<TEnum>(
    Converter<TEnum, string> displayTextConverter,
    bool addEnumeratorIntegerValues
)

Parameters

Name Type Description
displayTextConverter Converter<TEnum, String>

A System.Converter delegate that generates item descriptions to be displayed.

addEnumeratorIntegerValues Boolean

true, if values for generated items should store numeric enumerator values; false, if these values should store constant enumerator names instead.

Type Parameters

Name Description
TEnum

The type of the required enumeration.

Remarks

This method creates ListBoxItem objects from the specified enumeration’s members and uses the objects to populate a BaseListBoxControl‘s descendant. The new items are stored in the BaseCheckedListBoxControl.Items and BaseImageListBoxControl.Items collection.

The BaseListBoxControl‘s descendant controls provide additional settings:

  • The Value property (see ListBoxItem.Value) stores an item’s constant name or integer value. This depends on the value the addEnumeratorIntegerValues parameter receives.
  • The Description property (see CheckedListBoxItem.Description and ImageListBoxItem.Description) stores an item’s description displayed to the end user. You can generate the description in a delegate and pass it to the displayTextConverter parameter.

Example

The following code example shows how to populate the ImageListBoxControl, a BaseListBoxControl descendant, with items from the specified enum.

BaseListBoxControlImageListBoxControlAddEnum

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 ImageListBoxControl with items
            // generated based on the Day enumeration constants.
            // The method parameter specifies the delegate 
            // that encapsulates the method providing
            // custom item descriptions.
            // The second parameter set to true specifies
            // item values to be set to enumeration constant
            // integer values instead of constant names.
            imageListBoxControl1.AddEnum<Day>(new Converter<Day, string>(MyConverter), true);
        }

        // 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