Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Multiple Forms, Standard Modules, And Menus
Advertisements

Using Macros and Visual Basic for Applications (VBA) with Excel
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 7: Sub and Function Procedures
Programming In Visual Basic .NET
110-H1 More VB Tools Creating a Menu: What is a menu? a group of (related) commands displayed at at the top of an application Top level menu Disabled command.
Dialog Boxes and Menus. Menus Menu Bar Contains menus which drop down to display list of menu items Each item has a name and text property Each item has.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
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.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
C# Programming: From Problem Analysis to Program Design1 9 Programming Based on Events.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 7 Multiple Forms, Modules,
Example 2.
C# Programming: From Problem Analysis to Program Design1 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3 rd Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
A Guide to Oracle9i1 Creating an Integrated Database Application Chapter 8.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Copyright © 2014 Pearson Education, Inc. Chapter 7 Multiple Forms, Modules, and Menus.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
Apply Sub Procedures/Methods and User Defined Functions
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
CIS162AD - C# Methods, Menus, and Dialog Boxes 05_methods_menus.ppt.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Multiple Forms, Standard Modules, And Menus
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Debugging and Menus Part07dbg --- Solving the problem, Debugging in Break time, Menus, Common Dialogs, and User-written methods.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
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.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Compunet Corporation Programming with Visual Basic.NET Working with Menus and Dialog Boxes Week 14 Tariq Aziz and Kevin Jones.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
Visual Basic.NET BASICS Lesson 14 Menus and Printing.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
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 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Chapter 1: An Introduction to Visual Basic 2015
Files.
Using Procedures and Exception Handling
Chapter 7 Advanced Form Techniques
Brief description on how to navigate within this presentation (ppt)
Presentation transcript:

Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

5-2 Objectives Create menus and submenus for program control. Display and use the Windows common dialog boxes. Create context menus for controls and the form. Write reusable code in sub procedures and function procedures and call the procedures from other locations.

5-3 Menus Menu Bar Contains menus which drop down to display list of menu items – Can be used in place of or in addition to buttons to execute a procedure – Menu items are controls with properties and events. Easy to create menus for a Windows form using the Visual Studio environment’s Menu Designer Menus will look and behave like standard Windows menus.

5-4 Defining Menus (1 of 2) MenuStrip component is added to a form. MenuStrip is a container to which ToolStripMenuItems, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added.

5-5 Defining Menus (2 of 2) The MenuStrip component appears in the component tray below the form and the Menu Designer allows you to begin typing the text for the menu items.

5-6 The Text Property Holds the words that appear on the screen — like the Text property of a button or label To conform to Windows standards, the first menu’s Text property should be File, with a keyboard access key. Use the ampersand (&) in the text to specify the key to use for keyboard access. Enter and change the Text property for each menu and menu item using the Menu Designer or make the changes in the Text property using the Properties window.

5-7 The Name Property The File menu item that is added is automatically named FileToolStripMenuItem. The items are named so well that there won’t be a need to change the Name property of any menu component. If the Text property is changed for any menu item, the item is not automatically renamed; it will need to be renamed.

5-8 The MenuStrip Items Collection ToolStripMenu Items in the collection can be displayed, reordered, added, and deleted using the Items Collection Editor.

5-9 Submenus A filled triangle to the right of the menu item indicates the existence of a submenu. Create submenus by moving to the right of a menu item and typing the next item's text.

5-10 Separator Bars Used for grouping menu items according to their purpose Visually represented as a bar across the menu To create a separator bar, add a new menu item and click on its drop-down arrow.

5-11 Menu Properties Enabled property, True/False — can be set at design or run time Checked property, False/True — can be set at design or run time Used to indicate that an option is selected Setting keyboard shortcuts Select the menu item and in Properties window for menu item, select the ShortcutKeys property. Make choice from drop-down list.

5-12 Standards for Windows Menus Follow Windows standards for applications. Include keyboard access keys. Use standards for shortcut keys, if used. Place the File menu at left end of menu bar and end File menu with the Exit command. Help, if included, is placed at right end of menu bar. File Edit View Format Help

5-13 Common Dialog Boxes Predefined standard dialog boxes for: Specifying colors and fonts Printing, opening, and saving Add appropriate Common Dialog components to display the dialog boxes that are provided as part of the Windows environment. To use a common dialog component, add the component to the form, placing it in the component tray.

5-14 Common Dialog Tools Pointer ColorDialog FontBrowserDialog FontDialog OpenFileDialog SaveFileDialog

5-15 Displaying a Windows Common Dialog Box Use ShowDialog method to display the common dialog box at run time. ShowDialog only displays the dialog. ColorDialog1.ShowDialog( ) FontDialog1.ShowDialog( )

5-16 Modal versus Modeless Windows A dialog box is said to be modal when it stays on top of the application and must be responded to. Use the ShowDialog method to display a dialog box — it is a window displayed modally. Modeless windows do not demand that there is a response. Use the Show method to display a modeless window. A modeless window can be ignored by the user.

5-17 Using the Information from the Dialog Box Code must be written to retrieve and use the choice made by the user in the common dialog box. Example Color Dialog is displayed. User selects color and clicks OK — the selected color is stored in a property that can be accessed. Color that is selected is stored in the Color property and can be assigned to another object such as a control. TitleLabel.BackColor =.ColorDialog1.Color

5-18 Setting Initial Values Before executing the ShowDialog method, assign the existing values of the object's properties that will be altered. When the dialog box appears, the current values will be selected. If the user presses Cancel, property settings for the objects will remain unchanged. FontDialog1.Font = SubTotalLabel.Font or ColorDialog1.Color =.BackColor

5-19 Creating Context Menus Shortcut menus that pop up when you right-click Items are specific to the component to which the user is pointing, reflecting options available for that component or situation. A ContextMenuStrip component is added and appears in the component tray below the form. A context menu does not have a top-level menu, only menu items. Application can have multiple context menus.

5-20 Writing General Procedures A general procedure is reusable code that can be called from multiple procedures. Useful for breaking down large sections of code into smaller units Two types A Sub Procedure performs actions. A Function Procedure performs actions AND returns a value (the return value).

5-21 Passing Arguments to Procedures Declare variable as local and pass to any called procedures —(can be module level, but it makes the variable visible to all other procedures) If a sub procedure names an argument, any call to the procedure must supply the argument. Name of the argument does not have to be the same in both locations. Number of arguments, sequence, and data type must match.

5-22 Creating a New Sub Procedure In the Editor window, enclose the lines of code with a set of Sub and End Sub statements. To use the Sub Procedure, call it from another procedure. Code in a Sub Procedure cannot be executed unless called from another procedure. Private Sub ProcedureName( ) ' Statements in the procedure. End Sub

5-23 Sub Procedure Example Private Sub SelectColor(incomingColor As Color) With ColorDialog1.Color = incomingColor.ShowDialog( ) End With End Sub Private Sub ChangeTitleButton_Click( ) Dim OriginalColor As Color OriginalColor = TitleLabel.ForeColor SelectColor(originalColor) TitleLabel.ForeColor = ColorDialog1.Color End Sub Sub Procedure Calling Procedure

5-24 Passing Arguments ByVal or ByRef ByVal — value – Sends a copy of the argument’s value; original cannot be altered. ByRef — reference – Sends a reference to the memory location where the original is stored and therefore the procedure may change the argument’s value; original can be altered If not specified, arguments are passed by value.

5-25 Writing Function Procedures In the Editor window, enclose the lines of code with Private Function( ) and End Function statements. Since the procedure returns a value, a data type for the value must be specified. To use the Function, Call it by using it in an expression. Pass arguments ByVal or ByRef. Private Function ProcedureName( ) As Datatype ' Statements to execute. End Function

5-26 Returning the Result of a Function To return a value to the calling procedure, set up a return value. The return value will be placed by VB in a variable with the SAME name as the Function's name. --OR-- Use the Return statement to return the value.

5-27 Function Example Private Sub CalculateButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles CalculateButton.Click Dim SalesDecimal As Decimal SalesDecimal = Decimal.Parse(SalesTextBox.Text) CommissionLabel.Text = Commission(SalesDecimal).ToString("C") End With End Sub Calling Procedure Private Function Commission(ByVal SalesAmountDecimal As Decimal) _ As Decimal If SalesAmountDecimal < 1000D Then Commission = 0D ElseIf SalesAmountDecimal <= 2000D Then Commission = 0.15D* SalesAmountDecimal Else Commission = 0.2D * SalesAmountDecimal End If End Function Function

5-28 Functions with Multiple Arguments Functions can receive one or more arguments (values). Sequence and data type of arguments in Call must exactly match arguments in function header. Private Function Payment(ByVal RateDecimal As Decimal, ByVal TimeDecimal As Decimal, _ ByVal AmountDecimal As Decimal) As Decimal Dim RatePerMonthDecimal As Decimal RatePerMonthDecimal = RateDecimal / 12D ' Calculate and set the return value of the function. Payment = Convert.ToDecimal((AmountDecimal * RatePerMonthDecimal) / ((1 - (1 / (1 + RatePerMonthDecimal) ^ (TimeDecimal * 12D))))) End Function

5-29 Breaking Calculations into Smaller Units Projects with many calculations are easier to understand and write if calculations are broken into small units. Each unit should perform one program function or logic block.

5-30 Writing Function Procedures In the Editor window, enclose the lines of code with Private Function( ) and End Function statements. Since the procedure returns a value, a data type for the value must be specified. To use the Function, Call it by using it in an expression. Pass arguments ByVal or ByRef. Private Function ProcedureName( ) As Datatype ' Statements to execute. End Function

5-31 New OEA Form OBJECTEVENTTASK(S) PROCEDUREPARAMETER(S)TASK(S)

5-32 OEA – Procedure Example PROCEDUREPARAMETER(S)TASK(S) Sub – Clear NoneClear input fields Clear output fields Function - CalculateItemAmount Quantity Price Calculate Item Amount (Q * P)