Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.

Slides:



Advertisements
Similar presentations
Microsoft Expression Web-Illustrated Unit J: Creating Forms.
Advertisements

Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
Using Macros and Visual Basic for Applications (VBA) with Excel
Chapter 7: Sub and Function Procedures
© by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Using the Visual Basic Editor Visual Basic for Applications 1.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
CHAPTER 6 Loop Structures.
Microsoft Visual Basic 2012 CHAPTER TWELVE (ENRICHMENT CHAPTER) Windows Store Apps.
CHAPTER SIX.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
IE 411/511: Visual Programming for Industrial Applications
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
Microsoft Visual Basic 2005 CHAPTER 7 Creating Web Applications.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Microsoft Visual Basic 2008 CHAPTER TWELVE Cell Phone Applications and Web Services.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Chapter 2 – Introduction to the Visual Studio .NET IDE
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
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.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 7 Creating Templates, Importing Data, and Working with SmartArt, Images, and Screen Shots Microsoft Excel 2013.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Dive Into® Visual Basic 2010 Express
IS 350 Application Structure
Chapter 1: An Introduction to Visual Basic 2015
Variables and Arithmetic Operations
3.01 Apply Controls Associated With Visual Studio Form
3.01 Apply Controls Associated With Visual Studio Form
Using Procedures and Exception Handling
Variables and Arithmetic Operations
CIS16 Application Development and Programming using Visual Basic.net
Programming with Microsoft Visual Basic 2008 Fourth Edition
Presentation transcript:

Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling

8 Chapter 8: Using Procedures and Exception Handling2 Objectives ►Create a splash screen ►Pause the splash screen ►Add a ComboBox object to a Windows Form ►Write Code for a SelectedIndexChanged event ►Code a Sub procedure

8 Chapter 8: Using Procedures and Exception Handling3 Objectives ►Pass an argument to a procedure by value ►Pass an argument to a procedure by reference ►Code a Function procedure to return a value ►Create a class-level variable ►Catch an exception using a Try-Catch block

8 Chapter 8: Using Procedures and Exception Handling4 Introduction ►As an application grows, it is important to divide each facet of a problem into separate sections of code called procedures ( modules generically ) ►Try-Catch blocks can check for any error a user might commit ►One way to make your programs more professional is to display a splash screen while the full program loads ( also distracts the user if the program takes a while to load… )

8 Chapter 8: Using Procedures and Exception Handling5 Chapter Project ComboBox ListBox

8 Chapter 8: Using Procedures and Exception Handling6 Creating a Splash Screen ►Create a Windows application and name it Ocean Tours. Name the form frmTours. ►Add a splash screen to the project: Right-Click Ocean Tours Project under Solution Explorer and click Add  Add New Item on the Project menu. In the Add New Item dialog box, select Splash Screen in the Templates section. Name the Splash Screen frmOceanToursSplashScreen. Click the Add button. ►Specify Splash Screen to use for Project Click the splash screen form in the left side of the form to select the form. To set the application to display the splash screen first, right-click OceanTours in the Solution Explorer and click Properties.

8 Chapter 8: Using Procedures and Exception Handling7 Creating a Splash Screen ►In the Windows application framework properties section, click the Splash screen list arrow, and then click frmOceanToursSplashScreen to select the form as the splash screen used for project ►Click the Assembly Information Button on the Properties Designer to open the Assembly Information dialog box ►To customize the splash screen, change the Title to Ocean Tours and the Copyright to the present year. The File Version can be changed as you update the application

8 Chapter 8: Using Procedures and Exception Handling 8 Creating a Splash Screen ►Click the OK button on the Assembly Information dialog box. Close the OceanTours Project Designer window. ►To change the predefined image, first download the ocean.bmp picture from the scsite/vb2008/ch8/images/Ocean.jpg Web site and store the image in a location you remember. ►Then, click the frmOceanToursSplashScreen.vb [Design] tab. Click the left side of the splash screen, making sure to select the entire splash screen form. The Properties window should identify MainLayoutPanel if you have selected the entire splash screen form. ►Click to the right of the Background Image property in the Properties window, and then click the ellipsis button. ►In the Select Resource dialog box, click the Project resource file radio button if necessary. Import the ocean.jpg picture by clicking the Import button in the Select Resource dialog box and selecting the ocean.bmp image from the location where you stored it. Click the OK button in the Select Resource dialog box ►Run the application by clicking the Start Debugging button on the Standard toolbar

8 Chapter 8: Using Procedures and Exception Handling9 Creating a Splash Screen

8 Chapter 8: Using Procedures and Exception Handling10 Pausing the Splash Screen ►After the splash screen loads, the application executes any code in the form load event handler. ►To display the splash screen for five seconds, the code that calls the Sleep method should be in the form load event handler. To open the code editor window and the form load event handler, double-click the background of the frmTours Windows Form object in the Design window ►Click inside the frmTours Load event handler. Type Threading. To cause IntelliSense to display a list of possible entries. If necessary, type T to select Thread from the IntelliSense list. Type.S to select Sleep from the IntelliSense list. Type (5000) [ milliseconds ] Threading.Thread.Sleep(5000)

8 Chapter 8: Using Procedures and Exception Handling11 Pausing the Splash Screen

8 Chapter 8: Using Procedures and Exception Handling12 ComboBox Object [ combination of a TextBox and a List ] ►Text box Used to display textual information Enables inputting of text from keyboard ►List box Extends functionality of text box Provides ability to select item from predetermined list of values List is not displayed unless the user clicks the downarrow ►Can also type value

8 ComboBox.NET Component ►The ComboBox control presents users with a list of options. The list is shown and hidden as the control expands and collapses. In its default state, the list is collapsed, displaying only one choice. The user clicks a button to see the complete list of options.ComboBox ►The following illustration shows a ComboBox in different states. ( Collapsed and expanded )ComboBox Chapter 8: Using Procedures and Exception Handling13

8 Chapter 8: Using Procedures and Exception Handling14 ComboBox Object

8 Chapter 8: Using Procedures and Exception Handling15 Adding a ComboBox Object ►Drag the ComboBox.NET component from the Common Controls category of the Toolbox to the approximate location where you want to place the ComboBox object. A ComboBox is a “combination” of a TextBox and a ListBox. ►With the ComboBox object selected, scroll in the Properties window to the (Name) property. Double-click in the right column of the (Name) property and then enter the name cboIsland ►In the Properties window, scroll to the Text property. Click to the right of the Text property and enter Select Island Location: to specify the text that appears in the combo box. Resize the ComboBox object as needed to display the data in the box

8 Chapter 8: Using Procedures and Exception Handling16 Adding a ComboBox Object ►In the Properties window, scroll to the Items property, and click to the right of the Items property on the word (Collection). Click the ellipsis button. The String Collection Editor dialog box opens. Enter the island locations Aruba (press ENTER), Jamaica (press ENTER), and Key West ►In the String Collection Editor dialog box, click the OK button. ►Click the Start Debugging button on the Standard toolbar to run the application. Click the list arrow on the right of the ComboBox object to view the contents. You can select a choice from the list

8 Chapter 8: Using Procedures and Exception Handling17 Adding a ComboBox Object

8 Chapter 8: Using Procedures and Exception Handling18 Determining the ComboBox Selected Index If the user has not made a selection, the SelectedIndex property is -1

8 Chapter 8: Using Procedures and Exception Handling19 Determining the ComboBox Selected Index ►Select the ComboBox object named cboIsland on the Windows Form object ►Double-click the ComboBox object. Close the Toolbox ►Note: You can work with the SelectedItem or SelectedIndex with a ListBox, DropDownList, and a ComboBox. All these objects have a SelectedIndexChanged event handler.

8 Chapter 8: Using Procedures and Exception Handling20 Coding a Sub Procedure ►When a program is broken into manageable parts, each part is called a procedure A procedure is a named set of code that performs a given task. The generic term is module. ►A Sub procedure is a procedure that completes its task but does not return any data to the calling procedure ►A Sub procedure is the series of Visual Basic statements enclosed by the Sub and End Sub statements ►A Sub procedure is called with a statement consisting of the procedure name and a set of parentheses in the form of a procedure call  clear( )

8 Chapter 8: Using Procedures and Exception Handling21 Coding a Sub Procedure Return

8 Chapter 8: Using Procedures and Exception Handling22 Coding a Sub Procedure Case actions are procedure calls

8 Chapter 8: Using Procedures and Exception Handling23 Passing Arguments ►When a procedure is called, however, the call statement can pass an argument to the called procedure ► Note: the following examples use the MessageBox.Show method rather than the MsgBox function Should use an ElseIf here…

8 Chapter 8: Using Procedures and Exception Handling24 Passing Arguments by Value (ByVal) ►The value is copied into a variable whose name is specified in the Sub procedure declaration statement

8 Chapter 8: Using Procedures and Exception Handling25 Passing Arguments by Value (ByVal)

8 Chapter 8: Using Procedures and Exception Handling26 Passing Arguments by Reference (ByRef) ►The second way in which to pass an argument from a calling procedure to a called Sub procedure is by reference ►You specify you want to pass a value by reference by entering the keyword ByRef in the Sub procedure declaration ►Passing a value by reference allows code in the Sub procedure to modify the contents of the variable that is being passed. When you use ByRef, you are passing a reference ( address of an object in memory ) to the variable that holds the value instead of the value as when you use ByVal

8 Chapter 8: Using Procedures and Exception Handling27 Passing Arguments by Reference

8 Chapter 8: Using Procedures and Exception Handling28 Passing Arguments by Reference

8 Chapter 8: Using Procedures and Exception Handling29 Passing Multiple Arguments ►You can pass as many arguments as needed to a Sub procedure ►If you have more than one argument, the variables are passed in the same order in which they appear in the procedure call statement

8 Chapter 8: Using Procedures and Exception Handling30 Function Procedures ►A Function procedure is similar to a Sub procedure except that a Function procedure returns a single value to the calling procedure ►Note: A Function Procedure may accept parameters in its parameter list as well. Expression…

8 Chapter 8: Using Procedures and Exception Handling31 Function Procedures ►The Function procedure is different in appearance from a Sub procedure in the following ways: The Function procedure must return a value. The value can be any valid expression. The data type of the return value is listed in the procedure declaration The keyword Return is used in the Function procedure to return a single value

8 Chapter 8: Using Procedures and Exception Handling32 Function Procedures Or: Return decMiles / decGallons

8 Chapter 8: Using Procedures and Exception Handling33 Sub and Function Procedures ►Default access to both Sub and Function procedures is public. Generally these should be declared to be private. ►Default access when a Dim keyword is used at the class level is private.

8 Chapter 8: Using Procedures and Exception Handling34 Sub and Function Procedures  All executable code must be in procedures. You can define a Sub procedure inside a module, class, interface, or structure, but not inside another procedure.  ByVal is the default for all passed arguments in a function or sub procedure  Sub procedures are Public by default. To specify a different accessibility, include Protected, Friend, Protected Friend, or Private in the declaration.  When the Sub procedure returns to the calling program, execution continues with the statement following the statement that called it.  The Exit Sub statement causes an immediate exit from a Sub procedure. Any number of Exit Sub statements can appear anywhere in the procedure. You can also use the Return statement to immediately exit the procedure.  Any number of Return statements can appear anywhere in the procedure. You can also mix Exit Sub and Return statements.  A Sub procedure, like a Function procedure, is a separate procedure that can take arguments and perform a series of statements. However, unlike a Function procedure, which returns a value, a Sub procedure cannot be used in an expression.  You call a Sub procedure by using the procedure name, followed by the argument list in parentheses, in a statement.  You can omit the parentheses only if you are not supplying any arguments. You can optionally use the Call statement to call a Sub procedure. This can improve the readability of your program.

8 Chapter 8: Using Procedures and Exception Handling35 Creating a Private Class-Level Variable ►When a class-level variable cannot be referenced outside the class in which it is declared, the variable is said to have Private access. Underscore (_) is a convention

8 Chapter 8: Using Procedures and Exception Handling36 Exception Handling ►The Try-Catch set of statements detects exceptions and takes corrective action

8 Chapter 8: Using Procedures and Exception Handling37 Exception Handling

8 Chapter 8: Using Procedures and Exception Handling38 Exception Handling Null generally has an ordinal value of Zero The default value for a reference type is Null

8 Chapter 8: Using Procedures and Exception Handling39 Exception Handling Sequence of catch blocks is very important. Go from most specific to most general.

8 Chapter 8: Using Procedures and Exception Handling40 Program Design

8 Chapter 8: Using Procedures and Exception Handling41 Program Design

8 Chapter 8: Using Procedures and Exception Handling42 Program Design

8 Chapter 8: Using Procedures and Exception Handling43 Program Design when using Sub and Function Procedures ►When a program becomes larger, often it is advantageous to break the program into procedures, which perform specific tasks within the program Makes the program easier to read, understand, and debug ►The developer must determine what code should be placed in a procedure. Procedures should: Perform a single task Perform reasonably substantial processing ►Use Sub and Function procedures appropriately

8 Chapter 8: Using Procedures and Exception Handling44 Event Planning Document

8 Chapter 8: Using Procedures and Exception Handling45 Event Planning Document

8 Chapter 8: Using Procedures and Exception Handling46 Event Planning Document

8 Chapter 8: Using Procedures and Exception Handling47 Event Planning Document

8 Chapter 8: Using Procedures and Exception Handling48 Event Planning Document

8 Chapter 8: Using Procedures and Exception Handling49 Event Planning Document

8 Chapter 8: Using Procedures and Exception Handling50 Summary ►Create a splash screen ►Display the splash screen for 5 seconds ►Add a ComboBox object to a Windows Form ►Write Code for a SelectedIndexChanged event ►Code a Sub procedure

8 Chapter 8: Using Procedures and Exception Handling51 Summary ►Pass an argument to a procedure by value ►Pass an argument to a procedure by reference ►Code a Function procedure to return a value ►Create a class-level variable ►Catch an exception using a Try-Catch block

Microsoft Visual Basic 2008 CHAPTER 8 COMPLETE Using Procedures and Exception Handling