Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures

2 Previewing the Harvey Industries Application Open the Harvey Industries.exe file The Harvey Industries application calculates payroll for an employee 2 Figure 7-1: Payroll calculations shown in the interface

3 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 3

4 Procedures Procedure: –A block of program code that performs specific tasks Two types of procedures in Visual Basic: –Sub procedure: Does not return value –Function procedure: Does return value 4

5 Sub Procedures Two types of Sub procedures in Visual Basic: –Event Sub procedures –Independent Sub procedures Event procedure: Associated with specific object and event –Processed automatically in response to event of an object Independent Sub procedure: Independent of any object and event –Invoked from code using Call statement 5

6 Call statement, Argument, Parameter Call statement: Used to invoke procedure Syntax: Call procedurename([argumentlist]) –argumentlist: Used to pass information (optional) Argument ( 引用的參數 ) : Data item in argumentlist Parameter ( 定義的參數 ) : variable name in parameterlist Relationship between arguments and parameters –Should agree in number, position, and data type Types of data that can be passed as arguments to a procedure: –Variable, literal constant, named constant, keyword 6

7 Syntax of Sub Procedure and Call Statement Figure 7-2: Syntax of an independent Sub procedure and the Call statement 7 agree in number, position, and data type

8 Passing Variables Every variable has a value and a unique address in memory You can pass either variable’s value or its address to the called procedure Passing by value: –Passes the value stored in a variable Passing by reference: –Passes the memory address of a variable –Allows the called procedure to change the content of the passed variable 8

9 Passing a Variable by Value Passing by value: Provides only content of the variable to called procedure (receiving procedure) How to pass by value when defining a VB procedure –Include keyword ByVal before parameter (variable) Reasons to pass by value: –The called procedure does not need to change the values of arguments from the calling procedure By default, Visual Basic pass by value 9

10 Figure 7-5: ShowMsg procedure and btnDisplay Click event procedure 10 Definition and Use of Passing by Value called procedure calling procedure 1 3 2

11 Passing a Variable by Reference Passing by reference: Provides address (memory location) of a variable to the called procedure (receiving procedure) Reason to pass by reference: –The called procedure needs to change the values of the arguments from the calling procedure How to pass by reference when defining a VB procedure –Include keyword ByRef before parameter (variable) 11

12 Figure 7-8: CalcGrossPay procedure and btnCalc contro’s Click event procedure 12 Definition and Use of Passing by Reference called procedure calling procedure 1 3 2

13 Passing a Variable by Reference (Scenario 1: before processing Call statement ) Figure 7-9: Desk-check table before the computer processes the Call statement 13 In CalcGrossPay procedure

14 Passing a Variable by Reference ( Scenario 2 : Right after processing Call statement ) Figure 7-10: Desk-check table after the computer processes the Call statement and the CalcGrossPay procedure header 14 Step 2. Call CalcGrossPay procedure called procedure calling procedure called procedure step 2.1 copy Step 2.2 alias Step 1. Run btnCalc.Click Program

15 Passing a Variable by Reference Figure 7-11: Desk-check table after the computer processes the first statement in the CalcGrossPay procedure 15 2. Call CalcGrossPay 1. Run btnCalc.Click 3. Run this statement 2.2 alias 4. update 2.1 copy 0

16 Passing a Variable by Reference ( Scenario 3 : after processing If statement ) 16 Figure 7-12: Desk-check table after the computer processes the statement in the selection structure’s true path aliases CalcGrossPay procedure 2. dblGross = 333.25 3. dblGross = 344.86 1. dblGross = 0 result after this statement 1 2 3 4

17 Passing a Variable by Reference ( Scenario 4 : after CalcGrossPay procedure ends ) 17 Figure 7-13: Desk-check table after the CalcGrossPay procedure ends Exit CalcGrossPay procedure Program Value returning to btnCalc.Click procedure

18 Function Procedures Function procedure: –A block of code that performs a set of specific tasks –Returns a value after completing its tasks 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 18 Data types must agree

19 Figure 7-14: Syntax, example, and steps for creating a function 19 Definition and Usage of Function Procedure Data types must agree dblNew’s data types must be Double for writing a function

20 Example of Invoking Function Procedure Figure 7-15: Examples of invoking the GetNewPrice function 20 GetNewPrice function

21 Example of Using Function Procedure Figure 7-16: CalcGrossPay function and btnCalc control’s Click event procedure 21 和 Sub 比 1 3 2

22 Lesson A Summary Two types of procedures: Event and independent Function: Performs tasks and returns a value Event and independent procedures are called from application’s code using Call statement Pass by value: Send a copy of variable’s contents to the called procedure or function Pass by reference: Send a copy of variable’s addresses to the called procedure or function 22

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 by program code Determine the item either selected or entered in a combo box Code a combo box’s TextChanged event procedure 23

24 Including a Combo Box in an Interface Combo box: 1.Allows user to select from a number of choices 2.Contains a text portion and a list portion 3.Allows user to type an entry not on comboBox list 4.Can save space on form List box does not share above features 3 and 4 DropDownStyle property: –Values: Simple, DropDown (default), DropDownList 24

25 DropDownStyle property of Combo Box Figure 7-19: Examples of the combo box styles 25 text portion list portion

26 Important Properties of Combo Box Change in item value causes TextChanged event Use Item collection Items’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: Determines which item is selected –Text: Used to get or set value in text portion 26

27 Figure 7-20: Code used to add items to the combo boxes and also select a default item 27 Including a Combo Box (Select a default item)

28 Lesson B Summary Combo box displays a list of items for selection Combo box allows user to type an entry not on list 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 28

29 Lesson B Summary (continued) Use SelectedIndex, SelectedItem, or Text property to select a default combo box item from program code Use SelectedIndex, SelectedItem, or Text property to determine item that was selected 29

30 Lesson C Objectives After studying Lesson C, you should be able to: Prevent a form from closing Round a number 30

31 Coding the Harvey Industries Application Objective: –Calculate employee’s weekly gross pay ( 週毛薪 ), federal withholding tax (FWT), Social Security and Medicare (FICA) tax, and net pay ( 淨薪支 ) Review TOE chart for requirements 31

32 Figure 7-25: User interface for the Harvey Industries application 32 Coding the Harvey Industries Application (continued)

33 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 33

34 Coding the FormClosing Event Procedure (continued) Figure 26: Pseudocode for the FormClosing event procedure 34

35 Coding the FormClosing Event Procedure (continued) Figure 27: Message box displayed by the FormClosing event procedure 35

36 36 Figure 28: Pseudocode for the btnCalc control’s Click event procedure Coding the btnCalc Control’s Click Event Procedure

37 Figure 29: Selection structure entered in the procedure 37

38 Coding the GetFwt Function How to calculate weekly taxable wages ( 可稅薪支 ) –Multiply the number of withholding allowances by $67.31 –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 38

39 Coding the GetFwt Function (continued) Figure 7-33: Pseudocode for the GetFwt function 39

40 40 Programming with Microsoft Visual Basic 2005, Third Edition Coding the GetFwt Function Figure 7-30: Weekly FWT tables (Single person)

41 41 Programming with Microsoft Visual Basic 2005, Third Edition Coding the GetFwt Function Codes implementing weekly FWT tables (Single person)

42 Coding the GetFwt Function (continued) 42 Figure 7-34: GetFWT function header and footer

43 Completing the btnCalc Control’s Click Event Procedure Must call GetFwt function from btnCalc’s Click event procedure Math.Round function: Used to round value to specific number of decimal places Syntax: Math.Round (value[, digits]) –value: Numeric value to work on –digits: Number of places to right of decimal point 43

44 Completing the btnCalc Control’s Click Event Procedure (continued) 44 Figure 7-35: Payroll calculations displayed in the interface

45 Adding an Existing Form to a Solution 45 Add an existing form to Chap07/Harvey Industries Solution –Make sure that added form uses a different form name from any form name of Harvey Industries Solution in disk file system, and VB.NET The splash screen for Country Charming Inn in Chapter 1 is to be used as the splash screen for the integrated application –The splash screen identifies the application’s author and copyright year, or shows welcome message

46 Adding an Existing Form to a Solution (continued) Figure 1-31: Completed splash screen 46

47 Adding an Existing Form to a Solution 1.Download Chap07/Splash Solution 2.Project-> 加入現有項目 ->Find Splash Form.vb to add Open a new form by Timer’s Tick event Add following codes in the Tick event of the timer tmrExit Me.Hide() tmrExit.Enabled = False Dim mForm As New frmMain mForm.ShowDialog() Me.Close() Syntax used to instruct the computer to create an object from a class: Dim variablename As New classname 47 The name of the main form in current project Otherwise, the form frmMain will show up many times.

48 Include Image File for the Added Form VB doesn’t import image files automatically for the added form. You can do it by one of the following steps Click Splash Form’s PictureBox picCountry Use the task box of the PictureBox to set up its image file (image file is in the Resources folder of the Chap07/Splash Solution ) 48

49 Assigning the Startup Form When an application is started, VB.NET automatically invokes the form set as the Startup object of a project The setup steps for Startup object are as follows: 1.Right click on the project in the Project Explorer Window to open the Properties Window 2.Click the Application tab in Properties Window 3.Click the Startup object ComboBox to set up the startup form → choose frm Splash 49

50 Using a Form Object’s ShowDialog() & Show() Method A form object’s ShowDialog method allows you to display the form object on the screen with focus transfer (dialog-style interaction) –The syntax is: form.ShowDialog() The form object’s Show method allows you to display a form object on the screen without focus transfer –The syntax is: form.Show () 50

51 51 Programming with Microsoft Visual Basic 2005, Third Edition Show Another Form by a Button Download Chap07/Playtime Solution, Harvey Industries Solution How to use a button to open a new form 1.Project-> 加入現有項目 ->Find Playtime Form.vb to add 2.Include Image File for the PictureBox PictureBox1 3.Add following codes in the Click event of a button Show Playtime on the Harvey Industries’ MainForm –Dim hForm As New frmPlaytime hForm.txtName.Text = Me.txtName,Text Me.Hide() hForm.ShowDialog() Me.Show() –frmPlaytime.Show() or.ShowDialog() ‘Show 2 forms frmPlaytime.Focus() (when using frmPlaytime.Show() ) The name of main form in Playtime Solution 將表單 A 的資訊傳給表單 B ,再將表單 A 隱藏

52 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 Multiple forms processing Adding an existing form Invoke another form by a button 52


Download ppt "Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures."

Similar presentations


Ads by Google