Using the ComboBox Class

Using the ComboBox Class

The ComboBox class is used to create a combo box control, a control that combines a text box control with a list box. The ComboBox class is derived from ListControl, which is the parent class of the ListBox control discussed earlier in this chapter. The combo box control is often used in cases in which it’s convenient to offer the user a selection from multiple choices in the list box portion of the control, without requiring the space used by a list box. The combo box control also offers a variety of styles that control its behavior, such as whether the list box is shown or whether the text in the text box control can be modified.

Using Combo Box Properties

Because a combo box control consists of a list box and a text box control, it comes as no surprise that many of the properties exposed by the ComboBox class are shared with either the ListBox or the TextBox class. For example, the ComboBox class maintains a collection of items stored in the list box portion of the control. Although this is technically a different class from the collection used by the ListBox class—the type is ComboBox.ObjectCollection in this case—the collection is referenced through the Items property, just as with the ListBox control. The following code contains methods that add and remove items stored in a combo box control:

private void newItem_Click(object sender, System.EventArgs e)
{
    AddComboItemForm dlg = new AddComboItemForm();
    DialogResult result = dlg.ShowDialog(this);
    if(result == DialogResult.OK)
    {
        string newItem = dlg.NewItem;
        combo.Items.Add(newItem);
    }
}
private void removeItem_Click(object sender, System.EventArgs e)
{
    int index = combo.SelectedIndex;
    if(index != -1)
    {
        combo.Items.RemoveAt(index);
    }
    else
    {
        MessageBox.Show("You must select an item to remove",
                        "Can't remove item",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Hand);
    }
}

As with the ListBox class, the ComboBox class exposes the SelectedItem and SelectedIndex properties for determining which item is selected in the combo box control, as shown here:

int index = combo.SelectedIndex;
string name = (string)combo.SelectedItem;

Unlike the ListBox class, ComboBox doesn’t support multiple selection of items in its list box.

As with a text box, the text displayed in the text box section of the combo box is retrieved using the Text property, as shown here:

string name = combo.Text;

The combo box offers a flexible appearance that’s controlled by properties and can be changed at run time. The default style for a combo box is to supply a drop-down list box with the text box control. The DropDownStyle property is used to define the style for the combo box, and it’s set to one of the values from the ComboBoxStyle enumeration, shown in Table 12-9.

The control’s style can be changed at run time because of the stateless nature of Windows Forms, as discussed earlier in this chapter, in the section “Controlling Button Appearance with Properties.” Other properties that affect the appearance of the control are as follows:

  • DropDownWidth  Specifies the width in pixels of the drop-down portion of the combo box

  • DroppedDown  Specifies a Boolean value that indicates whether the drop-down list should be shown

  • Height  Specifies the height in pixels of a simple combo box

  • MaxDropDownItems  Specifies the number of items to be shown in the drop-down portion of the combo box

The following code uses many of the property styles to change the style of a combo box at run time. This code is taken from the Controls example project included on the companion CD.

private void dropDownRadio_CheckedChanged(object sender,
                                          System.EventArgs e)
{
    combo.DropDownStyle = ComboBoxStyle.DropDown;
}

private void dropDownListRadio_CheckedChanged(object sender, 
                                              System.EventArgs e)
{
    combo.DropDownStyle = ComboBoxStyle.DropDownList;
}

private void simpleRadio_CheckedChanged(object sender,
                                        System.EventArgs e)
{
    combo.DropDownStyle = ComboBoxStyle.Simple;
    combo.Height = 80;
}

In this code, the Height property for the combo box is set when the style is changed to the simple combo box style. The default height for other combo box styles is the height of the text box control. When the style is switched to a simple combo box, the height is used to determine the height of the entire control. If you change from one of the other combo box styles to a simple style combo box and keep the default height, the items in the drop-down portion of the simple combo box won’t be visible.

Handling Combo Box Events

Most of the events raised by a combo box control are also raised by the list box and text box controls. For example, the SelectedIndexChanged and Selected­Value­Changed events are raised when a new item is selected in the list box portion of the control. The TextChanged event fires when the contents of the text box are changed.

In addition to these commonly used events, the ComboBox class exposes its own set of unique events. The most commonly handled of these events are listed here:

  • DropDown  The drop-down list has been displayed.

  • SelectionChangeCommitted  The user has committed changes to an item from the list box portion of the control.

The following code uses the SelectionChangeCommitted event to determine when the user has selected an item in the combo box:

private void combo_SelectionChangeCommitted(object sender,
                                              System.EventArgs e)
{
    string message = string.Format("The new selection is {0}",
                                    combo.SelectedItem.ToString());
    MessageBox.Show(message);
}

In this example, the SelectedItem property is used to retrieve a reference to the selected item rather than simply calling combo.Text. This is because the OnCommitted event is raised before the text box control has been updated, and the Text property will return the old value of the control.



Part III: Programming Windows Forms