1 / 42

Programming with Microsoft Visual Basic 2010 5 th Edition

Programming with Microsoft Visual Basic 2010 5 th 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.

Télécharger la présentation

Programming with Microsoft Visual Basic 2010 5 th Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Microsoft Visual Basic 2010 5th 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

  3. Previewing the Harvey Industries Application (cont’d.) Figure 7-1 Interface showing the payroll calculations

  4. 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

  5. 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)

  6. 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

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

  8. Example: Event and Sub Procedure

  9. Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure

  10. 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

  11. Example (Using Sub Procedures)

  12. Figure 7-8 CalcGrossPay procedure and btnCalc control’s Click event procedure

  13. 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

  14. Figure 7-14 Function procedure syntax, examples, and steps

  15. Function Procedures (cont’d.) Figure 7-15 Examples of invoking the GetNewPrice function

  16. Example: Using Functions

  17. Figure 7-16 CalcGrossPay function and btnCalc control’s Click event procedure

  18. 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

  19. 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

  20. 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

  21. Including a Combo Box in an Interface (cont’d.) Figure 7-19 Examples of the combo box styles

  22. Figure 7-20 Code associated with the combo boxes in Figure 7-19

  23. 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

  24. Including a Combo Box in an Interface (cont’d.) Figure 7-22 Gross pay amount shown in the interface

  25. 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

  26. Lesson B Summary (cont’d.) • Use 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 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

  29. Coding the Harvey Industries Application (cont’d.) Figure 7-26 User interface for the Harvey Industries application

  30. 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

  31. Coding the FormClosing Event Procedure (cont’d.) Figure 7-27 Pseudocode for the FormClosing event procedure

  32. Coding the FormClosing Event Procedure (cont’d.) Figure 7-28 Message box displayed by the code in the FormClosing event procedure

  33. Coding the btnCalc Control’s Click Event Procedure Figure 7-29 Pseudocode for the btnCalc control’s Click event procedure

  34. Coding the btnCalc Control’s Click Event Procedure (cont’d.) Figure 7-30 Selection structure entered in the procedure

  35. 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

  36. Creating the GetFwt Function (cont’d.) Figure 7-33 FWT calculations for a married taxpayer whose weekly taxable wages are $388.46

  37. Creating the GetFwt Function (cont’d.) Figure 7-34 FWT calculations for a single taxpayer whose weekly taxable wages are $600

  38. Creating the GetFwt Function (cont’d.) Figure 7-35 Pseudocode for the GetFwt function

  39. Creating the GetFwt Function (cont’d.) Figure 7-36 GetFwt function header and footer

  40. 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

  41. Completing the btnCalc Control’s Click Event Procedure (cont’d.) Figure 7-37 Payroll calculations displayed in the interface

  42. 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

More Related