Programming with Microsoft Visual Basic 2017 Chapter 6

Programming with Microsoft Visual Basic 2017 Chapter 6 Sub and Function Procedures

FOCUS ON THE CONCEPTS LESSON Concepts covered in this lesson: • Event-handling Sub procedures • Independent Sub procedures • Passing information to a procedure • Rounding numbers • Function procedures 2

Event-Handling Sub Procedures (1 of 2) • Procedures that are processed only when a specific event occurs are called event-handling Sub procedures or, more simply, event procedures. • The Handles clause in an event procedure’s header indicates the object and event associated with the procedure. • An event procedure’s default name is composed of the object’s name followed by an underscore and the event’s name. 3

Event-Handling Sub Procedures (2 of 2) • • Unlike variable names, however, procedure names are usually entered using Pascal case, which means you capitalize the first letter in the name and the first letter of each subsequent word in the name. To associate multiple objects and events with a procedure, you list each object and event in the procedure’s Handles clause. 4

Independent Sub Procedures (1 of 2) • • Sub procedures that are not connected to any object and event are often referred to as independent Sub procedures. An independent Sub procedure is processed only when a statement in your code calls (or invokes) it. The arguments represent information that must be passed to the Sub procedure in order for the procedure to perform its task. An argument can be a literal, a named constant, a keyword, or a variable; however, in most cases, the argument will be a variable in the calling procedure. 5

Independent Sub Procedures (2 of 2) • An independent Sub procedure can be entered anywhere between the Public Class and End Class clauses in the Code Editor window, but it must be entered outside of any other procedure. • A parameter is simply a memory location; more specifically, it is a variable in the called procedure. 6

Passing Information to a Procedure • • An argument can be a literal, a named constant, a keyword, or a variable; however, in most cases, it will be a variable. Visual Basic allows you to pass either a copy of the variable’s value or the variable’s address to the receiving procedure (called procedure). Passing a copy of a variable’s value is referred to as passing by value. Passing the variable’s address is referred to as passing by reference. 7

Passing Variables by Value • • To pass a variable by value, you include the keyword By. Val before the name of its corresponding parameter in the receiving procedure’s parameter. List. When you pass a variable by value, the computer passes a copy of the variable’s contents to the receiving procedure (called procedure). 8

Passing Variables by Reference • • To pass a variable by reference in Visual Basic, you include the keyword By. Ref before the name of the corresponding parameter in the receiving procedure’s parameter. List. The By. Ref keyword tells the calling statement to pass the variable’s address rather than a copy of its contents. 9

Rounding Numbers • Use Visual Basic’s Math. Round method to round a value. • Syntax : Math. Round(value, digits) – In the syntax, value is a numeric expression and – 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 method returns an integer. 10

Function Procedures • • • Function procedure returns a value after performing its assigned task and are referred to more simply as functions. Unlike a Sub procedure, a function’s header and footer contain the Function keyword rather than the Sub keyword. A function’s header also includes the As data. Type section, which specifies the data type of the value the function will return. Example 11

Example of Function Procedures 12

APPLY THE CONCEPTS LESSON (1 of 2) After studying this lesson, you should be able to: • • • Add a combo box to the form Add items to a combo box and select a default item Code a combo box’s Key. Press event procedure Create an event-handling Sub procedure Calculate federal withholding tax Invoke an independent Sub procedure and a function Create an independent Sub procedure and a function Validate an application’s code Professionalize your application’s interface 13

Add a Combo Box to the Form (1 of 2) Combo Box • used in the place of list boxes • offers a list of choices to select • the list of choices can be hidden – allowing the user to save space on the form • • contains a text field, which may or may not be editable by the user three styles of combo boxes are available in Visual Basic. 14

Add a Combo Box to the Form (2 of 2) The style is controlled by the combo box’s Drop. Down. Style property, which can be set to Simple, Drop. Down (the default), or Drop. Down. List. Each style of combo box contains a text portion and a list portion. 15

Add Items to a Combo Box and Select a Default Item To code and test the Cerruti Company application: • Open the Cerruti Solution. sln file contained in the Chap 06Cerruti Solution folder. • Open the Code Editor window. • Locate the frm. Main_Load procedure. • Click the blank line above the End Sub – Enter the For…Next loop statement to add numbers from 0 to 10 to the cbo. Allowances control. – Enter an assignment statement to select a default item in the cbo. Allowances control. 16

Cerruti Company application 17

Add Items to a Combo Box and Select a Default Item 18

Code a Combo Box’s Key. Press Event Procedure (1 of 2) To code the combo box’s Key. Press procedure: • Open the code template for the cbo. Allowances_Key. Press procedure and enter the comment and code. – The cbo. Allowances control is a Drop. Down combo box, which means its text portion is editable. Drop. Down. Style property • Save the solution and then start the application. • Click the text portion of the Allowances box and then verify that the text portion will accept only numbers and the Backspace key. • Click the Exit button. 19

A-3 Code a Combo Box’s Key. Press Event Procedure (2 of 2) 20

Create an Event-Handling Sub Procedure To create an event-handling Sub procedure: • Open the code template for the cbo. Allowances_Text. Changed procedure. – Replace procedure name cbo. Allowances_Text. Changed with Clear. Output. – Modify the Handles clause and also, enter the four assignment statements. • Save the solution. 21

Create an Event-Handling Sub Procedure GUI 22

Calculate Federal Withholding Tax (1 of 5) • The amount of FWT (聯邦預扣稅) to deduct from an employee’s weekly gross pay is based on his or her weekly taxable wages and filing status, which is either – Single or – Married. 23

Calculate Federal Withholding Tax (2 of 5) Example 24

Calculate Federal Withholding Tax (3 of 5) Example 1 Single with weekly taxable wages of $344. 20 Taxable wages $ 344. 20 Of excess over - 224. 00 120. 20 Percentage * 0. 15 18. 03 Base amount + 18. 00 Tax $ 36. 03 25

Calculate Federal Withholding Tax (4 of 5) Example 26

Calculate Federal Withholding Tax (5 of 5) Example 2 Married with weekly taxable wages of $1, 700. 00 Taxable wages $ 1, 700. 00 Of excess over - 1, 626. 00 74. 00 Percentage * 0. 25 18. 50 Base amount + 201. 02 Tax $ 219. 55 27

Create an Independent Sub Procedure to Calculate FWT for Single Persons This procedure needs two items of information from its calling statement in the btn. Calc_Click procedure: • the employee’s taxable wages and • the address of a variable where the calculated tax can be stored. 28

Create an Independent Sub Procedure 29

Create a Function to Calculate FWT for Married Persons • This procedure needs only one item of information from the statement that invokes it in btn. Calc_Click procedure: – the employee’s taxable wages. • The function will return the calculated FWT to that statement, which will assign the returned value to the dbl. Fwt variable. 30

Create a Function 31

Complete the Application and Invoke an Independent Sub Procedure and a Function (1/3) • Complete the btn. Calc_Click procedure – Declare the variables (已完成) 32

Complete the Application and Invoke an Independent Sub Procedure and a Function (1/3) • Complete the btn. Calc_Click procedure – Convert the data types of user inputs (已完成) 33

Complete the Application and Invoke an Independent Sub Procedure and a Function (1/3) • Complete the btn. Calc_Click procedure – Calculate the gross pay (已完成) – Calculate the taxable wages (已完成) 34

Complete the Application and Invoke an Independent Sub Procedure and a Function • Complete the btn. Calc_Click procedure – Determine the FWT (未完成) 35

Complete the Application and Invoke an Independent Sub Procedure and a Function • Complete the btn. Calc_Click procedure – Calculate FICA tax (已完成) – Calculate net pay by Rounding statement (已完成) 36

Complete the Application and Invoke an Independent Sub Procedure and a Function • Complete the btn. Calc_Click procedure – Display calculated amounts (已完成) 37

Validate an Application’s Code GUI 38

Professionalize Your Application’s Interface • At times, an application may need to communicate with the user during run time. • You can accomplish this task by using Visual Basic’s Message. Box. Show method to display a message box. – The message box contains text, one or more buttons, and an icon. 39

Professionalize Your Application’s Interface 40

Summary (1 of 6) • Creating an independent Sub procedure • You call (invoke) an independent Sub procedure using a stand-alone statement, referred to as the calling statement. • You pass information to a Sub or Function procedure by including the information in the calling statement’s argument. List. • In the parameter. List in the receiving procedure’s header, you include the names of variables that will store the information passed to the procedure. • The number, data type, and order (position) of the arguments in the argument. List should agree with the number, data type, and order (position) of the parameters in the parameter. List. 41

Summary (2 of 6) • • • You can pass a variable by value to a procedure. – You do this by including the By. Val keyword before the parameter name in the receiving procedure’s parameter. List. You can pass a variable by reference to a procedure. – You do this by including the By. Ref keyword before the parameter name in the receiving procedure’s parameter. List. – Because the variable’s address is passed, the receiving procedure can change the contents of the variable. You can use the Math. Round method to round a number to a specified number of decimal places. 42

Summary (3 of 6) • Creating a Function procedure. – You invoke a function using a statement that assigns the function’s return value to a variable, uses the return value in a calculation, or displays the return value. • You use the Combo. Box tool in the toolbox to add a combo box to a form. – The combo box’s style is specified in its Drop. Down. Style property. 43

Summary (4 of 6) • You can use either the String Collection Editor or the Items collection’s Add method to add items to a combo box. – The Add 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. • • 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. 44

Summary (5 of 6) • • • You can use any of the following properties to select a combo box item from code: – Selected. Index, Selected. Item, or Text. 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 Drop. Down. List style, you can also use the Selected. Index or Selected. Item property. Drop. Down. Style property • To process code when the value in a combo box’s Text property changes, enter the code in the combo box’s Text. Changed event procedure. 45

Summary (6 of 6) • You can use the Message. Box. Show method to display a message box that contains text, one or more buttons, and an icon. • To process code when a form is about to be closed, enter the code in the form’s Form. Closing event procedure. – The Form. Closing 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 Form. Closing event procedure’s e parameter to True. 46
- Slides: 46