|
1 | 1 | # how-to-disable-some-items-winforms-combobox-dropdown |
2 | 2 | This example demonstrates how to disable some items from a winforms combobox dropdown. For more details please refer [how to disable some items from winforms combobox dropdown](https://www.syncfusion.com/kb/11254/how-to-disable-some-items-winforms-combobox-dropdown). |
| 3 | + |
| 4 | +## Changing forecolor for disabled items in dropdown |
| 5 | +You can change the forecolor for disabled items by handling [SfComboBox.DropDownListView.DrawItem]((https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.ListView.SfListView.html)) event to show the items is disabled. |
| 6 | + |
| 7 | +{% tabs %} |
| 8 | + |
| 9 | +{% highlight C# %} |
| 10 | + |
| 11 | +sfComboBox.DropDownListView.DrawItem += DropDownListView_DrawItem; |
| 12 | + |
| 13 | +private void DropDownListView_DrawItem(object sender, Syncfusion.WinForms.ListView.Events.DrawItemEventArgs e) |
| 14 | +{ |
| 15 | + bool isItemEnable = (this.sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && this.sfComboBox.AllowSelectAll && e.ItemIndex == 0) ? true : (e.ItemData as Details).IsEnabled; |
| 16 | + if (!isItemEnable) |
| 17 | + { |
| 18 | + e.Style.BackColor = Color.LightGray; |
| 19 | + e.Style.ForeColor = Color.Gray; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +{% endhighlight %} |
| 24 | + |
| 25 | +## Handling selection for disabled items in dropdown |
| 26 | +Selection of disabled items in multi selection mode handled using [SfComboBox.DropDownListView.SelectionChanged](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.ListView.SfListView.html) and [SfComboBox.DropDownListView.ItemChecking event](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.ListView.SfListView.html). In single selection mode, selection is handled using [SfComboBox.DropDownListView.SelectionChanging event](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.ListView.SfListView.html). |
| 27 | + |
| 28 | +{% tabs %} |
| 29 | + |
| 30 | +{% highlight C# %} |
| 31 | + |
| 32 | +sfComboBox.DropDownListView.SelectionChanged += DropDownListView_SelectionChanged; |
| 33 | +sfComboBox.DropDownListView.ItemChecking += DropDownListView_ItemChecking; |
| 34 | +sfComboBox.DropDownListView.SelectionChanging += DropDownListView_SelectionChanging; |
| 35 | + |
| 36 | +private void DropDownListView_SelectionChanged(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangedEventArgs e) |
| 37 | +{ |
| 38 | + if (e.AddedItems.Count == this.sfComboBox.DropDownListView.View.Items.Count) |
| 39 | + { |
| 40 | + for (int i = 0; i < this.sfComboBox.DropDownListView.CheckedItems.Count; i++) |
| 41 | + { |
| 42 | + if ((this.sfComboBox.DropDownListView.CheckedItems[i] as Details).IsEnabled == false) |
| 43 | + this.sfComboBox.DropDownListView.CheckedItems.RemoveAt(i); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +private void DropDownListView_ItemChecking(object sender, Syncfusion.WinForms.ListView.Events.ItemCheckingEventArgs e) |
| 49 | +{ |
| 50 | + bool isItemEnable = (this.sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && this.sfComboBox.AllowSelectAll && e.ItemIndex == 0) ? true : (e.ItemData as Details).IsEnabled; |
| 51 | + if (!isItemEnable) |
| 52 | + e.Cancel = true; |
| 53 | +} |
| 54 | + |
| 55 | +private void DropDownListView_SelectionChanging(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangingEventArgs e) |
| 56 | +{ |
| 57 | + if (e.AddedItems.Count > 0 && !(e.AddedItems[0] as Details).IsEnabled && e.AddedItems.Count != this.sfComboBox.DropDownListView.View.Items.Count) |
| 58 | + e.Cancel = true; |
| 59 | +} |
| 60 | + |
| 61 | +{% endhighlight %} |
| 62 | + |
| 63 | + |
| 64 | + |
0 commit comments