Back to Devexpress

RepositoryItemCheckedComboBoxEdit.AddEnum<TEnum>() Method

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemcheckedcomboboxedit-dot-addenum-1.md

latest6.4 KB
Original Source

RepositoryItemCheckedComboBoxEdit.AddEnum<TEnum>() Method

Adds new items that represent elements of the specified enumeration to the editor drop-down.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public void AddEnum<TEnum>()
vb
Public Sub AddEnum(Of TEnum)

Type Parameters

Name
TEnum

Remarks

The AddEnum<TEnum> 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. Instead of the enumeration constant name, the Value property can also be set to the constant value of the enumeration underlying integer type. For this purpose, call the RepositoryItemCheckedComboBoxEdit.AddEnum method overload with the addEnumeratorIntegerValues parameter set to true ;
  • the item Description property (see CheckedListBoxItem.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 AddEnum<TEnum> method overload with the displayTextConverter parameter.

The created items are added to the RepositoryItemCheckedComboBoxEdit.Items collection.

When a specific item is selected in the CheckedComboBoxEdit control, the CheckedComboBoxEdit.EditValue property is set to the corresponding enumeration constant name.

Example

The following example demonstrates how to populate the CheckedComboBoxEdit control with items using the RepositoryItemCheckedComboBoxEdit.AddEnum<TEnum> method. The image below shows the result of executing the code in the example.

csharp
using System;
using System.ComponentModel;

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 CheckedComboBoxEdit with items
            // generated based on the Day enumeration constants.
            // The method parameter specifies the delagate 
            // that incapsulates the method providing
            // custom item descriptions.
            checkedComboBoxEdit1.Properties.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);
            string result = Convert.ChangeType(d, dayUnderlyingType).ToString() + ". " + d.ToString();
            if (d == Day.Sat || d == Day.Sun) result += " (weekend)";
            return result;
        }
    }
}
vb
Imports System.ComponentModel

    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
    End Enum
    Public Partial Class Form1
        Inherits DevExpress.XtraEditors.XtraForm
        Public Sub New()
            InitializeComponent()
            ' Populates the CheckedComboBoxEdit with items
            ' generated based on the Day enumeration constants.
            ' The method parameter specifies the delagate 
            ' that incapsulates the method providing
            ' custom item descriptions.
            checkedComboBoxEdit1.Properties.AddEnum(Of Day)(New Converter(Of Day, String)(AddressOf MyConverter))
        End Sub
        ' Returns a custom string based on the specified day.
        Public Shared Function MyConverter(d As Day) As String
            Dim dayType As Type = d.GetType
            Dim dayUnderlyingType As Type = [Enum].GetUnderlyingType(dayType)
            Dim result As String = Convert.ChangeType(d, dayUnderlyingType).ToString() + ". " + d.ToString()
            If d = Day.Sat OrElse d = Day.Sun Then
                result += " (weekend)"
            End If
            Return result
        End Function
    End Class

See Also

Items

Description

Value

RepositoryItemCheckedComboBoxEdit Class

RepositoryItemCheckedComboBoxEdit Members

DevExpress.XtraEditors.Repository Namespace