|
1 | | -# how-to-disable-items-winforms-combobox-dropdown |
2 | | -This example demonstrates how to disable items from a winforms combobox dropdown. For more details please refer [how to disable items from winforms combobox dropdown](https://www.syncfusion.com/kb/11254/how-to-disable-some-items-winforms-combobox-dropdown). |
| 1 | +# How to Disable Items in WinForms ComboBox Dropdown |
| 2 | +## Overview |
| 3 | +This example demonstrates how to disable specific items in a WinForms ComboBox dropdown using Syncfusion's SfComboBox control. This is useful when you want to prevent users from selecting certain items based on business logic or application state. |
3 | 4 |
|
4 | | -## Changing forecolor for disabled items in dropdown |
5 | | -You can change the forecolor for disabled items by handling SfComboBox.DropDownListView.DrawItem event to show the items is disabled. |
| 5 | +## Changing ForeColor for Disabled Items |
| 6 | +You can visually indicate disabled items by customizing their appearance using the DrawItem event of DropDownListView. |
| 7 | +### C# Example |
| 8 | +```C# |
| 9 | +sfComboBox.DropDownListView.DrawItem += DropDownListView_DrawItem; |
6 | 10 |
|
7 | | -# C# |
| 11 | +private void DropDownListView_DrawItem(object sender, Syncfusion.WinForms.ListView.Events.DrawItemEventArgs e) |
| 12 | +{ |
| 13 | + bool isItemEnable = (sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && |
| 14 | + sfComboBox.AllowSelectAll && |
| 15 | + e.ItemIndex == 0) |
| 16 | + ? true |
| 17 | + : (e.ItemData as Details).IsEnabled; |
8 | 18 |
|
9 | | - sfComboBox.DropDownListView.DrawItem += DropDownListView_DrawItem; |
10 | | - |
11 | | - private void DropDownListView_DrawItem(object sender, Syncfusion.WinForms.ListView.Events.DrawItemEventArgs e) |
12 | | - { |
13 | | - bool isItemEnable = (this.sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && this.sfComboBox.AllowSelectAll && e.ItemIndex == 0) ? true : (e.ItemData as Details).IsEnabled; |
14 | | - if (!isItemEnable) |
| 19 | + if (!isItemEnable) |
| 20 | + { |
| 21 | + e.Style.BackColor = Color.LightGray; |
| 22 | + e.Style.ForeColor = Color.Gray; |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +## Handling Selection for Disabled Items |
| 28 | +Selection logic differs based on whether the ComboBox is in multi-selection or single-selection mode. |
| 29 | +### Multi-Selection Mode: |
| 30 | +```C# |
| 31 | +private void DropDownListView_SelectionChanged(object sender, ItemSelectionChangedEventArgs e) |
| 32 | +{ |
| 33 | + if (e.AddedItems.Count == sfComboBox.DropDownListView.View.Items.Count) |
| 34 | + { |
| 35 | + for (int i = 0; i < sfComboBox.DropDownListView.CheckedItems.Count; i++) |
15 | 36 | { |
16 | | - e.Style.BackColor = Color.LightGray; |
17 | | - e.Style.ForeColor = Color.Gray; |
| 37 | + if (!(sfComboBox.DropDownListView.CheckedItems[i] as Details).IsEnabled) |
| 38 | + sfComboBox.DropDownListView.CheckedItems.RemoveAt(i); |
18 | 39 | } |
19 | 40 | } |
| 41 | +} |
20 | 42 |
|
21 | | -## Handling selection for disabled items in dropdown |
22 | | -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. |
23 | | - |
24 | | -# C# |
| 43 | +private void DropDownListView_ItemChecking(object sender, ItemCheckingEventArgs e) |
| 44 | +{ |
| 45 | + bool isItemEnable = (sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && |
| 46 | + sfComboBox.AllowSelectAll && |
| 47 | + e.ItemIndex == 0) |
| 48 | + ? true |
| 49 | + : (e.ItemData as Details).IsEnabled; |
25 | 50 |
|
26 | | - private void DropDownListView_SelectionChanged(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangedEventArgs e) |
27 | | - { |
28 | | - if (e.AddedItems.Count == this.sfComboBox.DropDownListView.View.Items.Count)S |
29 | | - { |
30 | | - for (int i = 0; i < this.sfComboBox.DropDownListView.CheckedItems.Count; i++) |
31 | | - { |
32 | | - if ((this.sfComboBox.DropDownListView.CheckedItems[i] as Details).IsEnabled == false) |
33 | | - this.sfComboBox.DropDownListView.CheckedItems.RemoveAt(i); |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - private void DropDownListView_ItemChecking(object sender, Syncfusion.WinForms.ListView.Events.ItemCheckingEventArgs e) |
39 | | - { |
40 | | - bool isItemEnable = (this.sfComboBox.ComboBoxMode == ComboBoxMode.MultiSelection && this.sfComboBox.AllowSelectAll && e.ItemIndex == 0) ? true : (e.ItemData as Details).IsEnabled; |
41 | | - if (!isItemEnable) |
| 51 | + if (!isItemEnable) |
42 | 52 | e.Cancel = true; |
43 | | - } |
44 | | - |
45 | | - private void DropDownListView_SelectionChanging(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangingEventArgs e) |
| 53 | +} |
| 54 | +``` |
| 55 | +### Single-Selection Mode |
| 56 | +```C# |
| 57 | +private void DropDownListView_SelectionChanging(object sender, ItemSelectionChangingEventArgs e) |
| 58 | +{ |
| 59 | + if (e.AddedItems.Count > 0 && |
| 60 | + !(e.AddedItems[0] as Details).IsEnabled && |
| 61 | + e.AddedItems.Count != sfComboBox.DropDownListView.View.Items.Count) |
46 | 62 | { |
47 | | - if (e.AddedItems.Count > 0 && !(e.AddedItems[0] as Details).IsEnabled && e.AddedItems.Count != this.sfComboBox.DropDownListView.View.Items.Count) |
48 | 63 | e.Cancel = true; |
49 | | - } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | +## Reference |
| 68 | +For detailed guidance and step-by-step instructions, refer to the official Syncfusion Knowledge Base article: https://www.syncfusion.com/kb/11254/how-to-disable-some-items-winforms-combobox-dropdown |
50 | 69 |
|
| 70 | +## Screenshot |
51 | 71 |  |
52 | | - |
|
0 commit comments