Chapter six Reducing program complexity General sub procedures and developerdefined functions. After completing this unit, you should be able to Share code by creating general sub procedures and functions; use parameters to share data between procedures and functions; use code modules to organize code for reusability; use the KeyPress, Enter, and Leave events; use the concept of form modality; create and program main menu.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions 6- Introduction • Three important consideration help us design, construct, and maintain complex programs: Break complex tasks into smaller “subtasks.” Give each subtask a descriptive name Find processing tasks that have subtasks in common McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- Objectives • Share code by creating general sub procedures and functions • Use parameters to share data between procedures and functions • Use code modules to organize code for reusability • Use the KeyPress, Enter, and Leave events • Use the concept of form modality • Create and program main menus McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- 6.1 General Sub Procedures • We must be very precise in writing the criterion and alternative actions for decisions • In a program, – A condition is represented as an expression – An outcome is the result of an evaluated condition – An appropriate action follows the outcome McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- 6.1 General Sub Procedures (cont.) • Using General Sub Procedures in a Project – Eliminate inconsistencies by placing common statements in a general sub procedure – Apply a descriptive name for the sub procedure McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- 6.1 General Sub Procedures (cont.) • Execution of General Sub Procedures – Names of event procedures always end with an underscore followed by the type of event – Names of general sub procedures not – A procedure call invokes a procedure McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- 6.1 General Sub Procedures (cont.) • Local Variables in General Sub Procedures – General sub procedures can access module-level and global variables – They have their own variables – Procedure-level variables are not related to other procedures McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- 6.1 General Sub Procedures (cont.) • General Sub Procedures and Project Structure – Locating a General Sub Procedure in the Code Window • We enter and view event procedures in the Code window • We also enter and view general sub procedures in the Code window • Scroll to find a procedure’s heading McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6- 6.1 General Sub Procedures (cont.) – Procedure Scope • Determines which procedure’s can invoke it • Private and Public – Code Modules • Can contain general sub procedures • Help to organize a project • Can be included in many different projects – Project Structure • See Figure 6.13 of textbook McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6.1 General Sub Procedures (cont.) • Creating General Sub Procedures – Start from the Code window of a form or code module – Enter header for the sub procedure – Enter general sub procedure statements McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 610 6.3 Developer-Defined Functions • • • • Perform calculations or string manipulations Return values Use Return statements Function headings include type specifications McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 620 6.3 Developer-Defined Functions (cont.) • Creating Functions – Follow the same steps to create a general sub procedure – Use the keyword Function instead of Sub McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 621 6.4 Code Modules • Sub Main – Use to begin execution of the program by executing a general sub procedure – Select in the Misc Property Pages dialog box under the Project menu – You must create a public general sub procedure named Main in a code module McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 622 6.4 Code Modules (cont.) • Libraries – Programmers organize reusable code for easy access – A collection of code modules in categories is called a library McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 623 6.5 The KeyPress Event 624 • Enables your programs to respond to keystrokes made by the user • Any control that can have the focus is able to respond to this event McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6.5 The KeyPress Event (cont.) • The KeyPress Event for TextBox Controls – User presses a key – Visual Basic NET stores ANSI character of the key – Visual Basic NET begins KeyPress event procedure – The code will determine the disposition of the keypress McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 625 6.5 The KeyPress Event (cont.) – The SendKeys Class • Used to send any keystroke to an application • Keystrokes are represented by codes McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 626 6.6 The Enter and Leave Events 627 • Enter event occurs for a control when the control receives the focus • Leave event occurs for a control when the control loses the focus • Both events are triggered by the user or by code • Help to make an application more intuitive for the user McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 6.7 Modal versus Modeless Forms • Most real-world applications use multiple forms • Form modality controls how a form is displayed – When a modal form is shown, other forms become inactive – When a modeless form is displayed, both its controls and the controls on other forms are active McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 628 6.7 Modal versus Modeless Forms (cont.) • Modal Forms – A message box is the simplest example – User must click OK to return control to the procedure • Presenting a Sequence of Forms – Using modeless forms • Control code must be written within each form – Using modal forms • Control of all forms can be in a one procedure McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 629 6.8 The MainMenu Control 630 • Start by right-clicking on the MainMenu control in the component tray • Then select Edit Menu from the pop-up menu • Enter both main and sub menu items McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Chapter Summary • General sub procedures and developer-defined functions break programming into small parts • A general sub procedure performs a specific processing task • Developers can use a library of procedures performing common tasks • General sub procedures are not linked to any control on a form McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 631 Chapter Summary (cont.) 632 • General sub procedures can be called from other general sub procedures or from event procedures • A developer-defined function returns a single value when it executes • Procedures and functions can use parameters to share data • An argument can be passed to a procedure or function either by reference or by value McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Chapter Summary (cont.) • The KeyPress event procedure has a parameter that determines the key that the user has pressed • The Enter event occurs when the user gives the focus to a control • Form modality refers to the way forms react when two or more forms are displayed at one time • Form modality is specified by the type of Show method used McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 633 Chapter Summary (cont.) 634 • Applications can incorporate a main menu bar by using the MainMenu control McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved ... reserved 6- 6. 1 General Sub Procedures (cont.) • Execution of General Sub Procedures – Names of event procedures always end with an underscore followed by the type of event – Names of general sub procedures. .. rights reserved 6- 6. 1 General Sub Procedures (cont.) • General Sub Procedures and Project Structure – Locating a General Sub Procedure in the Code Window • We enter and view event procedures in... Companies, Inc All rights reserved Chapter Summary • General sub procedures and developer-defined functions break programming into small parts • A general sub procedure performs a specific processing