windowsforms-9466-controls-and-libraries-editors-and-simple-controls-examples-how-to-show-values-of-enumeration-in-checkedcomboboxedit.md
The following example edits bit fields (a set of flags) in a CheckedComboBoxEdit control. This control displays values from a custom MyColors enum. The enum includes five simple flags, ranging from None to Yellow , and one combined flag, Green.
The CheckedComboBoxEdit control does not support combined flags. You need to manually remove items that correspond to these flags from the RepositoryItemCheckedComboBoxEdit.Items collection. The removeCombinedFlags method performs this task.
The RepositoryItemCheckedComboBoxEdit.SetFlags method populates the RepositoryItemCheckedComboBoxEdit.Items collection with items for all available flags, except for the flag with a zero value. The removeCombinedFlags method then eliminates items that represent combined flags.
Finally, specify the initial value for the editor using the CheckedComboBoxEdit.SetEditValue method.
Note that the Green flag does not appear in the dropdown list, although you can assign this flag to the control’s value.
using DevExpress.XtraEditors.Repository;
[Flags]
enum MyColors {
None = 0x00,
Black = 0x01,
White = 0x02,
Blue = 0x04,
Yellow = 0x08,
Green = Blue | Yellow
}
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
// Populate the Items collection with all available flags.
checkedComboBoxEdit1.Properties.SetFlags(typeof(MyColors));
// Remove items that correspond to compound flags.
removeCombinedFlags(checkedComboBoxEdit1.Properties);
// Set an initial value.
checkedComboBoxEdit1.SetEditValue(MyColors.Black | MyColors.Green);
}
// Traverse through items and remove those that correspond to bitwise combinations of simple flags.
private void removeCombinedFlags(RepositoryItemCheckedComboBoxEdit ri) {
for (int i = ri.Items.Count - 1; i > 0; i--) {
Enum val1 = ri.Items[i].Value as Enum;
for (int j = i - 1; j >= 0; j--) {
Enum val2 = ri.Items[j].Value as Enum;
if (val1.HasFlag(val2)) {
ri.Items.RemoveAt(i);
break;
}
}
}
}
}
Imports DevExpress.XtraEditors.Repository
<Flags> _
Enum MyColors
None = &H0
Black = &H1
White = &H2
Blue = &H4
Yellow = &H8
Green = Blue Or Yellow
End Enum
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Populate the Items collection with all available flags.
CheckedComboBoxEdit1.Properties.SetFlags(GetType(MyColors))
' Remove items that correspond to compound flags.
removeCombinedFlags(CheckedComboBoxEdit1.Properties)
' Set an initial value.
CheckedComboBoxEdit1.SetEditValue(MyColors.Black Or MyColors.Green)
End Sub
' Traverse through items and remove those that correspond to bitwise combinations of simple flags.
Private Sub removeCombinedFlags(ri As RepositoryItemCheckedComboBoxEdit)
For i As Integer = ri.Items.Count - 1 To 1 Step -1
Dim val1 As [Enum] = TryCast(ri.Items(i).Value, [Enum])
For j As Integer = i - 1 To 0 Step -1
Dim val2 As [Enum] = TryCast(ri.Items(j).Value, [Enum])
If val1.HasFlag(val2) Then
ri.Items.RemoveAt(i)
Exit For
End If
Next
Next
End Sub
End Class