Programming with Microsoft Visual Basic 2010 5 th Edition
-
Upload
rooney-carpenter -
Category
Documents
-
view
24 -
download
3
description
Transcript of Programming with Microsoft Visual Basic 2010 5 th Edition
![Page 1: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/1.jpg)
Programming with Microsoft Visual Basic 2010 5th Edition
CHAPTER SEVEN
SUB AND FUNCTION PROCEDURES
![Page 2: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/2.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Previewing the Harvey Industries Application
Open the Harvey Industries.exe file
The Harvey Industries application calculates payroll for an employee
2
![Page 3: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/3.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Previewing the Harvey Industries Application (cont’d.)
3
Figure 7-1 Interface showing the payroll calculations
![Page 4: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/4.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A ObjectivesAfter studying Lesson A, you should be able to:
Explain the difference between a Sub procedure and a Function procedure
Create a procedure that receives information passed to it
Explain the difference between passing data by value and passing data by reference
Create a Function procedure
4
![Page 5: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/5.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
More About Sub Procedures
Procedure◦ Block of program code that performs specific task
Two types of Sub procedures in Visual Basic◦ Event procedure
◦ Procedure associated with specific object and event
◦ Independent Sub procedure◦ Independent of any object and event◦ Processed only when called (invoked)
5
![Page 6: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/6.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Passing Variables Variables
◦ Have both value and unique address
Passing by value◦ Passes a copy of the variable’s value◦ Example: Disclosing your bank account balance
Passing by reference◦ Passes a variable’s address◦ Example: Disclosing your bank account number
6
![Page 7: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/7.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Passing Variables by Value
Provides only contents of variable to receiving procedure
How to pass by value:◦ Include keyword ByVal before parameter
Reasons to pass by value:◦ Procedure needs to know contents of variable◦ Procedure does not need to change original value
By default, Visual Basic passes by value
7
![Page 8: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/8.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Example: Event and Sub Procedure
![Page 9: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/9.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure9
![Page 10: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/10.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Passing Variables by Reference
Provides address (memory location) of variable to procedure◦ Receiving procedure can thus access variable
Reason to pass by reference◦ Procedure needs to change variable’s contents
How to pass by reference◦ Include keyword ByRef before parameter
10
![Page 11: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/11.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Example (Using Sub Procedures)
![Page 12: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/12.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 7-8 CalcGrossPay procedure and btnCalc control’s Click event procedure
12
![Page 13: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/13.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Function Procedures Function procedure
◦ Block of code that performs specific task◦ Returns value after completing its task
Visual Basic provides built-in functions
Can also create your own functions◦ As datatype in header indicates return type of data◦ Return expression type must agree with As datatype
13
![Page 14: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/14.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition14
Figure 7-14 Function procedure syntax, examples, and steps
![Page 15: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/15.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Function Procedures (cont’d.)
15
Figure 7-15 Examples of invoking the GetNewPrice function
![Page 16: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/16.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Example: Using Functions
![Page 17: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/17.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition17
Figure 7-16 CalcGrossPay function and btnCalc control’s Click event procedure
![Page 18: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/18.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Summary Two types of Sub procedures
◦ Event and independent
Function: Performs task and returns value
Independent procedures and functions◦ Called from application’s code using Call statement
Pass by value◦ Send copy of variable’s contents to procedure or function
Pass by reference◦ Send variable’s address to procedure or function
18
![Page 19: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/19.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B ObjectivesAfter studying Lesson B, you should be able to:
Include a combo box in an interface
Add items to a combo box
Select a combo box item from code
Determine the item either selected or entered in a combo box
Code a combo box’s TextChanged event procedure
19
![Page 20: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/20.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Including a Combo Box in an Interface
Combo box◦ Allows user to select from number of choices◦ Allows user to type entry not on list◦ Can save space on form
List box does not share features two and three
DropDownStyle property◦ Values: Simple, DropDown (default), DropDownList
20
![Page 21: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/21.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Including a Combo Box in an Interface (cont’d.)
21
Figure 7-19 Examples of the combo box styles
![Page 22: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/22.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition22
Figure 7-20 Code associated with the combo boxes in Figure 7-19
![Page 23: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/23.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Including a Combo Box in an Interface (cont’d.)
To process code when Text property changes◦ Use TextChanged event procedure
Use Item collection’s Add method to add item
Other properties of combo box◦ Sorted: Sorts items in dictionary order◦ SelectedIndex: Used to select item in list portion◦ SelectedItem: Contains value of item selected◦ Text: Contains value that appears in text portion
Use label control to provide keyboard access to the combo box
23
![Page 24: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/24.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Including a Combo Box in an Interface (cont’d.)
24
Figure 7-22 Gross pay amount shown in the interface
![Page 25: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/25.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B Summary Use ComboBox tool to add combo box to form
Specify style of combo box using DropDownStyle property
Use Items collection’s Add method to add items to Combo box
Use combo box’s Sorted property to sort items in combo’s list
Enter code in combo box’s TextChanged event procedure◦ To process code when Text property changes
25
![Page 26: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/26.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B Summary (cont’d.)
Use SelectedIndex, SelectedItem, or Text property:◦ To select combo box item from code◦ To determine item that was selected
26
![Page 27: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/27.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson C ObjectivesAfter studying Lesson C, you should be able to:
Prevent a form from closing
Round a number
27
![Page 28: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/28.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Creating the Harvey Industries Application
Application objective is to calculate:◦ Employee’s weekly gross pay◦ Federal withholding tax (FWT)◦ Social Security and Medicare (FICA) tax◦ Net pay
Review TOE chart for requirements
28
![Page 29: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/29.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 7-26 User interface for the Harvey Industries application
Coding the Harvey Industries Application (cont’d.)
29
![Page 30: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/30.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the FormClosing Event Procedure
FormClosing event◦ Occurs when form is about to be closed because:
◦ Computer processes Me.Close() statement ◦ User clicks Close button on form’s title bar
Requirement for FormClosing event procedure◦ Verifying that user wants to close application◦ Taking appropriate action based on user’s response
To prevent closing◦ Set Cancel property of FormClosing procedure’s e parameter to true
30
![Page 31: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/31.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the FormClosing Event Procedure (cont’d.)
31
Figure 7-27 Pseudocode for the FormClosing event procedure
![Page 32: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/32.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the FormClosing Event Procedure (cont’d.)
32
Figure 7-28 Message box displayed by the code in the FormClosing event procedure
![Page 33: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/33.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the btnCalc Control’s Click Event Procedure
33
Figure 7-29 Pseudocode for
the btnCalc control’s Click
event procedure
![Page 34: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/34.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the btnCalc Control’s Click Event Procedure (cont’d.)
34
Figure 7-30 Selection structure entered in the procedure
![Page 35: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/35.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Creating the GetFwt Function
How to calculate weekly taxable wages◦ Multiply number of withholding allowances by $70.19◦ Subtract this result from weekly gross pay
Determining federal withholding tax (FWT)◦ Evaluate weekly taxable wages and filing status◦ Use data to look up FWT in special FWT tables
GetFwt function emulates FWT table lookup
35
![Page 36: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/36.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Creating the GetFwt Function (cont’d.)
36
Figure 7-33 FWT calculations for a married taxpayer whose weekly taxable wages are $388.46
![Page 37: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/37.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Creating the GetFwt Function (cont’d.)
37
Figure 7-34 FWT calculations for a single taxpayer whose weekly taxable wages are $600
![Page 38: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/38.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Creating the GetFwt Function (cont’d.)
38
Figure 7-35 Pseudocode for the GetFwt function
![Page 39: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/39.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Creating the GetFwt Function (cont’d.)
39
Figure 7-36 GetFwt function header and footer
![Page 40: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/40.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Completing the btnCalc Control’s Click Event Procedure
Must call GetFwt function from btnCalc’s Click event procedure
Math.Round function◦ Round value to specific number of decimal places◦ Syntax: Math.Round (value[, digits])◦ value: Numeric expression to work on◦ digits: Integer indicating number of places to right of decimal point
40
![Page 41: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/41.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Completing the btnCalc Control’s Click Event Procedure (cont’d.)
41
Figure 7-37 Payroll calculations displayed in the interface
![Page 42: Programming with Microsoft Visual Basic 2010 5 th Edition](https://reader035.fdocuments.us/reader035/viewer/2022062721/5681360f550346895d9d8432/html5/thumbnails/42.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson C Summary Use form’s FormClosing event procedure to process code when form is about to be closed
Set Cancel property of FormClosing event procedure’s e parameter to true to prevent form from being closed
Use Math.Round function to round number to specific number of decimal places
42