You are on page 1of 6

55

/DE#5=#&UHDWLQJ#D#9LVXDO#%DVLF#$SSOLFDWLRQ
In Labs 2, 3, 4, 5, 7, 9, 10, 11 and 14, you will create a loan payment estimate application. You will create
the full application in stages, each stage building on the code created in the previous lab. At the beginning
of each lab, you can either continue with your own files, or start with the files provided for you.

In this lab, you will begin the loan payment estimate application by creating a logon screen.

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


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

Estimated time to complete this lab: 30 minutes

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

• Create a simple application in Visual Basic.


• Create an event procedure.
• Retrieve object properties at run time.

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\Lab02\Solution folder.

3UHUHTXLVLWHV
There are no prerequisites for this lab.

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

([HUFLVH#4=#&UHDWLQJ#D#/RJRQ#6FUHHQ
In this exercise, you will create a logon screen for the loan payment estimate application. You will start a
new project, rename the form, place controls on the form, and set properties for the controls.

([HUFLVH#5=#$GGLQJ#&RGH#WR#(QDEOH#DQG#'LVDEOH#D#%XWWRQ
In this exercise, you will add code that will enable the OK button only when both a user name and a
password have been entered.

([HUFLVH#6#+2SWLRQDO,=#$GGLQJ#DQ#,FRQ
In this exercise, you will add an icon to a form using the Image control.
Lab 2: Creating a Visual Basic Application

55 ([HUFLVH#4=#&UHDWLQJ#D#/RJRQ#6FUHHQ
In this exercise, you will create a logon screen for the loan payment estimate application. You will start a
new project, rename the form, place controls on the form, and set properties for the controls.

X Start a new project


… 1. Start Visual Basic from the Start menu (if Visual Basic isn’t already running). Otherwise, on the File
menu, click New Project to start Visual Basic.
… 2. Choose Standard EXE, and then click OK.

X Add controls to the default form


… 1. Add three Label controls to the form.
… 2. Add two TextBox controls to the form.
… 3. Add two CommandButton controls to the form.
… 4. Move and size the controls so they look like the following illustration.

X Set properties for the controls


… 1. Select any control on the form.
… 2. In the Properties window, select the property for the control as listed in the following table.
… 3. Enter the new value for the property as specified in the table.
Current name Property New value

Label1 Name lblInstruction


Caption Type your name and password to log on.
Label2 Name lblUserName
Caption &User Name
Label3 Name lblPassword
Caption &Password
Text1 Name txtUserName
Text <blank>
Text2 Name txtPassword
Text <blank>
PasswordChar *

Notes

Page 10
Lab 2: Creating a Visual Basic Application

Current name Property New value

Command1 Name cmdOK


Caption OK
Default True
Command2 Name cmdCancel
Caption Cancel
Cancel True
Form1 Name frmLogon
Caption Enter Program Password
BorderStyle 1 - Fixed Single

Note At the top of the Properties window is a drop-down list box of all of the controls on the current
form. It is often easier to use the drop-down list to switch between controls when setting properties on
many different controls.

The resulting form should look something like the following illustration.

X Add code to the cmdOK Click event


… 1. Double-click the cmdOK command button. This opens the Code Editor window with the following code
already inserted:
Private Sub cmdOK_Click()

End Sub

… 2. Add code to the Click event procedure to display a message box with the current values of the User
Name and Password text boxes:
MsgBox "User Name = " & txtUserName.Text & _
", Password = " & txtPassword.Text

Note The space and underscore in the code above is used to break a single statement across multiple
lines. The ampersand is used to force the combining of two expressions.

… 3. Run the application. Type in a user name and a password, and then click OK. What happens?

Notes

Page 11
Lab 2: Creating a Visual Basic Application

… 4. Type in a different user name and password, and then press ENTER. What happens? Why?
… 5. Close the running application and return to Design mode.
To see answers to the above questions, click this icon.
(CD-ROM displays a hint.)

X Add code to the cmdCancel Click event


… 1. Double-click the cmdCancel command button. This opens the Code Editor window with the following
code already inserted:
Private Sub cmdCancel_Click()

End Sub

… 2. Add code to the Click event procedure to display a message box:


MsgBox "This is the Cancel button."

… 3. Run the application. Type in a user name and a password, and then click Cancel. What happens?
… 4. Type in a different user name and password, and then press ESC. What happens? Why?
… 5. Close the running application and return to Design mode.
To see answers to the above questions, click this icon.
(CD-ROM displays a hint.)

X Name the project


… 1. On the Project menu, click Project1 Properties.
… 2. In Project Name, type LoanProject, and click OK.

X Save the project


… 1. On the File menu, click Save Project.
… 2. When prompted to save the form, change the destination folder to<install folder>\Labs\Lab02, type
frmLogon for the file name, and click Save.
… 3. When prompted to save the project, change the destination folder to <install folder>\Labs\Lab02, type
Loan for the file name, and click Save.

Notes

Page 12
Lab 2: Creating a Visual Basic Application

55 ([HUFLVH#5=#$GGLQJ#&RGH#WR#(QDEOH#DQG#'LVDEOH#D#%XWWRQ
In this exercise, you will add code that will enable the OK button only when both a user name and a
password have been entered.

When you write an application, you should minimize the possibility of users making mistakes. One way to
do this is to disable controls when selecting them would cause an error. For example, users shouldn’t be
able to click the OK button on the logon form until they enter a user name and a password.

X Enable the OK button


… 1. In the Properties window, set the Enabled property of the cmdOK command button to False.
… 2. Double-click the txtUserName text box.
The Code Editor window opens to the txtUserName_Change event procedure.
… 3. Add the following lines of code to the txtUserName Change event:
If txtUserName.Text <> "" And txtPassword.Text <> "" Then
cmdOK.Enabled = True
Else
cmdOK.Enabled = False
End If

Note There should be no spaces between quotes. By placing a space between the quotes, the
condition in the If statement checks for the space character in the Textbox and not a zero length string.

… 4. Copy the code from the txtUserName_Change event to the Change event of the txtPassword text
box.
… 5. Save and test your application. Is the OK button enabled?
… 6. Type a user name. Is the OK button enabled? Why?
… 7. Now, type a password. Is the OK button enabled? Why?
To see answers to the questions in steps 5 through 7, click this icon.
(CD-ROM displays a hint.)

Notes

Page 13
Lab 2: Creating a Visual Basic Application

55 ([HUFLVH#6#+2SWLRQDO,=#$GGLQJ#DQ#,FRQ
In this exercise, you will add an icon to a form using the Image control.

X Add a graphic to the logon form


… 1. Add an Image control to the upper-left side of the form.
… 2. Set the Name property of the Image control to imgLogo.
… 3. Set the BorderStyle property of the Image control to 1 - Fixed Single.
… 4. Set the Picture property of the Image control to an icon by clicking the button (...) in the Properties
window. The Load Picture dialog box will appear.
… 5. Select the Pc04.ico icon from the <install folder>\Labs\Lab02\Solution folder and click Open.
… 6. Use the LoadPicture function to change the Image control's Picture property in the Image control's
Click event.
imgLogo.Picture = LoadPicture("\Path\filename")

Use the Face03.ico file in the <install folder>\Labs\Lab02\Solution folder as the new filename.
… 7. Save and test your application. Which picture is displayed when the application starts?
… 8. Click the image. What happens?
To see answers to the questions in steps 7 and 8, click this icon.
(CD-ROM displays a hint.)

Notes

Page 14

You might also like