windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-dot-addenum-1.md
Adds new items that represent elements of the specified enumeration to the control.
Namespace : DevExpress.XtraEditors
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
public void AddEnum<TEnum>()
Public Sub AddEnum(Of TEnum)
| Name |
|---|
| TEnum |
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 created items are added to the Items (see BaseCheckedListBoxControl.Items and BaseImageListBoxControl.Items) collection.
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.
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();
}
}
}
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 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(GetType(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(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)
Return Convert.ChangeType(d, dayUnderlyingType).ToString() + ". " + d.ToString()
End Function
End Class
See Also
BaseCheckedListBoxControl.Items