kdswhu# #,Qsxw#9Dolgdwlrq: 2emhfwlyhv

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

262

&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.

To see a demonstration of the lab solution, click this icon.


(CD-ROM plays the demonstration, "Lab 9 Solution.")

Estimated time to complete this lab: 30 minutes

2EMHFWLYHV
After completing this lab, you will be able to:

• Set validation properties of a text box.


• Use the Validate event to validate user input.
• Validate the user-entered numeric data.

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.

X Open the loan project


• Open the loan project that you have been working on, or open the loan project in the <install
folder>\Labs\Lab09 folder.
X Code the Validate events
… 1. Set the CausesValidation property of the Cancel button on frmLogon to False.

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

Private Sub txtPassword_Validate(Cancel As Boolean)


If txtPassword.Text = "" Then
MsgBox "A password is required."
Cancel = True
End If
End Sub

… 3. Save the project and test the application.

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.

X Check for an invalid value


… 1. Run the Loan application, type a non-numeric value into the txtPurchase text box, and click Monthly
Payment.
The run-time error Type Mismatch should occur.
… 2. In the Microsoft Visual Basic Error dialog box, click End.
… 3. Open the Code Editor window for frmMain and move to the cmdMonthly_Click event procedure.
… 4. Before calling the MonthlyPayment function, test to see if the value entered in txtPurchase is a
numeric value:
If IsNumeric(txtPurchase.Text) Then

… 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.

You might also like