Skip to main content

ListBoxItemCollection.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.Controls

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 fetches elements from the specified enumeration and stores them as ListBoxItem objects within the ListBoxItemCollection. The ListBoxControl bound to this collection displays the items according to settings passed as the method’s parameters:

  • addEnumeratorIntegerValues

    Each ListBoxItem generated from the enumeration’s element has the Value property. Some descendants also have the Description property. When you populate an editor from an enumeration, the Description and Value properties are both assigned the enumeration text values. If the addEnumeratorIntegerValues parameter is set to true, the items’ Value properties store integer constants related to enumeration elements, and Description properties - the enumeration elements’ text values.

  • displayTextConverter This parameter allows you to specify a delegate that encapsulates a method which modifies item descriptions the users see. 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
    }
    

Example

The example below demonstrates how to populate the ListBoxControl‘s ListBoxItemCollection with items from the specified enum.

ListBoxItemCollectionAddEnum

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();
            listBoxControl1.Items.AddEnum<Day>(new Converter<Day, string>(MyConverter), false);
        }

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