Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Sub and Function Procedures
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Programming Based on Events
Arrays.
Chapter 7: Sub and Function Procedures
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Multiple Forms and Standard Modules
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Programming with Microsoft Visual Basic th Edition
Tutorial 1: An Introduction to Visual Basic.NET1 Tutorial 1 An Introduction to Visual Basic.NET.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Visual Basic Fundamental Concepts
Chapter 1: An Introduction to Visual Basic .NET
Microsoft Visual Basic 2008: Reloaded Third Edition
IS 350 Application Structure
Chapter 1: An Introduction to Visual Basic 2015
Using Procedures and Exception Handling
Visual Basic..
Microsoft Visual Basic 2005: Reloaded Second Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Lecture Set 10 Windows Controls and Forms
Presentation transcript:

Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures2 Creating Sub and Function Procedures Lesson A Objectives After completing this lesson, you will 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 by reference  Create a Function procedure

Tutorial 7: Sub and Function Procedures3 Procedures  A procedure is a block of program code that performs a specific task  Procedures in Visual Basic.NET can be either Sub procedures or Function procedures  Function procedures return a value after performing their assigned task  Sub procedures do not return a value

Tutorial 7: Sub and Function Procedures4 Sub Procedures  Event procedures  Called by Visual Basic.NET in response to an event  Every event procedure has at least two parameters sender – the object that raised the event e – information about the object  User-defined procedures  You must call explicitly  You can define parameters

Tutorial 7: Sub and Function Procedures5 Event Procedures Private Sub ExitButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles ExitButton.Click Me.Close() End Sub Page 269, 301 Parameters

Tutorial 7: Sub and Function Procedures6 Including Parameters in a User-Defined Sub Procedure  User-defined Sub procedures have both a procedure header and procedure footer Accessibility Sub ProcedureName([ParameterList]) ‘ Statements in the procedure End Sub Private Sub CalculatePay(ByVal sngHours As Single, _ ByVal sngRate As Single, ByRef sngPay As Single) ‘ Calculate the pay from hours and rate of pay sngPay = sngRate * sngHours End Sub Call CalculatePay(35.9, 95, sngPay)

Tutorial 7: Sub and Function Procedures7 Passing Variables  Pass by value – make a copy of the data  Use ByVal before the parameter name  This is the default if you do not specify  Pass by reference – pass the address of the data  Use ByRef before the parameter name Private Sub CalculatePay(ByVal sngHours As Single, _ ByVal sngRate As Single, ByRef sngPay As Single) ‘ Calculate the pay from hours and rate of pay sngPay = sngRate * sngHours End Sub sngHours = 35.9: sngRate = 95.0 Call CalculatePay(sngHours, sngRate, sngPay)

Tutorial 7: Sub and Function Procedures8 Practice  Pass by Ref Handout  Scope Handout

Tutorial 7: Sub and Function Procedures9 Function Procedures  A Function procedure, typically referred to as a function, is a block of code that performs a specific task  You can also create your own functions, referred to as user-defined functions, in Visual Basic.NET  The Return statement alerts the computer that the function has completed its task and ends the function after returning the value of its expression

Tutorial 7: Sub and Function Procedures10 Function Procedures Accessibility Function FunctionName([ParameterList]) As DataType ‘ Statements in the procedure Return SomeData End Function Private Function CalculatePay(ByVal sngHours As Single, _ ByVal sngRate As Single) As Single ‘ Calculate the pay from hours and rate of pay Return sngRate * sngHours End Function sngHours = 35.9: sngRate = 95.0 sngPay= CalculatePay(sngHours,sngRate)

Tutorial 7: Sub and Function Procedures11 EOC Practice  Page  No1~9

Tutorial 7: Sub and Function Procedures12 Using a List Box Control Lesson B Objectives After completing this lesson, you will be able to:  Add a list box to a form  Add items to a list box  Sort the contents of a list box  Select a list box item from code  Determine the selected item in a list box  Code a list box’s SelectedValueChanged event

Tutorial 7: Sub and Function Procedures13 Adding a List Box to a Form  You can use a list box control to display a list of choices from which the user can select zero choices, one choice, or more than one choice  The number of choices the user is allowed to select is controlled by the list box control’s SelectionMode property  The Windows standard for list boxes is to display a minimum of three selections and a maximum of eight selections at a time

Tutorial 7: Sub and Function Procedures14 Adding Items to a List Box  The items in a list box belong to a collection called the Items collection  The first item in the Items collection appears as the first item in the list box  The second item appears as the second item in the list box, and so on  The first item in the Items has an index of zero  The second item has an index of one, and so on

Tutorial 7: Sub and Function Procedures15 Adding Items to a List Box  You use the Items collection’s Add method to specify the items you want displayed in a list box control  When you use the Add method to add an item to a list box, the position of the item in the list depends on the value stored in the list box’s Sorted property

Tutorial 7: Sub and Function Procedures16 Remove Items from a List Box  When you remove an item from the list, the indexes change for subsequent items in the list. All information about the removed item is deleted.  You can use RemoveAt method to remove a specific item from the list by specifying the actual item to remove from the list.  To remove all items from the list, use the Clear method.  ZipFile – Ex0716

Tutorial 7: Sub and Function Procedures17 The SelectedItem and SelectedIndex Properties  A list box’s SelectItem property and its SelectedIndex property can be used both to determine the item selected in the list box and to select a list box item from code  The selected item is also called the default list box item  Should be either the most used selection  Or, if all of the selections are used fairly equally, the first selection in the list

Tutorial 7: Sub and Function Procedures18 Select an item SelectedIndex  Supported by the.NET Compact Framework.  Overridden. Gets or sets the zero-based index of the currently selected item in a ListBox. SelectedItem SelectedItem  Supported by the.NET Compact Framework.  Gets or sets the currently selected item in the ListBox.  ZipFile – Ex0717

Tutorial 7: Sub and Function Procedures19 Select an item  Public Sub SetSelected( _ ByVal index As Integer, _ ByVal value As Boolean _ )IntegerBoolean  Parameters  index  The zero-based index of the item in a ListBox to select or clear the selection for.ListBox  value  true to select the specified item; otherwise, false.  ZipFile – Ex0717

Tutorial 7: Sub and Function Procedures20 SelectionMode  The SelectionMode property enables you to determine how many items in the ListBox a user can select at one time and how the user can make multiple-selections.ListBox  When the SelectionMode property is set to SelectionMode.MultiExtended, pressing SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys (UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW) extends the selection from the previously selected item to the current item. Pressing CTRL and clicking the mouse selects or deselects an item in the list.  When the property is set to SelectionMode.MultiSimple, a mouse click or pressing the SPACEBAR selects or deselects an item in the list.

Tutorial 7: Sub and Function Procedures21 Combo Box  Combo box  ZipFile – Ex0718 Text Property  Supported by the.NET Compact Framework.  Overridden. Gets or sets the text associated with this control. Replace Function  Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Tutorial 7: Sub and Function Procedures22 For Each  ZipFile – Ex0719  Apply For Each structure to listbox collections For Each … In Next  SelectedItems: Gets a collection containing the currently selected items in the ListBox.

Tutorial 7: Sub and Function Procedures23 ListView  ZipFile – Square  ZipFile – Ex0720  A ListView.ColumnHeaderCollection class stores the column headers that are displayed in the ListView control when the View property is set to View.Details.  The ListView.ColumnHeaderCollection stores ColumnHeader objects that define the text to display for a column as well as how the column header is displayed in the ListView control when displaying columns.

Tutorial 7: Sub and Function Procedures24 ListView ListView.ColumnHeaderCollection Constructor [Visual Basic] Initializes a new instance of the ListView.ColumnHeaderCollection class. Public Sub New( _ ByVal owner As ListView _ ) Parameters owner The ListView control that owns this collection. Remarks You cannot create an instance of this class without associating it with a ListView control.

Tutorial 7: Sub and Function Procedures25 ListView.ColumnHeaderCollection.Add Method  There are a number of ways to add column headers to the collection. The Add method adds a single column header to the collection. Adds a column header to the collection with specified text, width, and alignment settings. Overloads Public Overridable Function Add(String, Integer, HorizontalAlignment) As ColumnHeader

Tutorial 7: Sub and Function Procedures26 ListView.ColumnHeaderCollection.Clear Method  The Clear method allows you to remove all column headers from the collection instead of using the Remove method to remove a single column header at a time.

Tutorial 7: Sub and Function Procedures27 ListViewItem.ListViewSubItemCollection  When a ListView displays columns, the items and their subitems are displayed in their own columns. To specify which columns subitem data is displayed under, see the ListViewItem.ListViewSubItemCollection class.

Tutorial 7: Sub and Function Procedures28 Coding the GetFwtTax Function  The amount of federal withholding text (FWT) to deduct from an employee’s weekly gross pay is based on the employee’s filing status—either single, (including head of household) or married—and his or her weekly taxable wages  To calculate the federal withholding tax you need to know the employee’s  Gross pay amount  Marital status  Number of withholding allowances

Tutorial 7: Sub and Function Procedures29 Completing the CalculateButton Click Event Procedure  Now that you have created the GetFwtTax function, you can call the function from the CalculateButton Click event procedure

Tutorial 7: Sub and Function Procedures30 Clearing the Contents of the Label Controls  The label controls also should be cleared when the SelectedValueChanged event occurs for one of the list boxes in the interface  A list box’s SelectedValueChanged event occurs each time a different value is selected in the list box

Tutorial 7: Sub and Function Procedures31 Completing the Payroll Application Lesson C Objectives After completing this lesson, you will be able to:  Add an existing form to a solution  Add a new module to a solution  Code the Sub Main Procedure  Create an instance of a form  Display a form object using the ShowDialog method

Tutorial 7: Sub and Function Procedures32 Adding an Existing Form to a Solution  The Copyright screen is to be the splash screen for each custom application created by the company

Tutorial 7: Sub and Function Procedures33 Coding the Sub Main Procedure  Sub Main is a special procedure in Visual Basic.NET, because it can be declared as the “starting point” for an application  In other words, you can tell the computer to process the Sub Main procedure automatically when an application is started  You enter the Sub Main procedure in a module, which is a file that contains code that is not associated with any specific object in the interface

Tutorial 7: Sub and Function Procedures34 Creating an Instance of a Form  A class definition specifies (or defines) the attributes and behaviors of an object  When an application is started, Visual Basic.NET automatically processes the code contained in one object: the Startup object  Similarly, if the PayrollForm is specified as the Startup object, Visual Basic.NET automatically processes the code contained in the PayrollForm class definition  When the Sub Main procedure is the Startup object, as it is in this case, neither the CopyrightForm class definition nor the PayrollForm class definition will be processed automatically

Tutorial 7: Sub and Function Procedures35 Using a Form Object’s Show Dialog Method  The form object’s Show dialog method allows you to display a form object on the screen  The syntax of the ShowDialog method is form.ShowDialog(), where form is the name of the object variable that contains the form object’s address