Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.
<<PreviousNext>>

The Need for the Combo Box

Similar to the radio button and the check box, the list box provides a fixed set of choices for the user, and does not allow the user to add additional items directly into the box. There are cases in which it is necessary for the user to add more items at run time. For example, you may provide a list of zip codes in a list box for your user to choose from, but a new customer may have a zip code different from any existing ones. This is a case in which the combo box comes in handy. The combo box, as the name suggests, provides a combination of a text box and a list box (Figure 3-15). Its Text property represents the content of the text box area. Under certain DropDownStyle property settings (discussed later in this subsection), its text box area can be used to enter data, similar to a typical text box; and its list box can be used to select an item like a list box.

Figure 3-15
Combo box: icon and appearance

Similar Properties

The combo box has many properties that behave exactly the same as those of the list box:

Similar Methods

The combo box also has the Items.Add and Items.RemoveAt methods to add and remove items from its list. Note that an item entered into its text box area will not be automatically added to its list; that is, when another item appears in its text box, whatever was previously entered will be lost. If you need to keep the entered text in its list box, you will need to write code to handle it. Suppose you have a combo box named cboZipCode in your form. To add (to its list) the item that has just been entered into its text box area, you can code the following:

cboZipCode.Items.Add(cboZipCode.Text)

As you can see in the code, the text area is recognized as its Text property. The code will add whatever appears in the combo box’s text box into its list.

The DropDownStyle Property

The combo box has the DropDownStyle property, which the list box does not have. The available settings include the following:

Notice that when its DropDownStyle is set to DropDown or DropDownList, it requires only as much space as a single-line text box. Its text box clearly shows what has been selected or entered. In contrast, the list box requires more space to meaningfully display the list, and has no capability to accept items the user may desire to key in.
On the other hand, the list box also has some properties that are not available in the combo box. For example, the combo box does not have the SelectionMode property, which allows multiple selections. Only one item can be selected from its list; therefore, the combo box can be used as an alternative only to radio buttons and not to check boxes.

Between the List Box and Combo Box

Because there are many similarities and some differences between the list box and the combo box, how do you decide which control to use given a certain circumstance? Here is a checklist you can use to make your decision:

If you cannot decide from these guidelines, consider how each control will affect the layout of your form. The control that will give a more pleasing look in your form layout should be your choice.
The following table summarizes the properties pertaining to the list box and combo box.

Property Applicable Object Use/Remark
Items.Count List box, combo box Returns the number of items in the list.
SelectedIndex List box, combo box Returns or sets a number pointing to the position of the item clicked or selected by code; for example, the code: cboWeekDays.SelectedIndex = 3 will select the fourth item in the combo box named cboWeekDays. The item (Wednesday) will appear in the box.
SelectionMode List box When set to None, no item can be selected from the list box; to One, only one item can be selected; otherwise, multiple items can be selected (MutiSimple or MultiExtended).
Sorted List box, combo box When set to True, items in the list will appear in ascending order.
Items List box, combo box Gives the items in the list, indexed with a value starting at 0; for example, lstMonths.Items(3) will give the fourth item (April) in the list.
GetSelected (method) List box Returns a True or False value that indicates whether a particular item in the list box is selected; for example, lstMonths.GetSelected(0) will give a value True if the first item (January) is selected.
DropDownStyle Combo box Can be DropDown, Simple, or DropDownList. Refer to the text in this section for details of behavior under each setting.
SelectedItem List box, combo box Gives the item selected (clicked) from the list
Text Combo box Gives or sets the content of the text box area of the combo box
Last change: February 13 2016 18:47:26.
<<PreviousNext>>