420 likes | 536 Views
This chapter focuses on Sub and Function procedures within Microsoft Visual Basic 2010, essential for effective programming. Learn the differences between these procedures, how to pass data by value and by reference, and create your own custom functions. The chapter also covers how to utilize ComboBoxes in user interfaces, detailing their properties and event handling. Gain a solid understanding of these core components to enhance your application development skills in payroll calculations and other programs.
E N D
Programming with Microsoft Visual Basic 2010 5th Edition Chapter Seven Sub and Function Procedures
Previewing the Harvey Industries Application • Open the Harvey Industries.exe file • The Harvey Industries application calculates payroll for an employee
Previewing the Harvey Industries Application (cont’d.) Figure 7-1 Interface showing the payroll calculations
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
More About Sub Procedures • Procedure • 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)
Passing Variables • Variables • Have both value and unique address • 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
Passing Variables by Value • Provides only contents of variable to receiving procedure • How to pass by value: • Include keyword ByVal before parameter • Reasons to pass by value: • Procedure needs to know contents of variable • Procedure does not need to change original value • By default, Visual Basic passes by value
Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure
Passing Variables by Reference • Provides address (memory location) of variable to procedure • Receiving procedure can thus access variable • Reason to pass by reference • Procedure needs to change variable’s contents • How to pass by reference • Include keyword ByRef before parameter
Figure 7-8 CalcGrossPay procedure and btnCalc control’s Click event procedure
Function Procedures • Function procedure • Block of code that performs specific task • Returns 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 • Returnexpression type must agree with As datatype
Function Procedures (cont’d.) Figure 7-15 Examples of invoking the GetNewPrice function
Figure 7-16 CalcGrossPay function and btnCalc control’s Click event procedure
Lesson A Summary • Two types of Sub procedures • Event and independent • Function: Performs task and returns value • Independent procedures and functions • Called from application’s code using Call statement • Pass by value • Send copy of variable’s contents to procedure or function • Pass by reference • Send variable’s address to procedure or function
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
Including a Combo Box in an Interface • Combo box • Allows user to select from number of choices • Allows user to type 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
Including a Combo Box in an Interface (cont’d.) Figure 7-19 Examples of the combo box styles
Figure 7-20 Code associated with the combo boxes in Figure 7-19
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 value that appears in text portion • Use label control to provide keyboard access to the combo box
Including a Combo Box in an Interface (cont’d.) Figure 7-22 Gross pay amount shown in the interface
Lesson B Summary • Use ComboBox tool to add combo box to 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
Lesson B Summary (cont’d.) • Use SelectedIndex, SelectedItem, or Text property: • To select combo box item from code • To determine item that was selected
Lesson C Objectives After studying Lesson C, you should be able to: • Prevent a form from closing • Round a number
Creating the Harvey Industries Application • Application objective is to calculate: • Employee’s weekly gross pay • Federal withholding tax (FWT) • Social Security and Medicare (FICA) tax • Net pay • Review TOE chart for requirements
Coding the Harvey Industries Application (cont’d.) Figure 7-26 User interface for the Harvey Industries application
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 Cancelproperty of FormClosing procedure’s e parameter to true
Coding the FormClosing Event Procedure (cont’d.) Figure 7-27 Pseudocode for the FormClosing event procedure
Coding the FormClosing Event Procedure (cont’d.) Figure 7-28 Message box displayed by the code in the FormClosing event procedure
Coding the btnCalc Control’s Click Event Procedure Figure 7-29 Pseudocode for the btnCalc control’s Click event procedure
Coding the btnCalc Control’s Click Event Procedure (cont’d.) Figure 7-30 Selection structure entered in the procedure
Creating the GetFwt Function • How to calculate weekly taxable wages • Multiply number of withholding allowances by $70.19 • 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
Creating the GetFwt Function (cont’d.) Figure 7-33 FWT calculations for a married taxpayer whose weekly taxable wages are $388.46
Creating the GetFwt Function (cont’d.) Figure 7-34 FWT calculations for a single taxpayer whose weekly taxable wages are $600
Creating the GetFwt Function (cont’d.) Figure 7-35 Pseudocode for the GetFwt function
Creating the GetFwt Function (cont’d.) Figure 7-36 GetFwt function header and footer
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
Completing the btnCalc Control’s Click Event Procedure (cont’d.) Figure 7-37 Payroll calculations displayed in the interface
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