step 3 click file on the menu bar to view the individual ... · uses an inputbox object where users...
TRANSCRIPT
Click File on the menu bar to view the individual menu items and theirassociated icons on the File menu.
The standard File menu items (New, Open, Save, Save As, Print, Print Preview, and
Exit) are displayed with their associated icons and shortcut keys (Figure 6-16). The other
menus also contain standard items. You can code an event handler for each menu item by
double-clicking the item.
InputBox Function
To calculate the average vehicle speed, the Highway Radar Checkpoint applicationuses an InputBox object where users enter the speed for each vehicle. The InputBoxobject is a dialog box that prompts the user to enter a value. Similar to a MessageBoxobject, you code the InputBox function to specify when the InputBox object appears.The InputBox function displays a dialog box that consists of a message asking for in-put, an input area, a title, an OK button, and a Cancel button (see Figure 6-3 on page379). When the user enters the text and clicks the OK button, the InputBox functionreturns this text as a string. If the user clicks the Cancel button, the function returnsa null string (""). The code shown in Figure 6-17 demonstrates the syntax of theInputBox function:
STEP 3
User Interface Design 387
standard File
menu
FIGURE 6-16
ONLINE REINFORCEMENT
To view a video of the process
in the previous steps, visit
scsite.com/vb2010/ch6 and
then select Figure 6-14.
FIGURE 6-17
General Format: InputBox Function
strVariableName = InputBox("Question to Prompt User", "Title Bar")
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 387
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
For example, the code in Figure 6-18 creates a dialog box that requests the user’sage for a driver’s license application. The string returned by the InputBox function isassigned to the strAge variable.
When the application is executed, the InputBox object in Figure 6-19 opens,requesting that the user enter her age. The InputBox function can be used to obtaininput instead of a TextBox object.
The InputBox object returns all entered data as a string, which then can beconverted to the appropriate data type.
InputBox Object Default Value
The InputBox object can be assigned a default value. For example, if a collegeapplication for admission requests the student’s home state and the college or univer-sity is located in Virginia, the most likely state, Virginia, can be the default value inthe InputBox, as shown in Figure 6-20.
The code to produce this input box is shown in Figure 6-21.
388 Chapter 6 Loop Structures
FIGURE 6-18
assigned tostrAge
FIGURE 6-19
default value
FIGURE 6-20
FIGURE 6-21
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 388
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
As you can see, the third argument for the InputBox function call is the defaultvalue that is placed in the input box. It must be a string value and follow the syntaxshown in Figure 6-21 on the previous page.
InputBox Object for Highway Radar Checkpoint Application
The Highway Radar Checkpoint application uses an InputBox object that requeststhe speed of vehicles numbered 1–10, as shown in Figure 6-22.
The code for the Radar Speed InputBox is shown in Figure 6-23. Notice that theprompt message for the user is assigned to the strInputBoxMessage variable, and thetitle bar text (Radar Speed) is assigned to the strInputBoxHeading variable.
The variable intNumberOfEntries identifies the vehicle number. It is in-cluded in the prompt message through the use of concatenation. The variableintNumberOfEntries is incremented later in the code so that it refers to the correctvehicle each time the InputBox function call is executed.
In Figure 6-23, the default value is specified as a space (" "). When the input boxis displayed, a space will be selected in the input area. This space is required so that ifa user clicks the OK button without entering any data, the InputBox will not return anull character (""), which indicates the user clicked the Cancel button. This normallyis a good programming practice.
When the user clicks the Cancel button in an input box and the InputBox func-tion returns a null character, the program can test for the null character to determinefurther processing.
User Interface Design 389
FIGURE 6-22
HEADS UP
An InputBox object allows you to
gather input without placing a
TextBox for the requested value
on the Windows Form object.
FIGURE 6-23
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 389
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
Displaying Data Using the ListBox Object
In the Highway Radar Checkpoint application, the user enters the speed of eachvehicle into the InputBox object, and the program displays these speeds in a list box(see Figure 6-1 on page 378). To create such a list, you use the ListBox objectprovided in the Visual Basic Toolbox. A ListBox displays a group of values, calleditems, with one item per line. To add a ListBox object to a Windows Form object, youcan complete the following steps:
Drag the ListBox object from the Toolbox to the Windows Form objectwhere you want to place the ListBox object. When the pointer is in the correct location,release the left mouse button.
The ListBox object is placed on the form (Figure 6-24).
With the ListBox object selected, scroll in the Properties window to the(Name) property. Name the ListBox object lstRadarSpeed.
The name you entered is displayed in the (Name) property in the Properties window
(Figure 6-25). Notice a ListBox object name begins with lst.
STEP 2
STEP 1
390 Chapter 6 Loop Structures
ListBox object
ListBox .NET
component
FIGURE 6-24
(Name) property
FIGURE 6-25
ONLINE REINFORCEMENT
To view a video of the process
in the previous steps, visit
scsite.com/vb2010/ch6 and
then select Figure 6-24.
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 390
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
After placing a ListBox object on the Windows Form object, you can adjust thesize as needed by dragging the size handles (see Figure 6-24 on the previous page).Be sure to resize the ListBox so that it is large enough to hold the application data.The ListBox object for the Highway Radar Checkpoint application is designed to bewide enough to hold three digits, and long enough to hold 10 numbers (Figure 6-26).
To display the speed of each vehicle in the list box, you must write code to addeach item to the ListBox object. After an item is added, it is displayed in the list box.The general format of the statement to add an item to a ListBox object is shown inFigure 6-27.
In Figure 6-27, the Add procedure will add the item contained in the variableidentified by the Variable Name entry. The syntax for the statement must be followedprecisely.
The code to add the vehicle speed to the lstRadarSpeed ListBox object and thendisplay the speed of each vehicle (decVehicleSpeed) in the ListBox object is shown inFigure 6-28.
User Interface Design 391
three-digit
number
10 numbers
FIGURE 6-26
FIGURE 6-27
General Format: Adding Items to a ListBox Object
lstListBoxName.Items.Add(Variable Name)
FIGURE 6-28
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 391
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
If the number of items exceeds the number that can be displayed in thedesignated space of the ListBox object, a scroll bar automatically is added to the rightside of the ListBox object as shown in Figure 6-29.
To clear the items in a ListBox object, the Clear method works as it does for the TextBox object. The syntax of the statement to clear the ListBox is shown in Figure 6-30.
In the Highway Radar Checkpoint application, the user can select the Clearmenu item to clear the form. The code in Figure 6-31 removes the items from thelstRadarSpeed ListBox.
392 Chapter 6 Loop Structures
FIGURE 6-30
General Format: Clear the ListBox Object
lstListBoxName.Items.Clear()
scroll bar
FIGURE 6-29
FIGURE 6-31
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 392
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
Add Items During Design
The Highway Radar Checkpoint application allows the user to add items to the ListBox object during program execution, but you also can add items to a ListBox objectwhile designing the form. Adding items to the ListBox object during the design phaseallows the user to select an item from the ListBox object during execution. For example,in an application to select a favorite clothing store, you can add items to a ListBox objectnamed lstStores during the form design by completing the following steps:
Assume the lstStores ListBox object already has been placed and namedon the Windows Form object. Select the ListBox object on the Windows Form objectand then click the Items property in the Properties window.
The Items property in the Properties window is selected. An ellipsis button appears to the
right of the (Collection) entry (Figure 6-32).
User Interface Design 393
Items property
ellipsis button
ListBox object
selected
FIGURE 6-32
STEP 1
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 393
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
Click the ellipsis button in the right column of the Items property.The String Collection Editor window opens, allowing you to enter items that will be
displayed in the ListBox object named lstStores (Figure 6-33).
Click in the String Collection Editor window. Type the following items torepresent popular retail stores, pressing ENTER at the end of each line:
Abercrombie & FitchAeropostaleAmerican EagleExpressHollister
The items representing favorite retail stores appear in the String Collection Editor window
on separate lines (Figure 6-34).
STEP 3
394 Chapter 6 Loop Structures
String Collection
Editor window
enter items to
be placed in the
ListBox object
FIGURE 6-33
OK button
items entered for
ListBox object
FIGURE 6-34
STEP 2
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 394
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part
Click the OK button.The Windows Form object displays the stores in the lstStores ListBox object (Figure 6-35).
The user can select one of the items in the ListBox object during execution.
Selected Item Property
The SelectedItem property identifies which item in the ListBox is selected. Anassignment statement is used to assign that property to a variable, as shown in Figure 6-36.
The actual code to show the user’s selection of their favorite store from the List-Box object named lstStores in a message box is shown in Figure 6-37.
User Interface Design 395
items inListBox object
FIGURE 6-35
ONLINE REINFORCEMENT
To view a video of the process
in the previous steps, visit
scsite.com/vb2010/ch6 and
then select Figure 6-32.
STEP 4
FIGURE 6-36
General Format: Assign the Selected Item in a ListBox Object
strVariableName = lstListBoxName.SelectedItem
FIGURE 6-37
0538468459_Ch06_FINAL2.qxd 7/6/10 1:46 PM Page 395
Copyright 2010 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part