Provide users with controls to choose from
Most applications need to provide the user with choices, such as a simple "Yes/No" option, or choosing from a list of hundreds of possibilities. Visual Basic includes several standard controls for providing selections. The following table summarizes these controls and their uses.
Select a single option with CheckBox
CheckBox indicates whether a specific state is selected (on) or cleared (off). Using a CheckBox in an application provides the user with a "True/False" or "yes/no" choice. Because CheckBoxes work independently of each other, users can select as many CheckBoxes as they want at the same time. For example, in Figure 3.9, both bold and italics can be selected.
CheckBox App
This example uses a CheckBox to determine whether text should be displayed in normal font or in italics. For a working version of this example, see Check.frm in the application example Controls.vbp. As shown in Figure 3.10, this application has a TextBox, a Label, a CommandButton and two CheckBoxes.
The following table lists the property settings for objects in the application.
When bold or italic is selected, the Value property of the CheckBox is set to 1, and to 0 when not selected. The default Value is 0, so unless you change the Value property, the CheckBox will not be selected when it is first displayed. You can use the constants vbChecked and vbUnchecked to represent the values 1 and 0.
event in checkbox application
When a CheckBox is clicked, the CheckBox's Click event occurs. This event procedure tests whether the CheckBox is selected (that is, whether Value=vbChecked). If selected, converts the text to bold or italic by setting the Bold or Italic property of the Font object returned by the Font property of the TextBox.
PRivateSubchkBold_Click()
IfChkBold.Value=vbCheckedThen 'If selected.
txtDisplay.Font.Bold=True
Else 'If not selected.
txtDisplay.Font.Bold=False
EndIf
EndSub
PrivateSubchkItalic_Click()
IfChkItalic.Value=vbCheckedThen 'If selected.
txtDisplay.Font.Italic=True
Else 'If not selected.
txtDisplay.Font.Italic=False
EndIf
EndSub
Group options using option buttons
Option buttons represent a set of two or more choices given to the user. However, unlike CheckBox, option buttons always work as part of a group; therefore, selecting an option button immediately clears the other buttons in the group. Define an option button group that tells the user, "This is a set of choices, choose one and only one."
For example, in the option button group shown in Figure 3.11, the user can only select one of the three option buttons.
Create an option button group
All option buttons placed directly in a form (that is, not in a Frame or PictureBox) form a group. If you want to create other option button groups, you must place some of them in a Frame or PictureBox.
All option buttons in a given Frame form a separate group, as do all option buttons in a PictureBox. When using this method to generate an independent group, you must first draw a Frame or PictureBox, and then draw the option buttons inside. Figure 3.12 shows a form with two option button groups.
After a group of option buttons is drawn in the Frame, the user can only select one option button within the group. To group controls in a Frame, follow these steps:
1. Select the Frame control in the "Toolbox" and draw the Frame on the form.
2. Select the OptionButton control in the "Toolbox" and draw the control in the Frame.
3. If you also want to add option buttons to the Frame, repeat step 2. Draw the Frame first, and then draw each control on the Frame, so that when the Frame is moved, the controls will move together. If you move an existing control to the Frame, the control will not move with the Frame.
Note that if you group existing controls into a Frame, you can select these controls and then cut and paste them to the Frame or Picture control.
Control container
When the control is an independent object, there is some kind of parent-child relationship between the form and the control. Figure 3.12 shows how option buttons can be contained within a form or a Frame control.
To understand the concept of containers, first imagine that all controls are children of the form in which they reside. In fact, most controls support the read-only Parent property, whose value is the form in which the control is located. As a child, a control determines its position on the parent form. The Left property and Top property of the control are relative to the parent form, and the control cannot move beyond the boundaries of the parent form. When the container is moved, the control is also moved, so the relative position of the control and the container remains unchanged (that is, the Left property and Top property of the control remain unchanged).
Select or disable option buttons
An option button can be selected in the following ways:
1.Click the option button with your mouse during runtime.
2. Use the Tab key to locate the option button group, and then use the arrow keys (arrow keys) to locate the option button within the group.
3. Set its Value property to True using code: optChoice.Value=True
4. Use the shortcut key specified in the Label's title.
5. To make a button the default button in the option button group, just set its Value property to True at design time. It remains selected until the user selects a different option button or changes it with code.
To disable an option button, set its Enabled property to False. When the program is running, if this option button turns gray, it means that this option button cannot be selected.
Options app
The form shown in Figure 3.13 uses option buttons to select the processor type and operating system for a computer. When the user selects an option button within the group, the Label's title changes to reflect the current selection. For a working version of this example, see Options.frm in the application example Controls.vbp.
The following table lists the settings for object properties in the application.
Events in Options application
The options application responds to events as follows:
The Click events of the first three option buttons assign a corresponding description to the form-level string variable strComputer.
The Click events of the latter two option buttons assign a corresponding description to the second form-level variable strSystem.
The key to this method is to use these two form-level variables, strComputer and strSystem. These two variables have different string values, and the final selection of the option button depends on these values.
Each time a new option button is selected, the code for its Click event updates the value of the corresponding variable.
PrivateSubopt586_Click()
strComputer=Pentium
CallDisplayCaption
EndSub
It then calls a procedure called DisplayCaption, which concatenates the two variables and changes the Label's Caption property.
SubDisplayCaption()
lblDisplay.Caption=Youselecteda&
_strComputer&running&strSystem
EndSub
The subroutine is used because the process of changing the Caption property is basically the same for all five option buttons, except that the value of the variable varies depending on the situation. This avoids duplicating the same code in every Click event.
Details of variables and subprocesses are discussed in Chapter 5, "Programming Basics."
Using ListBox and ComboBox
ListBox and ComboBox provide users with choices. By default, options are displayed in a vertical single column, but can also be set to multiple columns. If the number of items exceeds the number that the ComboBox or ListBox can display, a ScrollBar automatically appears on the control. The user can then scroll up, down, left, and right through the list. Figure 3.14 shows a single-column ListBox.
ComboBox has the functions of both TextBox and ListBox. This control allows the user to make selections by typing text or selecting items in a list. Figure 3.15 shows a ComboBox.
In contrast to other controls that contain a single value, such as a Label's Caption property or a TextBox's text property, ListBox and ComboBox contain multiple values or collections of values. Use built-in methods to add, delete, and persist values in a collection at runtime. To add several items to a ListBox named List1, the code is as follows: List1.AddItemParis
List1.AddItemNewYork
List1.AddItemSanFrancisco
ListBox and ComboBox are effective ways to provide users with a large number of options in a limited space.
For more information about the ListBox and ComboBox controls, see Chapter 7, "Using Visual Basic's Standard Controls."
->