Sie sind auf Seite 1von 6

97

/DE#6=#:RUNLQJ#ZLWK#)RUPV
In this lab, you will add functionality to the loan project started in Lab 2. You will create a shell for the main
application form. From that shell, you will load the logon form, and then show the main form of the
application. You can continue to work with the files you created in Lab 2, or you can use the files provided
for you in the <install folder>\Labs\Lab03 folder.

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


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

Estimated time to complete this lab: 30 minutes

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

• Add a new form to an existing project.


• Change the startup form of a project.
• Load and display a form from another form.

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.

The solution for this lab is located in the <install folder>\Labs\Lab03\Solution folder.

3UHUHTXLVLWHV
There are no prerequisites for this lab.

([HUFLVHV
The following exercises provide practice working with the concepts covered in Chapter 3.

([HUFLVH#4=#$GGLQJ#WKH#0DLQ#)RUP
In this exercise, you will add a form, frmMain, to the project. This form will eventually become the main
form of the application. For now, frmMain will be used to load frmLogon.

([HUFLVH#5=#&ORVLQJ#)RUPV#DQG#(QGLQJ#DQ#$SSOLFDWLRQ
In this exercise, you will add code to close the logon form when OK or Cancel is clicked, and to end the
application when Done is clicked.

([HUFLVH#6=#$GGLQJ#D#0HVVDJH#%R[
In this exercise, you will add code to the main form that verifies that the user wants to exit the application.
You'll also set a property on the logon form to center it when it is shown.
Lab 3: Working with Forms

97 ([HUFLVH#4=#$GGLQJ#WKH#0DLQ#)RUP
In this exercise, you will add a form, frmMain, to the project. This form will eventually become the main
form of the application. For now, frmMain will be used to load frmLogon.

X Open the loan application project


• Open either the loan application project that you have been working on or the loan application project
located in the <install folder>\Labs\Lab03 folder.

X Add a new form to the project


… 1. On the Project menu, click Add Form.
… 2. In the Add Form dialog box, click Form, and then click Open.
A new form should be added to the project and displayed in the Visual Basic environment.
… 3. Set the following form properties:
Property Setting

(Name) frmMain
Caption Loan Payment
Estimate
BorderStyle 1 - Fixed Single
MinButton True

X Add controls to frmMain


… 1. Add a command button to frmMain that will close the form.
Your form should look similar to the following illustration:

… 2. Set the following properties for the command button:


Property Setting

(Name) cmdDone
Caption &Done
Cancel True

Notes

Page 16
Lab 3: Working with Forms

X Show the logon form


… 1. Double-click anywhere on the frmMain form.
The Code Editor window appears with the following code segment:
Private Sub Form_Load()

End Sub

… 2. Within this procedure, add a line of code to display the logon form modally.
To see an example of how your code should look, click this icon.
(CD-ROM displays a hint.)

X Change the startup form


… 1. On the Project menu, click LoanProject Properties.
… 2. In the Startup Object drop-down list box, select frmMain, and then click OK.

X Save and test the application


… 1. Save the project.
… 2. Run the application. Type a user name and password, and click OK. What happens? Why doesn’t the
logon form disappear?
To see the answers to the questions in steps 2, click this icon.
(CD-ROM displays a hint.)
… 3. Close the running application and return to Design mode.

Notes

Page 17
Lab 3: Working with Forms

97 ([HUFLVH#5=#&ORVLQJ#)RUPV#DQG#(QGLQJ#DQ#$SSOLFDWLRQ
In this exercise, you will add code to close the logon form when OK or Cancel is clicked, and to end the
application when the Done button of the Main form is clicked.

X Unload the logon form when the user clicks OK on frmLogon


• Add code to the Click event procedure of the cmdOK button on frmLogon to close the form after
displaying the message box:
Unload frmLogon

X End the application when the user clicks Cancel on frmLogon, or Done on frmMain
… 1. Add code to the Click event procedure of the cmdCancel button on frmLogon to end the application:
End

… 2. Add code to the Click event procedure of the cmdDone button on frmMain to unload the form:
Unload frmMain

… 3. Add code to the Unload event procedure of frmMain to end the application:
End

X Save and test the application


… 1. Save the project to the <install folder>\Labs\Lab03 folder.
… 2. Run the application. Type a user name and password. Click OK. What happens?
… 3. Now click Done. What happens?
To see answers to the questions in steps 2 and 3, click this icon.
(CD-ROM displays a hint.)
… 4. Close the running application and return to Design mode.

Notes

Page 18
Lab 3: Working with Forms

97 ([HUFLVH#6=#$GGLQJ#D#0HVVDJH#%R[
In this exercise, you will add code to the main form that verifies that the user wants to exit the application.
You'll also set a property on the logon form to center it when it is shown.

Instead of just ending the application in the Unload event of the main form, you can enable the user to
choose between closing the application or continuing to use it.

X Query the user before exiting the application


• Edit the code in the Form_Unload event procedure of frmMain, to look like the following:
Dim iAnswer As Integer

iAnswer = MsgBox ("Are you sure you're done?", vbYesNo)


If iAnswer = vbNo Then
Cancel = True
Else
End
End If

X Center the forms


… 1. Set the frmLogon and frmMain StartUpPosition properties to the center of the screen (2 –
CenterScreen).
… 2. Save and test the application.

Notes

Page 19
This page intentionally left blank.

Das könnte Ihnen auch gefallen