Skip to main content

CheckedComboBoxEdit.SetEditValue(Object) Method

Assigns the value to the CheckedComboBoxEdit.EditValue property, and checks editor items that have related values.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.2.dll

Declaration

public virtual void SetEditValue(
    object value
)

Parameters

Name Type Description
value Object

An object that identifies checked items in the editor’s dropdown window.

Remarks

The edit value identifies checked items in the editor’s dropdown window. If the SetEditValue method parameter is a value that no editor item has, the editor resets its EditValue property and deselects all its items.

checkedComboBoxEdit1.Properties.Items.Add(1, "One");
checkedComboBoxEdit1.Properties.Items.Add(2, "Two");
checkedComboBoxEdit1.Properties.Items.Add(3, "Three");

//Valid code, selects item "Two"
checkedComboBoxEdit1.SetEditValue(2);
//or
checkedComboBoxEdit1.SetEditValue("2");

//Valid code, selects items "One" and "Three"
checkedComboBoxEdit1.SetEditValue("1, 3");

//Party valid code, selects items "One" and "Three", but ignores the "100" value
checkedComboBoxEdit1.SetEditValue("1, 3, 100");

//Invalid code, clears editor selection
checkedComboBoxEdit1.SetEditValue("Two");
checkedComboBoxEdit1.SetEditValue(4)

Depending on the RepositoryItemCheckedComboBoxEdit.EditValueType property value, you need to pass either strings\objects or Lists to the SetEditValue method.

checkedComboBoxEdit1.Properties.Items.Add(true, "Yes");
checkedComboBoxEdit1.Properties.Items.Add(false, "No");

//Edit value is a string
checkedComboBoxEdit1.Properties.EditValueType = EditValueTypeCollection.CSV;
checkedComboBoxEdit1.SetEditValue(true);
//or
checkedComboBoxEdit1.SetEditValue("True");

//Edit value is a list
checkedComboBoxEdit1.Properties.EditValueType = EditValueTypeCollection.List;
checkedComboBoxEdit1.SetEditValue(new List<bool>() { true });

If the editor stores bit fields, the edit value must be of the corresponding enumeration type and must represent a set of flags to be checked in the dropdown (see “Example 2” below).

Example 1

The code below populates the RepositoryItemCheckedComboBoxEdit.Items collection with five items. Each item stores a string value.

You should use the CheckedComboBoxEdit.SetEditValue method to select the “Circle” and “Ellipse” items. Then, to prevent users from deselecting it, disables the “Circle” item.

CheckedComboBoxEdit_ex

This code modifies the RepositoryItemCheckedComboBoxEdit.SeparatorChar property. This changes the edit value separator character from the default comma (“,”) to a semicolon (“;”). Ensure that the SetEditValue method uses this same separator character in its parameter.

// Add check items to the control's dropdown.
string[] itemValues = new string[] { 
    "Circle", "Rectangle", "Ellipse", 
    "Triangle", "Square" };
foreach (string value in itemValues)
    checkedComboBoxEdit1.Properties.Items.Add(value, CheckState.Unchecked, true);
// Specify the separator character.
checkedComboBoxEdit1.Properties.SeparatorChar = ';';
// Set the edit value.
checkedComboBoxEdit1.SetEditValue("Circle; Ellipse");
// Disable the Circle item.
checkedComboBoxEdit1.Properties.Items["Circle"].Enabled = false;

Example 2

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.

CheckedComboBoxEdit_ex_Flags

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;
                }
            }
        }
    }
}
See Also