How to use CheckedComboBoxEdit with SearchControl in C#

By FoxLearn 8/30/2024 2:43:16 AM   233
Using CheckedComboBoxEdit with SearchControl in a C# application typically involves working with DevExpress components in a WinForms.

Here’s a step-by-step guide on how you can integrate these two controls:

First, You need to create a CustomRepositoryItemSearchControl class that inherits from RepositoryItemSearchControl

[UserRepositoryItem("RegisterCustomRepositoryItemSearchControl")]
public class CustomRepositoryItemSearchControl : RepositoryItemSearchControl
{
    public const string CustomEditName = "CustomSearchControl";

    static CustomRepositoryItemSearchControl()
    {
        RegisterCustomRepositoryItemSearchControl();
    }

    public static void RegisterCustomRepositoryItemSearchControl()
    {
        Image img = null;
        EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(CustomSearchControl), typeof(CustomRepositoryItemSearchControl), typeof(SearchControlViewInfo), new ButtonEditPainter(), true, img));
    }

    public override string EditorTypeName => CustomEditName;

    public override void Assign(RepositoryItem item)
    {
        BeginUpdate();
        base.Assign(item);
        CustomRepositoryItemSearchControl source = item as CustomRepositoryItemSearchControl;
        if (source == null)
            return;
        EndUpdate();
    }

    protected override SearchControlQueryParamsEventArgs OnQueryCriteriaParams()
    {
        SearchControlQueryParamsEventArgs args = base.OnQueryCriteriaParams();
        if (Client.GetType().Name == "PopupCheckedListBoxControl" && !string.IsNullOrEmpty(args.SearchText))
            args.SearchText += " \"(Select All)\"";
        return args;
    }
}

Next, Create a CustomSearchControl class that inherits from SearchControl

[ToolboxItem(true)]
public class CustomSearchControl : SearchControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new CustomRepositoryItemSearchControl Properties => base.Properties as CustomRepositoryItemSearchControl;

    public override string EditorTypeName => CustomRepositoryItemSearchControl.CustomEditName;
}

Finally, Create a CustomCheckedComboBoxEdit class that inherits from CheckedComboBoxEdit control

[ToolboxItem(true)]
public class CustomCheckedComboBoxEdit : CheckedComboBoxEdit
{
    static CustomCheckedComboBoxEdit()
    {
        CustomRepositoryItemSearchControl.RegisterCustomRepositoryItemSearchControl();
    }

    protected override PopupContainerControl CreatePopupCheckListControl()
    {
        var popupContainerControl = base.CreatePopupCheckListControl();
        var checkedListBoxControl = popupContainerControl.Controls.OfType<CheckedListBoxControl>().First();

        var customSearchControl = new CustomSearchControl() { Client = checkedListBoxControl };
        customSearchControl.Dock = DockStyle.Top;
        customSearchControl.Properties.FindDelay = 100;

        popupContainerControl.Controls.Add(customSearchControl);
        return popupContainerControl;
    }
}

You need to overridden the CreatePopupCheckListControl() method, to add a CustomSearchControl and bind it to the internal CheckedListBoxControl.

If you wish to include the Select All item from filtering, you can create a custom SearchControl, then overide the RepositoryItemSearchControl.OnQueryCriteriaParams method, then add the "(Select All)" string to the current search string by setting the SearchControlQueryParamsEventArgs.SearchText property. 

The search control works as expected, It automatically adds the (Select All) row in the list, which is also participating in the filtering process.

Rebuild your project, then you can see your CustomCheckedComboBoxEdit in the Visual Studio toolbox.

By following these steps, you should be able to integrate CheckedComboBoxEdit and SearchControl effectively in your DevExpress-based C# application.