Condividi tramite


Visual Basic Concepts

Using the Combo Box Control

A combo box control combines the features of a text box and a list box. This control allows the user to select an item either by typing text into the combo box, or by selecting it from the list.

Figure 7.6   The combo box control

Combo boxes present a list of choices to the user. If the number of items exceeds what can be displayed in the combo box, scroll bars will automatically appear on the control. The user can then scroll up and down or left to right through the list.

When to Use a Combo Box Instead of a List Box

Generally, a combo box is appropriate when there is a list of suggested choices, and a list box is appropriate when you want to limit input to what is on the list. A combo box contains an edit field, so choices not on the list can be typed in this field.

In addition, combo boxes save space on a form. Because the full list is not displayed until the user clicks the down arrow (except for Style 1, which is always dropped down), a combo box can easily fit in a small space where a list box would not fit.

For More Information   See "Using List Boxes and Combo Boxes" in "Forms, Controls, and Menus" for a simple demonstration of these controls. Also see "Using the List Box Control" later in this chapter for more information about the list box control.

Data-Bound Features

Visual Basic includes both standard and data-bound versions of the combo box control. While both versions allow you to display, edit, and update information from most standard types of databases, the data-bound combo box provides more advanced data access features. The Data-Bound combo box control also supports a different set of properties and methods than the standard combo box control.

For More Information   See "Using the DataCombo and DataList Controls" in "Using Visual Basic's Standard Controls" for more information.

Combo Box Styles

There are three combo box styles. Each style can be set at design time and uses values, or equivalent Visual Basic constants, to set the style of the combo box.

Style Value Constant
Drop-down combo box 0 vbComboDropDown
Simple combo box 1 vbComboSimple
Drop-down list box 2 vbComboDropDownList

Figure 7.7   Combo box styles

With the default setting (Style = 0 – Dropdown Combo), a combo box is a drop-down combo box. 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. Selecting one of the choices inserts it into the text portion at the top of the combo box. The user also can open the list by pressing ALT+ DOWN ARROW when the control has the focus.

Simple Combo Box

Setting the Style property of a combo box to 1 – Simple Combo specifies a simple combo box in which the list is displayed at all times. To display all entries in the list, you must draw the list box large enough to display the entries. A vertical scroll bar is automatically inserted when there are more entries than can be displayed. The user can still enter text directly or select from the list. As with a drop-down combo box, a simple combo box also allows users to enter choices not on the list.

A drop-down list box (Style = 2 – Dropdown List) is like a regular list box — it displays a list of items from which a user must choose. Unlike list boxes, however, the list is not displayed until you click the arrow to the right of the box. The key difference between this and a drop-down combo box is that the user can't type into the box, he can only select an item from the list. Use this type of list box when space is at a premium.

Adding Items

To add items to a combo box, use the AddItem method, which has the following syntax:

box.AddItemitem[, index]

Argument Description
box Name of the list or combo box.
item String expression to add to the list. If item is a literal constant, enclose it in quotation marks.
index Specifies where the new item is to be inserted in the list. An index of 0 represents the first position. If index is omitted, the item is inserted at the end (or in the proper sorted order).

While list items are commonly added in the Form_Load event procedure, you can use the AddItem method at any time. This gives you the ability to add items to the list dynamically (in response to user actions).

The following code places "Chardonnay," "Fumé Blanc," "Gewürztraminer," and "Zinfandel" into a combo box named Combo1 with its Style property set to 0 (vbComboDropDown):

Private Sub Form_Load ()
   Combo1.AddItem "Chardonnay"
   Combo1.AddItem "Fumé Blanc"
   Combo1.AddItem "Gewürztraminer"
   Combo1.AddItem "Zinfandel"
End Sub

Whenever the form is loaded at run time and the user clicks the down arrow, the list appears as shown in Figure 7.8.

Figure 7.8   "Wine list" combo box

Adding Items at Design Time

You can also enter items into the list at design time by setting the List property in the Properties window of the combo box control. When you select the List property option and then click the down arrow, you can type list items and then press the CTRL+ENTER key combination to start a new line.

You can only add items to the end of the list. So, if you want to alphabetize the list, set the Sorted property to True. See "Sorting a List" below for more information.

Adding an Item at a Specified Position

To add an item to a list at a specific position, specify an index value after the new item. For example, the next line of code inserts "Pinot Noir" into the first position, adjusting the position of the other items downward:

Combo1.AddItem "Pinot Noir", 0

Notice that it is 0, not 1, that specifies the first item in a list (see Figure 7.9).

Figure 7.9   Adding an item to a list

Sorting a List

You can specify that items be added to a list in alphabetical order by setting the Sorted property to True and omitting the index. The sort is not case-sensitive; thus, the words "chardonnay" and "Chardonnay" are treated the same.

When the Sorted property is set to True, using the AddItem method with the index argument can lead to unpredictable, unsorted results.

Removing Items

You can use the RemoveItem method to delete items from a combo box. RemoveItem has one argument, index, which specifies the item to remove:

box.RemoveItemindex

The box and index arguments are the same as for AddItem.

For example, to remove the first entry in a list, you would add the following line of code:

Combo1.RemoveItem 0

To remove all list entries in a combo box, use the Clear method:

Combo1.Clear

Getting List Contents with the Text Property

Usually, the easiest way to get the value of the currently selected item is to use the Text property. The Text property corresponds to whatever is entered in the text box portion of the control at run time. This can be either a selected list item or a string that a user types in the text box.

For example, the following code displays information about Chardonnay if a user selects "Chardonnay" from a list box:

Private Sub Combo1_Click ()
   If Combo1.Text = "Chardonnay" Then
      Text1.Text = "Chardonnay is a medium-bodied _
      white wine."
   End If
End Sub

The Text property contains the currently selected item in the Combo1 list box. The code checks to see if "Chardonnay" has been selected and, if so, displays the information in the text box.

Accessing List Items with the List Property

The List property provides access to all items in the list. This property contains an array in which each item in the list is an element of the array. Each item is represented in string form. To refer to an item in the list, use this syntax:

box.List(index)

The box argument is a reference to a combo box, and index is the position of the item. The top item has an index of 0, the next has an index of 1, and so on. For example, the following statement displays the third item (index = 2) in a list in a text box:

Text1.Text = Combo1.List(2)

Determining Position with the ListIndex Property

If you want to know the position of the selected item in a list in a combo box, use the ListIndex property. This property sets or returns the index of the currently selected item in the control and is available only at run time. Setting the ListIndex property for a combo box also generates a Click event for the control.

The value of this property is 0 if the first (top) item is selected, 1 if the next item down is selected, and so on. ListIndex is – 1 if no item is selected or if a user enters a choice in a combo box (Style 0 or 1) instead of selecting an existing item in the list.

Note   The NewIndex property allows you to keep track of the index of the last item added to the list. This can be useful when inserting an item into a sorted list.

Returning the Number of Items with the ListCount Property

To return the number of items in a combo box, use the ListCount property. For example, the following statement uses the ListCount property to determine the number of entries in a combo box:

Text1.Text = "You have " & Combo1.ListCount & " _
entries listed"