Articles in this section
Category / Section

How to disable some items WinForms ComboBox dropdown?

1 min read

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 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied