Multiple-Item Controls: Working with ListBox and ComboBox Controls

In this lesson, you will learn how to add items to and remove items from lists. You will also learn how to perform an action when a user selects an item in a list.

When you want to give users a list of choices, you can display the list of items in a ListBox control or a ComboBox control.

A ListBox control lets you display several items at the same time, enabling users to scroll through a longer list. When a user selects an item, it becomes highlighted in the list, as the following illustration shows.

ListBox control

ListBox control

A ComboBox control is a combination of a text box and a list box. By default, the combo box appears as a text box, but when users click the drop-down arrow, a list appears. When a user selects an item, it becomes highlighted and is visible in the default view, as the following illustration shows.

ComboBox control

ComboBox control

The processes for adding items to list boxes and combo boxes are similar. You can use the Add method of the ListBox control and the Add method of the ComboBox control to add items.

Try It!

To add items to a list box

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

    A new Windows Forms project opens.

  3. Add a ListBox control to the form, leaving the default name, ListBox1.

  4. Double-click the form to add the default event handler in the Code Editor.

  5. In the Form_Load event handler, add the following code to add items to the list.

    With Me.ListBox1.Items
        .Add("red")
        .Add("blue")
        .Add("green")
        .Add("yellow")
        .Add("orange")
        .Add("pink")
        .Add("brown")
        .Add("black")
    End With
    
  6. Press F5 to run the program.

    The form opens and displays a ListBox that contains a list of colors.

  7. Close the program.

Performing an Action When the Selected Item Changes

You can perform an action, such as displaying a color, when the user selects the corresponding item in a list box. To do this, you write code in the SelectedIndexChanged event handler of the ListBox. There is also a corresponding event for the ComboBox control.

You can determine which item in the list is selected by using the SelectedItem property. You can assign the selected color in the list to the BackColor property of a control by using the FromName method of the Color object.

To display the color selected in the list box

  1. Add a TextBox control to the form, leaving the default name TextBox1.

  2. Double-click the ListBox control to add the default event handler in the Code Editor.

  3. In the ListBox_SelectedIndexChanged event handler, add the following code to display the color in the text box. Make sure that the ColorName variable contains a value before you assign it to the BackColor property of the text box.

    Dim ColorName As String = CStr(ListBox1.SelectedItem)
    
    If ColorName IsNot Nothing Then
        Me.TextBox1.BackColor = 
            System.Drawing.Color.FromName(ColorName)
    End If
    
  4. Press F5 to run the program.

  5. When the form opens, click an item in the list box to see the selected color displayed in the text box.

  6. Close the program.

Checking Whether an Item Exists in the List

When you add items to a list, you often don't want to duplicate an existing item. For example, you might want users to be able to copy an item in the list box to a combo box by double-clicking the item. If the item is already in the combo box, it should not be added again. You can use the Contains method to determine whether the item is already in the combo box. There is also a corresponding Contains method for the list box.

To add items to a combo box

  1. Add a ComboBox control to the form, leaving the default name, ComboBox1.

  2. Open the Code Editor by right-clicking the form and clicking View Code.

  3. In the Code Editor, in the Class Name drop-down list, click ListBox1.

  4. In the Method Name drop-down list, click DoubleClick to create the event handler.

  5. Add the following code to the ListBox1_DoubleClick event handler to add items to the combo box. Because you can have an empty list box, you should make sure that the selected item is not empty before you add it.

    If ListBox1.SelectedItem IsNot Nothing Then
        If Not ComboBox1.Items.Contains(Me.ListBox1.SelectedItem) Then
            Me.ComboBox1.Items.Add(Me.ListBox1.SelectedItem)
        End If
    
        Me.ComboBox1.SelectedItem = Me.ListBox1.SelectedItem
    
    End If
    
  6. Press F5 to run the program.

    Double-click items in the list box and verify that they are added to the combo box only one time.

  7. Close the program.

Deleting an Item from a List Box

You can delete an item from a list box by using the Remove method. For example, instead of copying items from a list box to a combo box, you might decide to move them. You can move an item by adding it to the combo box first and then deleting it from the list box.

To delete items from a list box

  1. Add the following code above the End If statement in the ListBox_DoubleClick event handler.

    Me.ListBox1.Items.Remove(Me.ListBox1.SelectedItem)
    
  2. Press F5 to run the program.

  3. Double-click items in the list box and verify that they are removed from the list box and added to the combo box.

Next Steps

In this lesson, you learned how to add items to and remove items from lists. You also learned how to perform an action when a user selects an item. In the next lesson, you will learn how to use controls that display the date and time.

Next Lesson: Displaying Dates: Using MonthCalendar and DateTimePicker Controls

See Also

Reference

ListBox Control Overview (Windows Forms)

ComboBox Control Overview (Windows Forms)

Other Resources

Creating the Visual Look of Your Program: Introduction to Windows Forms