Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures

2 Previewing the Cerruti Company Application Programming with Microsoft Visual Basic 20122 Figure 7-1 Interface showing the payroll calculations

3 Previewing the Cerruti Company Application (cont.) Programming with Microsoft Visual Basic 20123 Figure 7-2 Message box containing a confirmation message

4 Lesson A Objectives After studying Lesson A, you should be able to: Create and call an independent Sub procedure 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 Programming with Microsoft Visual Basic 20124

5 Procedure –A block of program code that performs a specific task Two types of Sub procedures in Visual Basic: –Event procedure A procedure associated with a specific object and event –Independent Sub procedure Independent of any object and event Processed only when called (invoked) by a Call statement Programming with Microsoft Visual Basic 20125 Sub Procedures

6 Programming with Microsoft Visual Basic 20126 Sub Procedures (cont.) Figure 7-3 Syntax and examples of an independent Sub procedure and the Call statement

7 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 Programming with Microsoft Visual Basic 20127 Passing Variables Figure 7-4 Illustrations of passing by value and passing by reference

8 Programming with Microsoft Visual Basic 20128 Passing Variables (cont.) Passing Variables by Value Provides only the contents of the variable to the receiving procedure How to pass by value: –Include the keyword ByVal before the parameter Reasons to pass by value: –The procedure needs to know the contents of the variable –The procedure does not need to change the original value By default, Visual Basic passes by value

9 Programming with Microsoft Visual Basic 20129 Passing Variables (cont.) Figure 7-5 Favorite Title application’s interface Figure 7-6 Partially-coded btnDisplay_Click event procedure Passing Variables by Value (cont.)

10 Programming with Microsoft Visual Basic 201210 Passing Variables (cont.) Figure 7-8 Message shown in the interface Figure 7-7 DisplayMsg procedure Passing Variables by Value (cont.)

11 Programming with Microsoft Visual Basic 201211 Passing Variables (cont.) Figure 7-9 DisplayMsg procedure header and Call statement Passing Variables by Value (cont.)

12 Programming with Microsoft Visual Basic 201212 Passing Variables (cont.) Passing Variables by Reference Provides the address (memory location) of the variable to the procedure –The receiving procedure can thus access the variable Reason to pass by reference: –The procedure needs to change the variable’s contents How to pass by reference: –Include the keyword ByRef before the parameter

13 Programming with Microsoft Visual Basic 201213 Passing Variables (cont.) Figure 7-11 btnCalc_Click procedure from Chapter 6 Figure 7-10 Gross Pay application’s interface Passing Variables by Reference (cont.)

14 Programming with Microsoft Visual Basic 201214 Passing Variables (cont.) Figure 7-12 Call statement entered in the procedure Figure 7-14 Gross pay shown in the interface Figure 7-13 CalcGross procedure Passing Variables by Reference (cont.)

15 Programming with Microsoft Visual Basic 201215 Passing Variables (cont.) Figure 7-15 CalcGross and btnCalc_Click procedures Passing Variables by Reference (cont.)

16 Programming with Microsoft Visual Basic 201216 Passing Variables (cont.) Figure 7-16 Desk-check table before the Call statement is processed Figure 7-17 Desk-check table after the Call statement and CalcGross procedure header are processed Passing Variables by Reference (cont.)

17 Programming with Microsoft Visual Basic 201217 Passing Variables (cont.) Figure 7-18 Desk-check table after the statement in the CalcGross procedure is processed Passing Variables by Reference (cont.)

18 Function procedure –A block of code that performs a specific task –Returns a value after completing its task –Visual Basic provides built-in functions –You can also create your own functions As dataType in header indicates the return type of data –The Return statement is typically the last statement in a function The Return expression type must agree with the As dataType Programming with Microsoft Visual Basic 201218 Function Procedures

19 Programming with Microsoft Visual Basic 201219 Function Procedures (cont.) Figure 7-22 Syntax and examples of functions

20 Programming with Microsoft Visual Basic 201220 Function Procedures (cont.) Figure 7-23 Examples of invoking the GetNewPrice function

21 Programming with Microsoft Visual Basic 201221 Function Procedures (cont.) Figure 7-24 CalcGross function and btnCalc_Click procedure

22 Lesson A Summary There are two types of Sub procedures: –Event –Independent A function performs a task and returns a value Independent procedures and functions are called from an application’s code using the Call statement Passing by value sends a copy of the variable’s contents to a procedure or function Passing by reference sends a variable’s address to a procedure or function Programming with Microsoft Visual Basic 201222

23 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 number of items in the list portion of a combo box Sort the items in the list portion of a combo box Determine the item either selected or entered in a combo box Code a combo box’s TextChanged event procedure Programming with Microsoft Visual Basic 201223

24 Programming with Microsoft Visual Basic 201224 Including a Combo Box in an Interface Combo box –Allows the user to select from a number of choices –Allows the user to type an entry not on the list –Can save space on a form A list box only allows the user to select from a number of choices DropDownStyle property –Three values: Simple DropDown (default) DropDownList

25 Programming with Microsoft Visual Basic 201225 Including a Combo Box in an Interface (cont.) Figure 7-27 Examples of the combo box styles

26 Programming with Microsoft Visual Basic 201226 Including a Combo Box in an Interface (cont.) Figure 7-28 Code associated with the combo boxes in Figure 7-27

27 Programming with Microsoft Visual Basic 201227 Including a Combo Box in an Interface (cont.) Figure 7-29 Correct TabIndex values Figure 7-30 Gross pay amount shown in the interface

28 Programming with Microsoft Visual Basic 201228 Including a Combo Box in an Interface (cont.) Figure 7-31 Modified code for the Gross Pay application

29 Programming with Microsoft Visual Basic 201229 Lesson B Summary To add a combo box to a form: –Use the ComboBox tool in the toolbox To specify the style of a combo box: –Set the combo box’s DropDownStyle property To add items to a combo box: –Use the Items collection’s Add method –The method’s syntax is object.Items.Add(item) –In the syntax, object is the name of the combo box, and item is the text you want added to the list portion of the control

30 Programming with Microsoft Visual Basic 201230 Lesson B Summary (cont.) To automatically sort the items in the list portion of a combo box: –Set the combo box’s Sorted property to True To determine the number of items in the list portion of a combo box: –Use the Items collection’s Count property –Its syntax is object.Items.Count, in which object is the name of the combo box To select a combo box item from code: –Use any of the following properties: SelectedIndex, SelectedItem, or Text

31 Programming with Microsoft Visual Basic 201231 Lesson B Summary (cont.) To determine the item either selected in the list portion of a combo box or entered in the text portion: –Use the combo box’s Text property –However, if the combo box is a DropDownList style, you also can use the SelectedIndex or SelectedItem property To process code when the value in a combo box’s Text property changes: –Enter the code in the combo box’s TextChanged event procedure

32 Lesson C Objectives After studying Lesson C, you should be able to: Prevent a form from closing Round a number Programming with Microsoft Visual Basic 201232

33 Creating the Cerruti Company Application Programming with Microsoft Visual Basic 201233 Figure 7-33 TOE chart for the Cerruti Company application

34 Creating the Cerruti Company Application (cont.) Programming with Microsoft Visual Basic 201234 Figure 7-34 User interface for the Cerruti Company application

35 Coding the FormClosing Event Procedure FormClosing event –Occurs when a form is about to be closed because: The computer processes the Me.Close() statement The user clicks the Close button on the form’s title bar Requirements for the FormClosing event procedure: –Verifying that the user wants to close the application –Taking appropriate action based on the user’s response To prevent closing, set the Cancel property of the FormClosing procedure’s e parameter to True Programming with Microsoft Visual Basic 201235

36 Coding the FormClosing Event Procedure (cont.) Programming with Microsoft Visual Basic 201236 Figure 7-36 Message box displayed by the code in the FormClosing event procedure Figure 7-35 Pseudocode for the FormClosing event procedure

37 Coding the btnCalc_Click Procedure Programming with Microsoft Visual Basic 201237 Figure 7-37 Pseudocode for the btnCalc_Click procedure

38 Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201238 Figure 7-38 Listing of named constants and variables for the btnCalc_Click procedure

39 Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201239 Figure 7-39 Selection structure entered in the procedure Figure 7-40 Second selection structure entered in the procedure

40 Creating the GetFwt Function To calculate weekly taxable wages: –Multiply the number of withholding allowances by $73.08 (2012 withholding allowance value) –Subtract this result from the weekly gross pay To determine federal withholding tax (FWT): –Evaluate the weekly taxable wages and filing status –Use data to look up the FWT in special FWT tables The GetFwt function emulates the FWT table lookup Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201240

41 Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201241 Figure 7-41 Weekly FWT tables for the year 2012 Creating the GetFwt Function (cont.)

42 Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201242 Figure 7-42 Example of a FWT calculation Figure 7-43 Another example of a FWT calculation Creating the GetFwt Function (cont.)

43 Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201243 Figure 7-44 Pseudocode for the GetFwt function Figure 7-45 GetFwt function header and footer Creating the GetFwt Function (cont.)

44 Completing the btnCalc_Click Procedure Programming with Microsoft Visual Basic 201244 Rounding Numbers You must call the GetFwt function from the btnCalc’s Click event procedure Math.Round function –Rounds a value to a specific number of decimal places –Syntax: Math.Round (value[, digits]) value is the numeric expression to work on digits is the integer indicating the number of places to the right of the decimal point

45 Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201245 Figure 7-46 Syntax and examples of the Math.Round function Rounding Numbers (cont.)

46 Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201246 Figure 7-48 Payroll calculations using the first set of test data Figure 7-47 Data for testing the Cerruti Company’s application (continues) Rounding Numbers (cont.)

47 Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201247 Figure 7-47 Data for testing the Cerruti Company’s application Figure 7-49 Payroll calculations using the second set of test data (continued) Rounding Numbers (cont.)

48 Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 201248 Figure 7-50 Cerruti Company application’s code (continues) Rounding Numbers (cont.)

49 Programming with Microsoft Visual Basic 201249 Figure 7-50 Cerruti Company application’s code (continues) (continued) Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.)

50 Programming with Microsoft Visual Basic 201250 (continued) Figure 7-50 Cerruti Company application’s code (continues) Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.)

51 Programming with Microsoft Visual Basic 201251 Figure 7-50 Cerruti Company application’s code (continued) Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.)

52 Programming with Microsoft Visual Basic 201252 Lesson C Summary To process code when a form is about to be closed: –Enter the code in the form’s FormClosing event procedure –The FormClosing event occurs when the user clicks the Close button on a form’s title bar or when the computer processes the Me.Close() statement To prevent a form from being closed: –Set the Cancel property of the FormClosing event procedure’s e parameter to True, like this: e.Cancel = True

53 Programming with Microsoft Visual Basic 201253 Lesson C Summary (cont.) To round a number to a specific number of decimal places: –Use the Math.Round function The function’s syntax is Math.Round(value[, digits]) –value is a numeric expression –digits (which is optional) is an integer indicating how many places to the right of the decimal point are included in the rounding If the digits argument is omitted, the Math.Round function returns an integer


Download ppt "Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures."

Similar presentations


Ads by Google