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

295

/DE#44=#$GGLQJ#0HQXV
In this lab, you will add functionality to the loan project started in Lab 2. You will add a menu and a status
bar to the main form of the loan application. In an optional exercise, you can add a toolbar.

You can continue to work with the files you have already created, or you can use the files that have been
provided in the folder <install folder>\Labs\Lab11.

Estimated time to complete this lab: 60 minutes

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


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

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

• Add a menu to a form.


• Add a status bar to a form.
• Add a toolbar to a 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 folder <install folder>\Labs\Lab11\Solution.

3UHUHTXLVLWHV
Before working on this lab, you should be familiar with the following:

• Adding new procedures.

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

([HUFLVH#4=#$GGLQJ#D#0HQX
In this exercise, you will delete the command buttons on frmMain and call their functions from menu items
instead.

([HUFLVH#5=#$GGLQJ#D#6WDWXV#%DU
In this exercise, you will add a status bar to frmMain and post messages on the status bar instead of using
message boxes.

([HUFLVH#6#+2SWLRQDO,=#$GGLQJ#D#7RROEDU
In this exercise, you will add a toolbar with two buttons to frmMain.
Lab 11: Adding Menus

295 ([HUFLVH#4=#$GGLQJ#D#0HQX
In this exercise, you will delete the command buttons on frmMain and call their functions from menu items
instead.

Like command buttons, a menu provides the user with a way to invoke actions in an application.

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\Lab011 folder.

X Create a menu on frmMain


… 1. With frmMain open, on the Tools menu, click Menu Editor.
… 2. Add the following menu items:
Caption Name Shortcut

&File mnuFile none


....E&xit mnuFileExit none
&Loan mnuLoan none
....&Monthly Payment mnuLoanMonthly CTRL+M
....&Total Paid mnuLoanTotal CTRL+T

… 3. After adding the menu items, click OK.

X Create new Sub procedures


… 1. Open the Code Editor window for frmMain.
… 2. Insert two new private Sub procedures: ShowMonthly, and ShowTotal.
… 3. Copy the code from the cmdMonthly_Click event procedure to the new ShowMonthly Sub procedure.
… 4. Copy the code from the cmdTotal_Click event procedure to the new ShowTotal Sub procedure.

X Add code to the menu item Click events


… 1. Add code to the Click event procedure of the Exit menu item, mnuFileExit, which unloads frmMain.
… 2. Add code to the Click event procedure of the Monthly Payment menu item, mnuLoanMonthly, which
calls the new ShowMonthly procedure.
… 3. Add code to the Click event procedure of the Total Paid menu item, mnuLoanTotal, which calls the
new ShowTotal procedure.

Notes

Page 70
Lab 11: Adding Menus

X Save and test your application


… 1. Select each menu item.
… 2. Test the access keys.
… 3. Test the shortcut keys: CTRL+M, CTRL+T.

X Delete the command buttons


Now that there are menu items to invoke the functions previously called by the command buttons, you can
delete the command buttons and their Click event procedures.
… 1. Delete the Monthly Payment, Total Paid, and Done command buttons on frmMain.
The Click event procedures for the command buttons will move to the General Declarations section of
the Code Editor window for the form.
… 2. Delete the Click event procedures cmdMonthly_Click, cmdTotal_Click, and cmdDone_Click.

Notes

Page 71
Lab 11: Adding Menus

295 ([HUFLVH#5=#$GGLQJ#D#6WDWXV#%DU
In this exercise, you will add a status bar to frmMain and post messages on the status bar instead of using
message boxes.

Like message boxes, a status bar provides feedback to the user.

X Add Microsoft Windows Common Controls to the project


… 1. On the Project menu, click Components.
… 2. From the list of available controls, select Microsoft Windows Common Controls 6.0, and then click
OK.
Notice the new controls are added to the Visual Basic Toolbox.

Note If Microsoft Windows Common Controls does not appear in the list of available controls,
ensure that the Selected Items Only option is not selected and that the Controls tab is selected in the
Components dialog box.

X Create a status bar


… 1. From the Toolbox, add a StatusBar control to the bottom of frmMain.
… 2. Set the Name property of the status bar to sbrLoan.
… 3. Open the Status Bar control's Property Pages dialog box by selecting the Custom property of the
status bar in the Properties window.
… 4. Select the Panels tab, and click the Insert Panel button to insert a second panel.
… 5. Set properties for the two panels as follows:
Panel index Property Desired setting

1 Key msg
Style 0 - sbrText
Autosize 1 - sbrSpring
2 Key time
Alignment 1 - sbrCenter
Style 5 - sbrTime
Autosize 2 - sbrContents

Note The Panel's Key property is case sensitive.

… 6. Click Apply to observe the effects of the Minimum Width setting.


… 7. Click OK when the width is set to your satisfaction.

Notes

Page 72
Lab 11: Adding Menus

X Post messages to the status bar


… 1. In the ShowMonthly procedure:
a. Replace the error message box with a message in the status bar:
sbrLoan.Panels("msg").Text = "Numeric value required."

b. Replace the message box showing the results of the MonthlyPayment function with a message in
the status bar:
sbrLoan.Panels("msg").Text = "Monthly payment: " & _
Format(dblMonthly, "currency")

… 2. In the ShowTotal procedure, replace each message box with a message in the status bar.
… 3. Save and test your work.

Notes

Page 73
Lab 11: Adding Menus

295 ([HUFLVH#6#+2SWLRQDO,=#$GGLQJ#D#7RROEDU
In this exercise, you will add a toolbar with two buttons to frmMain.

X Add an image list to frmMain


… 1. From the Toolbox, add an ImageList control to frmMain.

Note The image list is invisible when the application runs, so don’t worry about its placement.

… 2. In the Visual Basic Properties window, set the image list’s Name property to imlLoan.
… 3. Open the Image List control's Property Pages dialog box by selecting the Custom property of the
status bar in the Properties window.
… 4. Click the 16 x 16 option button on the General tab.
… 5. Move to the Images tab and insert two pictures that have been provided on this CD-ROM in the
following folders:
<install folder>\Labs\Lab11\exit.bmp
<install folder>\Labs\Lab11\month.bmp
… 6. After inserting the pictures, click OK.

Note The image on a toolbar button should be 16 x 16 pixels. You can use the Visual Basic Image Editor
to create these images. In a complete installation of Visual Basic, the Image Editor, Imagedit, is found in
the COMMON\TOOLS\VB\IMAGEDIT folder of the Microsoft Visual Studio® 98 CD-ROM.

Notes

Page 74
Lab 11: Adding Menus

X Add a toolbar with two toolbar buttons to frmMain


… 1. From the Toolbox, add a Toolbar control to the top of frmMain.
… 2. In the Visual Basic Properties window, set the toolbar’s Name property to tlbLoan.
… 3. Open the Toolbar control's Property Pages dialog box by selecting the Custom property of the status
bar in the Properties window.
… 4. On the General tab:
a. Set the ImageList property to the image list you just created, imlLoan.
b. Set the Style property to 1-tbrFlat
… 5. On the Buttons tab, insert two buttons with the following properties.
Button index Property Desired setting

1 Description Exit button


Key exit
ToolTip Text Exit
Image 1
2 Description Monthly Payment
button
Key month
ToolTip Text Monthly Payment
Image 2

… 6. After inserting the buttons, click OK.

X Respond to the toolbar ButtonClick event


All buttons on a toolbar share the same ButtonClick event procedure. Therefore, you must determine
which button invoked the event and call the appropriate function.
… 1. Open the Code Editor window for the ButtonClick event of the tlbLoan toolbar.
… 2. Add code that determines which button was selected and executes the appropriate procedure:
Select Case Button.Key
Case "month"
ShowMonthly
Case "exit"
Unload frmMain
End Select

… 3. Save and test your work.

Notes

Page 75
This page intentionally left blank.

You might also like