Download presentation
Presentation is loading. Please wait.
Published byPenelope Rachel Ryan Modified over 9 years ago
1
Programming with Microsoft Visual Basic 2010 5th Edition
Chapter Seven Sub and Function Procedures
2
Previewing Harvey Industries Application
Open the Harvey.exe file The Harvey Industries application calculates payroll for an employee Figure 7-1 Interface showing the payroll calculations
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 Create a Function procedure Explain the difference between passing data by value and passing data by reference
4
More About Sub Procedures
A 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
Passing Variables Variables Passing by value Passing by reference
Have both value and unique address Passing by value Passes a copy of the variable’s value Passing by reference Passes a variable’s address
6
Passing Variables by Value
Provides only the contents of variable to receiving procedure (called procedure) How to pass by value: Include keyword ByVal before parameter Reasons to pass by value: Procedure needs to know the contents of variable Procedure does not need to change original value (此變數只會出現在assignment statement右邊) By default, Visual Basic passes by value example
7
Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure
(定義程序時的參數) (引用程序時的參數) Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure
8
Passing Variables by Reference
Provides the address of variable to receiving procedure Address : memory location Receiving procedure can thus change the value of the variable Reason to pass by reference Procedure needs to change variable’s contents (此變數會出現在assignment statement左邊,異動值傳回呼叫的程式) How to pass by reference Include keyword ByRef before parameter example
9
lstHours lstRate 43.0 7.75 Figure 7-8 CalcGrossPay procedure and btnCalc control’s Click event procedure
10
Passing Variables by Reference (cont’d.)
Figure 7-9 Argument values before the Call CalcGrossPay statement is processed program
11
alias (Called by ByRef) (Called by ByVal) program
Figure 7-10 Argument and parameter values after the Call statement and CalcGrossPay procedure header are processed program
12
Figure 7-11 Argument and parameter values after the first statement in the CalcGrossPay procedure is processed program
13
Figure 7-12 Argument and parameter values after the statement in the selection structure’s true path is processed program
14
Figure 7-13 Argument and parameter values after the CalcGrossPay procedure ends
program
15
Function Procedures Function procedure
A block of code that performs specific task Returns a 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 Syntax & example
16
Figure 7-14 Function procedure syntax, examples, and steps
17
Invoking Function Procedures
Figure 7-15 Examples of invoking the GetNewPrice function
18
Sub procedure方式 Figure 7-16 CalcGrossPay function and btnCalc control’s Click event procedure
19
Lesson A Summary Two types of Sub procedures
Event and independent Function: Performs task and returns a value Independent procedures and functions Called from application’s code using Call statement Pass by value Send a copy of variable’s contents to procedure or function Pass by reference Send variable’s address to procedure or function
20
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
21
Including a Combo Box in an Interface
Allows user to select from a number of choices Allows user to type an 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 comparison
22
Including a Combo Box in an Interface
cboCity cboState cboName Figure 7-19 Examples of the combo box styles Program for this UI
23
Figure 7-20 Code associated with the combo boxes in Figure 7-19
24
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 the value that appears in text portion Use label control to provide keyboard access to the combo box example
25
Including a Combo Box in an Interface (cont’d.)
Figure 7-22 Gross pay amount shown in the interface
26
Lesson B Summary Use ComboBox tool to add combo box to a 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 Usage of SelectedIndex, SelectedItem, or Text property: To select combo box item from code To determine item that was selected
27
Lesson C Objectives After studying Lesson C, you should be able to:
Prevent a form from closing Round a number
28
Creating Harvey Industries Application
Application objective is to calculate: Employee’s weekly gross and net pay (初步薪資與淨薪資) Federal withholding tax (FWT) Social Security and Medicare (FICA) tax Net pay
29
Coding FormClosing Event Procedure
Occurs when form is about to be closed because: Computer processes Me.Close() statement User clicks Close button on form’s title bar Typical functions of 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
Coding the FormClosing Event Procedure
Figure 7-27 Pseudocode for the FormClosing event procedure result
31
Coding the FormClosing Event Procedure (cont’d.)
Figure 7-28 Message box displayed by the code in the FormClosing event procedure 實作練習在429~430頁
32
Coding btnCalc Control’s Click Event
UI Figure 7-29 Pseudocode for the btnCalc control’s Click event procedure 實作練習在431~432頁
33
Creating the GetFwt Function
GetFwt function emulates FWT table lookup Determining federal withholding tax (FWT) Evaluate weekly taxable wages (可稅週薪) and filing status Use data to look up FWT in special FWT tables How to calculate weekly taxable wages Multiply number of withholding allowances by $70.19 Subtract this result from weekly gross pay pseudocode
34
Creating the GetFwt Function (cont’d.)
Figure 7-35 Pseudocode for the GetFwt function
35
Creating the GetFwt Function (Married)
Figure 7-33 FWT calculations for a married (已婚的) taxpayer whose weekly taxable wages are $388.46
36
Creating the GetFwt Function (Single)
Figure 7-34 FWT calculations for a single (單身的) taxpayer whose weekly taxable wages are $600
37
GetFwt Function Header and Footer
Figure 7-36 GetFwt function header and footer 實作練習在435~439頁
38
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 實作練習在436~438頁
39
Adding an Existing Form to a Solution
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 Visual Studio and disk file system The splash screen for Country Charming Inn in Chapter 1 is to be integrated into Chap07/Harvey Industries Solution The splash screen identifies the application’s author and copyright year, or shows welcome message 實作練習 : 需下載 Chap07/ Harvey Industries Solution Chap07/Splash Solution Chap02/Playtime Solution
40
Adding an Existing Form to a Solution
Figure 1-31: Completed splash screen 40
41
Adding an Existing Form to a Solution
Rename the form in Splash Solution 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 Otherwise, the form frmMain will show up many times. The name of the main form in Harvey Industries Solution 41
42
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 Make the statement Me.picCountry.Image = …… as a comment 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 ) 42
43
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: Right click on the project in the Project Explorer Window to open the Properties Window Click the Application tab in Properties Window Click the Startup object ComboBox to set up the startup form → choose frmSplash 43
44
ShowDialog() vs. 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 () 44
45
Show Another Form by a Button
Download Chap02/Playtime Solution, Chap07/Harvey Industries Solution How to use a button to open a new form Project->加入現有項目->Find Playtime Form.vb to add Include Image File for the PictureBox PictureBox1 Add following codes in the Click event of a button Show Playtime on the Harvey Industries’ MainForm Me.Hide() ‘Show a form at a time Dim hForm As New frmPlaytime 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 45 45
46
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.