Visual Basic Tutorials – Controls – The Combo Box

Visual Basic Controls – The ComboBox Control

The ComboBox control also contains multiple items but typically occupies less space on the screen. The ComboBox control is an expandable ListBox control. The user can expand it to make a selection, and collapse it after the selection is made. The real advantage of the ComboBox control, however, is that the user can enter new information in the ComboBox, rather than being forced to select from the items listed.

Combo boxes present a list of choices to the user such that only the selected item is displayed while the rest of the list is hidden. The user can see the list box by clicking on the arrow on the right hand corner of the combo box’s text box portion.

If the number of items exceeds than what can be displayed in the list box portion of combo box, then a vertical scroll bar will automatically appear on the control. The user can then scroll up and down through the list.

 

The Combo Box Properties:

Typically, a combo box has all the properties exhibited by the text box and the list box. Properties unique to combo box are:

  • Style– The value assigned to this property decides the look’ of the combobox. There are three combobox styles:
    • Simple ComboBox – Specifies a simple combobox in which the list is displayed at all times. The user can also enter text.
    • Drop down ComboBox – The user can either enter text directly (as in a text box) or click the detached arrow at the right of the combo box to open a list of choices.
    • Drop down List Combo Box – Displays a list box when the user clicks on the arrow. The user can only select an item from the list and cannot enter text in the box.
  • Locked – Used to specify whether the user can enter a value in the text box section of the control.

Just as in the case of the list box, the comboBox has the following properties:

  • Index
  • ListCount
  • Sorted

Unlike a list box, a combo box does not allow multiple selections.

The ComboBox Methods:

The methods of the combo box are, typically, the same as those of the list box.

  • AddItem – This method will add the specified item to the combo box control.
  • RemoveItem – used to delete an item form the combo box control.

The ComboBox Events:

The combo box supports all the events that a list box has such as:

  • Click – Occurs each time the user clicks on the list box.
  • Scroll – Occurs when a user scrolls through the list in the list box.

Similarly, the combo box also has events in common with the text box:

  • Change – occurs when the user changes the text in the text box portion of the combo box.

In addition, the combo box has events that are unique to it. One such event is:

  • DropDown – occurs when the user clicks on the arrow to display the list.

Adding items to ComboBox

At run-time, you can add items to a combo box using the AddItem() method, which adds items to the list box part. You can also add items to the list box using the List property, which is an indexed array of items in the list box. If you want to set text in the text box, set the combo box’s Text property.

Here’s an example; in this case, we add four items to a combo box’s list:

Private Sub Form_Load()

Combo1.AddItem ("Item 0")

Combo1.AddItem ("Item 1")

Combo1.AddItem ("Item 2")

Combo1.AddItem ("Item 3")

End Sub

You can also add items to the list with the List property. Here we create a fifth item and give it a caption this way:

Private Sub Form_Load()

Combo1.AddItem ("Item 0")

Combo1.AddItem ("Item 1")

Combo1.AddItem ("Item 2")

Combo1.AddItem ("Item 3")

Combo1.List(4) = "Item 4"

End Sub

Removing Items from a Combo Box

Just as with list boxes, you can remove items from combo boxes using the RemoveItem() method. You just pass the index of the item you want to remove from the combo box’s list to RemoveItem().

Here’s an example. In this case, we can add four items to a combo box, Items 0 through 3, when the combo box’s form loads:

Private Sub Form_Load()

Combo1.AddItem ("Item 0")

Combo1.AddItem ("Item 1")

Combo1.AddItem ("Item 2")

Combo1.AddItem ("Item 3")

End Sub

Next, we remove Item 1 in the list this way:

Private Sub Command1_Click()

Combo1.RemoveItem 1

End Sub

TIP: You should note that removing an item from a combo box changes the indexes of the remaining items. After you remove Item 1 in the preceding example, Item 2 now gets Index 1 and Item 3 gets Index 2. If you want to change those indexes back to their original values, set the items Index properties.