Skip to main content
A newer version of this page is available. .

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.v19.2.dll

Declaration

public void SetFlags(
    Type enumType
)

Parameters

Name Type Description
enumType 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 CheckedComboBoxEdit control supports only simple flags. Simple flags are those that when combined with the bitwise AND operation, produce a zero value. The RepositoryItemCheckedComboBoxEdit.SetFlags 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 RepositoryItemCheckedComboBoxEdit.SetFlags 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 shows how to edit bit fields (a set of flags) in a CheckedComboBoxEdit control.

The control displays values of a custom MyColors enum, which has five simple flags (None through Yellow) and one combined flag (Green). Combined flags are not supported by the CheckedComboBoxEdit control. Items that correspond to these flags must be manually removed from the RepositoryItemCheckedComboBoxEdit.Items collection (see the removeCombinedFlags method which performs this operation).

The RepositoryItemCheckedComboBoxEdit.SetFlags method populates the RepositoryItemCheckedComboBoxEdit.Items collection with items corresponding to all available flags (except for a flag with a zero value). Then, the removeCombinedFlags method is called to remove items corresponding to combined flags. Finally, the editor’s initial value is specified using the CheckedComboBoxEdit.SetEditValue method.

The following image shows the result:

CheckedComboBoxEdit_ex_Flags

Note that the Green flag is not present in the dropdown list, although you can set 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;
                }
            }
        }
    }
}
See Also