|
1 | | -# How-to-select-the-item-through-index-in-WF-SfComboBox |
2 | | -This respository contains the sample that how to select the item through index in WF SfComboBox control. |
| 1 | +# How to Select an Item by Index in WinForms SfComboBox |
| 2 | +This example demonstrates how to select an item programmatically by index in the Syncfusion WinForms SfComboBox control. The SfComboBox supports both index-based selection and object-based selection, making it flexible for scenarios like applying default selections, loading saved preferences, or responding to application logic. |
| 3 | + |
| 4 | +## Why This Is Useful |
| 5 | +- **Dynamic Selection**: Automatically select items based on conditions. |
| 6 | +- **Improved UX**: Pre-load user preferences or defaults. |
| 7 | +- **Flexibility**: Choose items by index or by object reference. |
| 8 | + |
| 9 | +## Key Properties |
| 10 | +- **SelectedIndex**: Sets or gets the zero-based index of the selected item. |
| 11 | +- **SelectedItem**: Sets or gets the actual object from the data source. |
| 12 | + |
| 13 | +## Example Code |
| 14 | +```C# |
| 15 | + |
| 16 | +public Form1() |
| 17 | +{ |
| 18 | + InitializeComponent(); |
| 19 | + |
| 20 | + // Sample data |
| 21 | + List<State> list = GetData(); |
| 22 | + |
| 23 | + // Create and configure SfComboBox |
| 24 | + SfComboBox sfComboBox1 = new SfComboBox |
| 25 | + { |
| 26 | + Size = new Size(150, 28), |
| 27 | + Location = new Point(138, 56), |
| 28 | + DataSource = list, |
| 29 | + DisplayMember = "DispValue", |
| 30 | + ValueMember = "SfValue", |
| 31 | + ComboBoxMode = Syncfusion.WinForms.ListView.Enums.ComboBoxMode.SingleSelection, |
| 32 | + AutoCompleteMode = AutoCompleteMode.SuggestAppend, |
| 33 | + AutoCompleteSuggestMode = Syncfusion.WinForms.ListView.Enums.AutoCompleteSuggestMode.Contains, |
| 34 | + MaxDropDownItems = 10 |
| 35 | + }; |
| 36 | + |
| 37 | + // Select the item based on index (second item) |
| 38 | + sfComboBox1.SelectedIndex = 1; |
| 39 | + |
| 40 | + // Select the item based on object |
| 41 | + sfComboBox1.SelectedItem = list[1]; |
| 42 | + |
| 43 | + this.Controls.Add(sfComboBox1); |
| 44 | +} |
| 45 | + |
| 46 | +private List<State> GetData() |
| 47 | +{ |
| 48 | + return new List<State> |
| 49 | + { |
| 50 | + new State { DispValue = "California", SfValue = 1 }, |
| 51 | + new State { DispValue = "Texas", SfValue = 2 }, |
| 52 | + new State { DispValue = "Florida", SfValue = 3 } |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +public class State |
| 57 | +{ |
| 58 | + public string DispValue { get; set; } |
| 59 | + public int SfValue { get; set; } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Notes |
| 64 | +- Use SelectedIndex for index-based selection (zero-based). |
| 65 | +- Use SelectedItem for object-based selection. |
| 66 | +- Ensure the data source is bound before setting these properties. |
| 67 | + |
| 68 | +## Reference |
| 69 | +For more details, refer to the official Syncfusion Knowledge Base: https://support.syncfusion.com/kb/article/10653/how-to-select-the-item-through-index-in-winforms-sfcombobox |
| 70 | + |
| 71 | +## Output |
| 72 | + |
| 73 | + |
0 commit comments