Skip to main content

ImageComboBoxItemCollection.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.1.dll

NuGet Package: DevExpress.Win.Navigation

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 ImageComboBoxItem objects within the ImageComboBoxItemCollection. The ImageComboBoxEdit control bound to this collection displays the items according to settings passed as the method’s parameters:

  • addEnumeratorIntegerValues

    Each ImageComboBoxItem generated from the enumeration’s element has the Description and the inherited Value properties. 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, item 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 following code example demonstrates how to populate the ImageComboBoxEdit control’s ImageComboBoxItemCollection with items from the specified enum.

RepositoryItemImageComboBoxAddEnum

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();
            imageComboBoxEdit1.Properties.Items.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