Search code examples
vb.netdevexpress

Get Checked Items In A DevExpress CheckedComboBoxEdit


I am using the DevExpress 9.3 CheckedComboBoxEdit, and I need to get the collection of all checked items. It seems like this should be a simple task, but the closest thing I have found to a solution is something that says I can use:

CheckedComboBoxEdit.Properties.GetItems.GetCheckedValues()

Unfortunately, there is no GetCheckedValues method here. I have found the following:

CheckedComboBoxEdit.Properties.GetCheckedItems()

which returns an object, but I cannot find any reference on what I should cast the object as. I have also tried to iterate through the items, and check each one to see if it is checked, following the suggestion from here, but Items returns a collection of Strings, not CheckedListBoxItem, so I cannot test if they are checked.

What I want is a String collection of checked items; right now, I am okay to receive them as any type of collection, or even create the collection myself. I know there must be some very simple thing that I am overlooking, but I can't seem to find it.

SOLUTION

This is the solution that I came up with. I would prefer something more elegant; it seems like there should be a way to get the checked items, since this is what the control is for. Nevertheless, this seems to work:

Private Function GetChecked() As List(Of String)
    Dim checked As New List(Of String)
    Dim checkedString As String = CType(SitePickerControl.Properties.GetCheckedItems(), String)
    If (checkedString.Length > 0) Then
        checked.AddRange(checkedString.Split(New Char() {","c}))
    End If
    Return checked
End Function

If anyone can give me a proper solution, I would love to see it.


Solution

  • This is what I use:

    var ids = (from CheckedListBoxItem item in checkedComboBoxEdit.Properties.Items
               where item.CheckState == CheckState.Checked
               select (int)item.Value).ToArray();
    

    You can also make an extension method on CheckedListBoxItem which will return only checked items values.

    (It's C#, not VB, but the concept is the same.)