ComboBox Control
The ComboBox combines a text box with a drop-down list. Users can either select from the list or type their own value.
What is a ComboBox?
A ComboBox has two parts: a text box and a drop-down list. Users can type directly in the text box or click the arrow to select from a list. It's great for saving screen space.
Common uses: Country selection, month picker, category dropdown, department selection.
How to Use
- Drag a ComboBox onto your form.
- Rename it (e.g.,
cmbCountry). - Add items using the Items collection or in code.
- Set DropDownStyle to control user input.
Code Example
// Add items in code
cmbCountry.Items.AddRange(new object[]
{
"USA", "Canada", "UK", "Germany", "France"
});
cmbCountry.SelectedIndex = 0;
// Get selected value
private void btnSubmit_Click(object sender, EventArgs e)
{
string selected = cmbCountry.SelectedItem?.ToString() ?? "Nothing selected";
MessageBox.Show($"Selected: {selected}");
}
// DropDownStyle options:
// - DropDown: User can type or select (default)
// - DropDownList: User can only select from list
// - Simple: Always shows the list
cmbCountry.DropDownStyle = ComboBoxStyle.DropDownList;
Exercise
Task: Create a simple pizza order form.
- Add a ComboBox for pizza sizes (Small, Medium, Large, Extra Large).
- Add a ComboBox for crust type (Thin, Thick, Stuffed).
- Add a Button: "Order Pizza".
- When clicked, display the selected options.
- Pre-select the most popular options.
Key Takeaway
ComboBoxes save space and are great for predefined options. Use DropDownList
when you want to restrict user input.