Programming with Microsoft Visual Basic 2010 5th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES Previewing the Harvey Industries Application Open the Harvey Industries.exe file The Harvey Industries application calculates payroll for an employee 2 Programming with Microsoft Visual Basic 2010, 5th Edition Previewing the Harvey Industries Application (cont’d.) Figure 7-1 Interface showing the payroll calculations 3 Programming with Microsoft Visual Basic 2010, 5th Edition Lesson A Objectives After 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 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 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 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 Programming with Microsoft Visual Basic 2010, 5th Edition Example: Event and Sub Procedure Programming with Microsoft Visual Basic 2010, 5th Edition Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure 9 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 1 0 Programming with Microsoft Visual Basic 2010, 5th Edition Example (Using Sub Procedures) Programming with Microsoft Visual Basic 2010, 5th Edition Figure 7-8 CalcGrossPay procedure and btnCalc control’s Click event procedure 12 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 Programming with Microsoft Visual Basic 2010, 5th Edition Figure 7-14 Function procedure syntax, examples, and steps 14 Programming with Microsoft Visual Basic 2010, 5th Edition Function Procedures (cont’d.) Figure 7-15 Examples of invoking the GetNewPrice function 15 Programming with Microsoft Visual Basic 2010, 5th Edition Example: Using Functions Programming with Microsoft Visual Basic 2010, 5th Edition Figure 7-16 CalcGrossPay function and btnCalc control’s Click event procedure 17Programming 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 Programming with Microsoft Visual Basic 2010, 5th Edition Lesson B Objectives After 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 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 Programming with Microsoft Visual Basic 2010, 5th Edition Including a Combo Box in an Interface (cont’d.) Figure 7-19 Examples of the combo box styles 21 Programming with Microsoft Visual Basic 2010, 5th Edition Figure 7-20 Code associated with the combo boxes in Figure 7-19 22 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 Programming with Microsoft Visual Basic 2010, 5th Edition Including a Combo Box in an Interface (cont’d.) Figure 7-22 Gross pay amount shown in the interface 24 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 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 Programming with Microsoft Visual Basic 2010, 5th Edition Lesson C Objectives After studying Lesson C, you should be able to: Prevent a form from closing Round a number 27 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 Programming with Microsoft Visual Basic 2010, 5th Edition Coding the Harvey Industries Application (cont’d.) Figure 7-26 User interface for the Harvey Industries application 29 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 Programming with Microsoft Visual Basic 2010, 5th Edition Coding the FormClosing Event Procedure (cont’d.) Figure 7-27 Pseudocode for the FormClosing event procedure 31 Programming with Microsoft Visual Basic 2010, 5th Edition Coding the FormClosing Event Procedure (cont’d.) Figure 7-28 Message box displayed by the code in the FormClosing event procedure 32 Programming with Microsoft Visual Basic 2010, 5th Edition Coding the btnCalc Control’s Click Event Procedure Figure 7-29 Pseudocode for the btnCalc control’s Click event procedure 33 Programming with Microsoft Visual Basic 2010, 5th Edition Coding the btnCalc Control’s Click Event Procedure (cont’d.) Figure 7-30 Selection structure entered in the procedure 34 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 Programming with Microsoft Visual Basic 2010, 5th Edition Creating the GetFwt Function (cont’d.) Figure 7-33 FWT calculations for a married taxpayer whose weekly taxable wages are $388.46 36 Programming with Microsoft Visual Basic 2010, 5th Edition Creating the GetFwt Function (cont’d.) Figure 7-34 FWT calculations for a single taxpayer whose weekly taxable wages are $600 37 Programming with Microsoft Visual Basic 2010, 5th Edition Creating the GetFwt Function (cont’d.) Figure 7-35 Pseudocode for the GetFwt function 38 Programming with Microsoft Visual Basic 2010, 5th Edition Creating the GetFwt Function (cont’d.) Figure 7-36 GetFwt function header and footer 39 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 Programming with Microsoft Visual Basic 2010, 5th Edition Completing the btnCalc Control’s Click Event Procedure (cont’d.) Figure 7-37 Payroll calculations displayed in the interface 41 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 Programming with Microsoft Visual Basic 2010, 5th Edition