Intermediate 12 min read Control #4

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

  1. Drag a ComboBox onto your form.
  2. Rename it (e.g., cmbCountry).
  3. Add items using the Items collection or in code.
  4. 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.

  1. Add a ComboBox for pizza sizes (Small, Medium, Large, Extra Large).
  2. Add a ComboBox for crust type (Thin, Thick, Stuffed).
  3. Add a Button: "Order Pizza".
  4. When clicked, display the selected options.
  5. 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.