RepositoryItemCheckedComboBoxEdit.SetFlags(Type) Method
Creates items in the dropdown to represent elements of the specified set of flags.
Namespace: DevExpress.XtraEditors.Repository
Assembly: DevExpress.XtraEditors.v24.2.dll
NuGet Package: DevExpress.Win.Navigation
#Declaration
#Parameters
Name | Type | Description |
---|---|---|
enum |
Type | A Type object that specifies the enumeration type representing a bit field (set of flags). |
#Remarks
The CheckedComboBoxEdit control allows a bit field (a set of flags) to be displayed and edited. A bit field is an enumeration declared with the FlagsAttribute attribute. To initialize the control’s RepositoryItemCheckedComboBoxEdit.Items collection with flags, use the RepositoryItemCheckedComboBoxEdit.SetFlags
method.
This method creates items for all available flags in the specified enum, except for the flag that has a zero value.
After adding flags, you can delete individual items from the RepositoryItemCheckedComboBoxEdit.Items collection, if required. However, do not add custom items when the control is bound to a bit field. When the editor displays a set of flags, its edit value is of the enumeration type, and it represents a combination of checked flags.
Note
The CheckedRepository
method creates items for all flags, including compound flags (bitwise combinations of simple flags).
However, the editor will not function correctly when items corresponding to compound flags are present in the Items collection. These items must be manually removed from the Items collection once the Repository
method has been called. See an example below.
To set the editor’s value when the control is used for standalone editing, use the CheckedComboBoxEdit.SetEditValue method. Do not set the editor’s EditValue directly.
#Example
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;
}
}
}
}
}