We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to disable some items WinForms ComboBox dropdown?

Platform: WinForms |
Control: SfComboBox

This article describes how to disable dropdown items in WinForms SfComboBox. This requirement can be achieved by handling SfComboBox.DropDownListView.DrawItem, SfComboBox.DropDownListView.SelectionChanged, SfComboBox.DropDownListView.SelectionChanging and SfComboBox.DropDownListView.ItemChecking events.

Changing forecolor for disabled items in dropdown

You can change the forecolor for disabled items by handling SfComboBox.DropDonnListView.DrawItem event to show the items is disabled.

sfComboBox.DropDownListView.DrawItem += DropDownListView_DrawItem;
 
private void DropDownListView_DrawItem(object sender, Syncfusion.WinForms.ListView.Events.DrawItemEventArgs e)
{
    bool isItemEnable = (this.sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && this.sfComboBox.AllowSelectAll && e.ItemIndex == 0) ? true : (e.ItemData as Details).IsEnabled;
    if (!isItemEnable)
    {
        e.Style.BackColor = Color.LightGray;
        e.Style.ForeColor = Color.Gray;
    }
}

Handling selection for disabled items in dropdown

Selection of disabled items in multi selection mode handled using SfComboBox.DropDownListView.SelectionChanged and SfComboBox.DropDownListView.ItemChecking event. In single selection mode, selection is handled using SfComboBox.DropDownListView.SelectionChanging event.

sfComboBox.DropDownListView.SelectionChanged += DropDownListView_SelectionChanged;
sfComboBox.DropDownListView.ItemChecking += DropDownListView_ItemChecking;
sfComboBox.DropDownListView.SelectionChanging += DropDownListView_SelectionChanging;
 
private void DropDownListView_SelectionChanged(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangedEventArgs e)
{
    if (e.AddedItems.Count == this.sfComboBox.DropDownListView.View.Items.Count)
    {
        for (int i = 0; i < this.sfComboBox.DropDownListView.CheckedItems.Count; i++)
        {
            if ((this.sfComboBox.DropDownListView.CheckedItems[i] as Details).IsEnabled == false)
                this.sfComboBox.DropDownListView.CheckedItems.RemoveAt(i);
        }
    }
}
 
private void DropDownListView_ItemChecking(object sender, Syncfusion.WinForms.ListView.Events.ItemCheckingEventArgs e)
{
    bool isItemEnable = (this.sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && this.sfComboBox.AllowSelectAll && e.ItemIndex == 0) ? true : (e.ItemData as Details).IsEnabled;
    if (!isItemEnable)
        e.Cancel = true;
}
 
private void DropDownListView_SelectionChanging(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangingEventArgs e)
{
    if (e.AddedItems.Count > 0 && !(e.AddedItems[0] as Details).IsEnabled && e.AddedItems.Count != this.sfComboBox.DropDownListView.View.Items.Count)
        e.Cancel = true;
} 

The output for the above code is shown below:

Screenshot for above code.

View sample in GitHub 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile