Professional Documents
Culture Documents
kdswhu# #,Qsxw#9Dolgdwlrq: 2emhfwlyhv
kdswhu# #,Qsxw#9Dolgdwlrq: 2emhfwlyhv
kdswhu# #,Qsxw#9Dolgdwlrq: 2emhfwlyhv
&KDSWHU#<=#,QSXW#9DOLGDWLRQ
In this lab, you will write code and set properties to control how the user interacts with your loan
application. By implementing these steps, you will make it easier for users to enter valid data. In addition,
you will add controls that provide visual feedback to further aid a user in using your interface.
You can continue to work with the Loan project files you've created, or you can use the files provided for
you in the folder <install folder>\Labs\Lab09.
2EMHFWLYHV
After completing this lab, you will be able to:
The solution for this lab is located in the folder <install folder>\Labs\Lab09\Solution.
To complete the exercises in this lab, you must have the required software. For detailed information about
the labs and setup for the labs, see Labs in this course.
3UHUHTXLVLWHV
There are no prerequisites for this lab.
([HUFLVHV
The following exercises provide practice working with the concepts covered in Chapter 9.
([HUFLVH#4=#$GGLQJ#&RGH#WR#WKH#9DOLGDWH#(YHQW
In this exercise, you will add code to the Validate events of the User Name and Password text boxes. This
code will verify that the user enters text in both fields.
([HUFLVH#5=#9DOLGDWLQJ#1XPHULF#'DWD
The MonthlyPayment and TotalPaid functions require a numeric value in the txtPurchase text box.
Calling either function with a string in the text box causes a run-time error. In this exercise, you will protect
against this error by checking for a number in the text box before calling the functions.
Chapter 9: Input Validation
262 ([HUFLVH#4=#$GGLQJ#&RGH#WR#WKH#9DOLGDWH#(YHQW
In this exercise, you will add code to the Validate events of the User Name and Password text boxes. This
code will verify that the user enters text in both fields.
Note The Validate event occurs only when the CausesValidation property of the control that is about
to receive the focus is set to True. The Validate event also includes a Cancel argument which, when
set to True, allows the control to retain focus.
… 2. Add code to the Validate events for txtUserName and txtPassword that verifies data was entered into
each text box and sets the focus back to the corresponding text box if no data was entered:
Private Sub txtUserName_Validate(Cancel As Boolean)
If txtUserName.Text = "" Then
MsgBox "A user name is required."
Cancel = True
End If
End Sub
Notes
Page 60
Chapter 9: Input Validation
262 ([HUFLVH#5=#9HULI\LQJ#1XPHULF#'DWD
The MonthlyPayment and TotalPaid functions require a numeric value in the txtPurchase text box.
Calling either function with a string in the text box causes a run-time error. In this exercise, you will protect
against this error by checking for a number in the text box before calling the functions.
… 5. If the value is numeric, call the MonthlyPayment function and display the results in a message box.
… 6. If the value is not numeric:
a. Display a message box instructing the user to type a numeric value.
b. Set focus back to the txtPurchase text box.
c. Highlight the invalid text in the text box.
Note For more information about highlighting text, see Working with Selected Text in Chapter 7.
… 7. Add the same type of error checking to the cmdTotal_Click event procedure.
… 8. Save and test your work.
To see an example of how your code should look, click this icon.
(CD-ROM displays a hint.)
Notes
Page 61
This page intentionally left blank.