Using the CheckBox control
When you select a CheckBox control, the control displays the selected tag. You typically use this control to provide yes/no or true/false options. The checkbox controls that can be grouped display multiple options from which the user can select one or more options.
The CheckBox control is the same as the OptionButton control, where each is used to indicate the user's choice. The difference is that for a set of OptionButton, only one of them can be selected at a time, and any number of check boxes can be selected for the CheckBox control.
More information for a simple example of a CheckBox control, see "Selecting a single option with a check box" in Chapter Three, "Forms, controls, and menus."
Value Property
The Value property of the CheckBox control indicates which of the check boxes are in the selected, unselected, or prohibited State (dim). When selected, the value setting is 1. For example:
The user clicks the CheckBox control to specify the selected or unselected state, and then detects control state and writes the application based on this information to perform certain actions.
By default, the CheckBox control is set to vbunchecked. To select several check boxes in advance in a column check box, set the Value property to vbchecked in the Form_Load or form_initialize procedure.
You can set the Value property to vbgrayed to disable the check box. For example, you may sometimes want to disable a check box before a condition is met.
Click event
Whenever you click the CheckBox control, it triggers the Click event and then writes the application to perform certain actions based on the state of the check box. In the following example, each time a CheckBox control is clicked, its Caption property is changed to indicate the selected or unselected state.
Private Sub Check1_click ()
If Check1.value = vbchecked Then
Check1.caption = "Checked"
ElseIf Check1.value = vbunchecked Then
Check1.caption = "Unchecked"
End If
End Sub
Note If you try to double-click the CheckBox control, double-click as two times, and each time you click it, this means that the CheckBox control does not support double-clicking events.
Responding to mouse and keyboard
The CheckBox control's Click event is also triggered by using the TAB key on the keyboard and pressing the SPACEBAR key, which shifts the focus to the CheckBox control.
You can add hyphens before a letter of the Caption property and create a keyboard shortcut to toggle the selection of the CheckBox control. For example:
In this example, pressing the ALT+C key will toggle the state of the control between the selected and unselected.
Enhance the visual effects of a CheckBox control
The checkbox control, like the CommandButton and OptionButton controls, can enhance its visual effects by changing the setting value of the Style property and using the picture, DownPicture, and DisabledPicture properties. For example, you might sometimes want to add an icon or bitmap to a check box, or display a different image when you click or disable a control.
Using the ComboBox control
The combo box control combines the functions of a text box and a list box. With this control, users can select items by entering text in a combo box, or by selecting items from a list.
The combo box provides the user with a list to choose from. If the number of items exceeds the number of items that the combo box can display, scroll bars appear automatically on the control. Users can scroll the list up and down or left and right.
When to use a combo box instead of a list box
Typically, a combo box applies to a list of suggested options, and when you want to limit your input to a list, you use a list box. The combo box contains the editing area, so you can enter an option that is not in the list into the column area.
In addition, the combo box saves space on the form. You can easily fit a combo box where you cannot fit a list box until you click the down arrow of the combo box (except for the combo box in style 1, which is always in the Drop-down state) to display the full list.
For a simple example of these controls, see "Using ListBox and ComboBox" in chapter Three, "Forms, controls, and menus." For more information about list box controls, also see "Using the ListBox Control" later in this chapter.
Data binding attributes
ComboBox controls for standard and data-bound editions in Visual Basic. Although the information in most standard-type databases can be displayed, edited, and updated through these two versions, DBCombo provides more advanced data access features. The DBCombo control also supports a set of properties and methods that are different from the standard combo box controls.
For more information, see the "Using the DataList and DataCombo" Control in the seventh chapter, "Using Visual Basic standard controls."
Style of combo box
There are three combo box styles here. Each style can be set at design time or runtime, and each style uses a numeric or corresponding Visual Basic constant to set the style of the combo box.
Drop-down combo Box
Under the default setting (Style = 0), the combo box is Drop-down. The user can enter text directly (as in the text box), or click the accompanying arrow to the right of the combo box to open the list of options. When you select an option, insert this option into the text section at the top of the combo box. When the control gets focus, you can also press ALT + DOWN ARROW to open the list.
Simple combo box
Setting the combo box style property to 1 specifies a simple combo box that displays the list at any time. To display all items in the list, you must draw the list box large enough. A vertical scroll bar is automatically inserted when the number of entries exceeds the limit that can be displayed. Users can enter text directly or select from a list. Like a drop-down combo box, a simple combo box also allows users to enter options that are not in the list.
Drop-down list box
A drop-down list box (Style = 2) is similar to a regular list box-it displays a list of items that the user must select from. However, the Drop-down list box differs from a list box in that the list is not displayed unless you click the arrow to the right of the box. The main difference between this list box and the Drop-down combo box is that the user cannot enter an option in the list box, but only select it in the list. You can use this type of list box when there is less space on the form.
Add Project
To add items to a combo box, you should use the AddItem method, which has the following syntax:
You typically add list items to the Form_Load event procedure, but you can also use the AddItem method at any time. This allows you to add items to the list dynamically (in response to the user's actions).
The following code places "Chardonnay," "Fum 抏 Blanc," "Gew 黵 Ztraminer," and "Zinfandel" into a combo box named Combo1,style Property 0 (Vbcombodropdown):
Private Sub Form_Load ()
Combo1.additem "Chardonnay"
Combo1.additem "Fum ' e Blanc"
Combo1.additem "Gewürztraminer"
Combo1.additem "Zinfandel"
End Sub
At run time, whenever the form is loaded and the user clicks the down arrow, the list appears as shown in the figure.
Add Project at design time
You can also add items to the list by setting the list property of the Properties window for the combo box control at design time. Select the List property option and click the down arrow to enter the item, and then press Ctrl+enter to change to a new line.
You can only add items to the end of the list. So, if you want to sort the list alphabetically, you should set the sorted property to True. For more information, see "Sorting Lists" later.
Add an item at a specified location
To add an item at the specified location in the list, you should specify the index value after the new item. For example, the downlink code inserts "Pinot Noir" into the first position and adjusts the position of the other items downward:
Combo1.additem "Pinot Noir", 0
Note that the first position in the specified list is 0 instead of 1 (see figure).
Sort List
Set the sorted property to True and omit the index, you can specify items to add in the list in alphabetical order. The sort is case-insensitive; so "Chardonnay" and "Chardonnay" are treated as a word.
After the sorted property is set to True, using the AddItem method with the index parameter will result in unpredictable unordered results.
Delete items
You can delete items in a combo box by using the RemoveItem method. RemoveItem has a parameter index that specifies the item to delete:
Box. RemoveItem Index
box and index parameters are the same as those in AddItem.
For example, to delete the first item in the list, add the following line of code:
Combo1.removeitem 0
To delete all list items in a combo box, you should use the Clear method:
Combo1.clear
Get list contents with Text property
The simplest common way to get the currently selected item value is to use the Text property. At run time, the Text property corresponds to this literal regardless of what text is entered into the text box portion of the control. It can be a selected list option, or a string that the user enters in a text box.
For example, if the user selects "Chardonnay" in the list box, the following code displays information about the Chardonnay:
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 item currently selected in the Combo1 list box. The code looks at whether "Chardonnay" is selected, and if so, displays the information in the text box.
Access list options with the List property
With the List property, you can access all items in the list. This property contains an array, and each item in the list is an element of the array. Each item is represented as a string. To refer to items in the list, use the following syntax:
Box. List (Index)
The box argument references the combo box, and index is the location of the item. The index of the top item is 0, the index of the next item is 1, and so on. For example, in a text box, the following statement displays the third item in the list (index = 2):
Text1.Text = Combo1.list (2)
Using ListIndex attribute to judge position
To know the location of the selected item in the combo box list, use the ListIndex property. This property sets or returns the index value of the currently selected item in the control and is valid only at run time. Setting the ListIndex property of a combo box also triggers the control's Click event.
If you select the first (top) item, the property value is 0, the next Item property value selected is 1, and so on. If the item is not selected, or if the user enters an option (style 0 or 1) in the combo box and does not select an existing item in the list, then ListIndex is-1.
Note the NewIndex property can be used to track the index of the last item added in the list. This is useful when inserting items into a sorted list.
Returns the number of items with the ListCount property
To return the number of items in the combo box, use the ListCount property. For example, the following statement uses the ListCount property to determine the number of items in a combo box:
Text1.Text = "You have" & Combo1.listcount & "_
Entries listed "