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) • • • The procedure that is invoked when a specific event occurs is called event-handling Sub procedure or, more simply, event procedure. An event procedure’s default name is composed of the object’s name followed by an underscore and the event’s name. The Handles clause in an event procedure’s header indicates the object and event associated with the procedure. 3
Event-Handling Sub Procedures (2 of 2) • • Unlike variable names, 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 an event procedure, you list each object and its associated 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 referred to as independent Sub procedures. An independent Sub procedure is processed only when a statement in your code calls (or invokes) it. Arguments (參數值) represent information that must be passed to a called 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) • A parameter (參數變數) is simply a memory location; more specifically, it is a variable in the called procedure. • The code of 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. 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) – 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 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 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 • 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 • 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. GUI • 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. 練習 p. 260 -261. 1~3 16
Cerruti Company application 17
Add Items to a Combo Box and Select a Default Item 18
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. 練習 p. 262. 1~2 19
Create an Event-Handling Sub Procedure GUI 20
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. 21
Calculate Federal Withholding Tax (2 of 5) Example 22
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 23
Calculate Federal Withholding Tax (4 of 5) Example 24
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 25
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. 練習 p. 266 -267. 1~3 1. Enter the procedure header 2. Copy the Select Case statement from the file Single. txt in project folder 26
Create an Independent Sub Procedure Copied from file Single. txt 27
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. 1. Enter the function header 2. Declare the dbl. Fed. Wth. Tax variable 練習 p. 267 -268. 1~3 3. Enter the Return statement 4. Copy the Select Case statement from the file Married. txt in project folder 28
Create a Function Copied from file Married. txt 29
Complete the Application and Invoke an Independent Sub Procedure and a Function (1/3) • Complete the btn. Calc_Click procedure – Declare the variables (範例程式碼已完成) 30
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 (範例程式碼已完成) 31
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 (範例程式碼已完成) 32
Complete the Application and Invoke an Independent Sub Procedure and a Function • Complete the btn. Calc_Click procedure – Determine the FWT 練習 p. 265. 5~7 33
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 (已完成) 34
Complete the Application and Invoke an Independent Sub Procedure and a Function • Complete the btn. Calc_Click procedure – Display calculated amounts (範例程式碼已完成) 35
Validate an Application’s Code GUI 36
Professionalize Your Application’s Interface (Using Form. Closing Event) • Use form’s Form. Closing event procedure to process code when form is about to be closed • Set Cancel property of Form. Closing event procedure’s e parameter to true to prevent form from being closed • We can communicate with the user during run time 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. 練習 p. 274. 1~2 icons title message Code buttons 37
Professionalize Your Application’s Interface message buttons title icons 38
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. 39
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. 40
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. 41
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. 42
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. 43
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. 44
- Slides: 44