Chapter 7, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Multiple Forms, Standard Modules, And Menus
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.
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.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 7 Multiple Forms, Modules,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Creating Menu. Objectives Create a menu system for a form –Create a menu –Create a menu titles –Create a menu items –Create a submenu –Modify menu –Edit.
Copyright © 2014 Pearson Education, Inc. Chapter 7 Multiple Forms, Modules, and Menus.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
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.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Multiple Forms, Standard Modules, And Menus
Multiple Forms and Standard Modules
Using Arrays and File Handling
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.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
CHAPTER 9 Introducing Microsoft Office Learning Objectives Start Office programs and explore common elements Use the Ribbon Work with files Use.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
Productivity Programs Common Features and Commands.
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.
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.
Introduction to Visual Basic.NET Your First Visual Basic.NET Application.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
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 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
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.
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.
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 Using Document Collaboration, Integration, and Charting Tools Microsoft Word 2013.
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Multiple Forms and Menus
Visual Basic Fundamental Concepts
PictureBox, MessageBox, Multiple Forms, Splash Screens and Timers
Chapter 2: The Visual Studio .NET Development Environment
Benchmark Series Microsoft Word 2010 Level 1
Apply Procedures to Develop Menus, List Box and Combo Box Objects
How to design a Windows Forms application
Using Procedures and Exception Handling
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Variables and Arithmetic Operations
Visual Basic..
Chapter 6 Multiform Projects
Multiple Forms, Modules, and Menus
Lecture Set 11 Creating and Using Classes
Microsoft Office Access 2003
Brief description on how to navigate within this presentation (ppt)
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Presentation transcript:

Chapter 7, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Chapter 7, Slide 2Starting Out with Visual Basic 3 rd Edition Chapter 7 Introduction

Chapter 7, Slide 3Starting Out with Visual Basic 3 rd Edition Chapter 7 Topics  How to add multiple forms to a project  How to create a standard module Holds procedures and functions not associated with a specific form  Creating a menu system Context menus With commands and submenus that the user may select from

Chapter 7, Slide 4Starting Out with Visual Basic 3 rd Edition Section 7.1 Multiple Forms Visual Basic Projects May Have Multiple Forms A Form Designated as the Startup Object Is Displayed When the Project Executes Other Forms in a Project Are Displayed by Programming Statements

Chapter 7, Slide 5Starting Out with Visual Basic 3 rd Edition Form Names  Each form has Name property Programs refer to a form by this name VB assigns name Form1 Name property allows us to change form name Form name prefix is frm  Each form also has a file name (.vb extension) Forms are stored on disk using this name Right click in Solution Explorer, and select Rename to change the file name

Chapter 7, Slide 6Starting Out with Visual Basic 3 rd Edition  Click Add New Item on the toolbar Or Project on menu, then Add Windows Form  Add New Item dialog box appears  Click Windows Form  Change default name  Click Add button  New form appears in: Design window Solution Explorer Adding a New Form to a Project

Chapter 7, Slide 7Starting Out with Visual Basic 3 rd Edition Switching from Forms to Form Code  Design window has two tabs for each form One for form design One for the code associated with a form  For two forms named frmMain & frmError, can select from these tabs: Error form design Main form design Main form code Error form code

Chapter 7, Slide 8Starting Out with Visual Basic 3 rd Edition Changing the Startup Form  First form in a project becomes startup object Form displayed when application starts  Right-click project in Solution Explorer to change startup form Click Properties Click down arrow in Startup Form box Select new startup form from list Click Ok

Chapter 7, Slide 9Starting Out with Visual Basic 3 rd Edition Classes and Instances  The form design is a class It’s a design of a form but not an actual form Think of it like a blueprint ◦ A blueprint is a detailed design of a house ◦ But a blueprint is not a house  A form design can be used to create one or more instances of the form Like building a house from the blueprint  In order to use a form in a program, we must first create an instance of it from the design

Chapter 7, Slide 10Starting Out with Visual Basic 3 rd Edition Creating an Instance of a Form  Dim statement used to create instance of form  To create an instance of frmError: frmError is the form design name (the class) New frmError creates an instance of the form Variable errorForm refers to the form instance and is used to perform operations on the form  The form is not yet visible, but it now exists  Show or ShowDialog makes the form visible Dim ObjectVariable As New ClassName() Dim errorForm As New frmError()

Chapter 7, Slide 11Starting Out with Visual Basic 3 rd Edition Modal Forms & ShowDialog Method  A modal form prevents the user from changing focus to another form in the application as long as it remains open  For example: Variable errorForm represents an instance of frmError as shown in the previous slide The ShowDialog method displays the form instance named errorForm as a modal form  Must close errorForm in order to change focus to another form in the application errorForm.ShowDialog()

Chapter 7, Slide 12Starting Out with Visual Basic 3 rd Edition Modeless Forms & Show Method  A modeless form allows the user to change focus at will to another form in the application while that form remains open  For example: Variable errorForm represents an instance of frmError as shown previously The Show method displays the form instance named errorForm as a modeless form  Can change focus to other forms in the application while errorForm remains open errorForm.Show()

Chapter 7, Slide 13Starting Out with Visual Basic 3 rd Edition Closing a Form  A form may close itself using the Close method and referring to itself as "Me":  As in Me.Close() Private Sub btnClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnClose.Click Me.Close() End Sub

Chapter 7, Slide 14Starting Out with Visual Basic 3 rd Edition Hiding a Form  Closing a Form eliminates it from memory  To retain the form in memory but remove it from the display, use the Hide Method:  To redisplay the form use the ShowDialog or Show method Me.Hide()

Chapter 7, Slide 15Starting Out with Visual Basic 3 rd Edition More on Modal and Modeless Forms statement; messageForm.ShowDialog() ' Statements below will ' not execute until the ' Form is closed statement; messageForm.Show() ' Statements below will ' execute right after the ' Form is displayed statement;  Display of a modal form causes execution of calling statements to halt until form is closed  Display of a modeless form allows execution to continue  Tutorial 7-1 demonstrates these differences

Chapter 7, Slide 16Starting Out with Visual Basic 3 rd Edition Modal and Modeless Form Example  Tutorial 7-1 demonstrates a key difference between modal and modeless forms  When a modeless form is displayed, execution in the calling program continues For…Next loop following Show method executes, adding a sequence of numbers to the list box with AnotherForm still open

Chapter 7, Slide 17Starting Out with Visual Basic 3 rd Edition Modal and Modeless Form Example  When a modal form is displayed, execution is suspended in the calling program  Calling program execution resumes with For…Next loop when called form is closed

Chapter 7, Slide 18Starting Out with Visual Basic 3 rd Edition The Form Load Event  The Load event is triggered just before the form is initially displayed  Any code needed to prepare the form prior to display should be in the Load event  If some controls should not be visible initially, set their Visible property in the Load event  Double click on a blank area of the form to set up a Load event as shown below Private Sub frmMain_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load End Sub

Chapter 7, Slide 19Starting Out with Visual Basic 3 rd Edition The Form Activated Event  The Activated event is triggered when focus switches to the form from another form or application  The Load event is triggered once when the form is initially displayed  The Activated event is also triggered when the form is initially displayed Occurs immediately after the Load event  The Activated event may be triggered many more times while a form is being displayed

Chapter 7, Slide 20Starting Out with Visual Basic 3 rd Edition Creating an Activated Event Handler  Create an Activated event handler by selecting form events from class name drop-down list  Then select the Activated Event from the method name drop-down list

Chapter 7, Slide 21Starting Out with Visual Basic 3 rd Edition The Form Closing Event  The Closing event is triggered as the form is being closed, but before it has closed  The Closing event can be used to ask the user if they really want the form closed Private Sub frmMain_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles MyBase.Closing If MessageBox.Show(“Are you Sure?”, “Confirm”, _ MessageBoxButtons.YesNo) = DialogResult.Yes Then e.Cancel = False‘continue, close form Else e.Cancel = True‘cancel form close End If End Sub

Chapter 7, Slide 22Starting Out with Visual Basic 3 rd Edition The Form Closed Event  Closed event triggered after a form is closed  Note that it is now too late to prevent the form from being closed (it is already)

Chapter 7, Slide 23Starting Out with Visual Basic 3 rd Edition Using Objects on a Different Form  When code in a form refers to an object, it is assumed that object is in that same form  You can refer to an object in another form Simply preface the object name with the variable name associated with that form frmGreeting has a control named lblMessage Set Text property to Hello before displaying Dim greetingForm As New frmGreeting() greetingForm.lblMessage.Text = "Hello!" greetingForm.ShowDialog()

Chapter 7, Slide 24Starting Out with Visual Basic 3 rd Edition Class-level Variables in a Form  Class-level variables are Private by default  This means they are not accessible by code in other forms  If you want to access from other forms, they must be declared with the Public qualifier: Public sngTotal As Single ' Instead of the declaration ' Dim sngTotal As Single

Chapter 7, Slide 25Starting Out with Visual Basic 3 rd Edition Public/Private Procedures in a Form  Procedures, by default, are Public  They can be accessed by code outside of their Form  To make a procedure invisible outside its own form, declare it to be Private

Chapter 7, Slide 26Starting Out with Visual Basic 3 rd Edition Multiple Form Tutorial  Tutorial 7-2 shows how to use multiple forms  Select course & professor for each time block  The Show Schedule button then displays the schedule in a new form

Chapter 7, Slide 27Starting Out with Visual Basic 3 rd Edition Section 7.2 Standard Modules A Standard Module Contains Code - Declarations and Procedures - That Are Used by Other Files in a Project

Chapter 7, Slide 28Starting Out with Visual Basic 3 rd Edition Standard Modules  A separate.vb file not associated with a form  Contains no Event Procedures  Used for code to be shared by multiple forms  Procedures or variables used by one form should be declared in that form  Procedures or variables used by many forms should be declared in a standard module

Chapter 7, Slide 29Starting Out with Visual Basic 3 rd Edition Standard Module Syntax  ModuleName is normally same as.vb file  Module Contents are sub procedures and functions which can be Private - only used by functions in that module Public - can be called from outside of the module  If not specified, a procedure is public Module ModuleName [Module Contents] End Module

Chapter 7, Slide 30Starting Out with Visual Basic 3 rd Edition Adding a Module to a Project  Click Add New Item on the toolbar Or Project on menu, then Add Module  Add New Item dialog box appears  Click Module  Change default name  Click Add button  New module found in: Code window Solution Explorer

Chapter 7, Slide 31Starting Out with Visual Basic 3 rd Edition Module Level Variables  These are declared within a module  But outside of any functions or sub procedures in that module  If declared Dim or Private, the scope is the module (called module scope)  If declared Public, the scope is the entire application (called global scope)

Chapter 7, Slide 32Starting Out with Visual Basic 3 rd Edition Module Level Function Tutorial  Tutorial 7-3 demonstrates the use of a function in a standard module  Two forms provided: Public Ticket Sales Student Ticket Sales  Both forms require logic to compute sales tax  The two forms can share sales tax logic if placed in a module level function

Chapter 7, Slide 33Starting Out with Visual Basic 3 rd Edition Application with No Startup Form  Must change the startup form to Sub Main  Main must be a public Sub procedure  It must be in a standard module  When the application starts No Form will be displayed Main will be given control

Chapter 7, Slide 34Starting Out with Visual Basic 3 rd Edition Section 7.3 Menus Visual Basic Allows You to Create a System of Drop-down Menus for Any Form in Your Application You Use the Menu Designer to Create a Menu System

Chapter 7, Slide 35Starting Out with Visual Basic 3 rd Edition Components of a Menu System Menu Name Submenu Menu Command  Each drop-down menu has a menu name  Each drop-down menu has a list of actions or commands that can be performed  Some commands may lead to a submenu

Chapter 7, Slide 36Starting Out with Visual Basic 3 rd Edition Components of a Menu System  Commands can be performed by A key or key combination called a shortcut key A single letter and the Alt key called an access key  Menu must be open to use an access key  Shortcut key works when menu is not displayed Shortcut Key (F7) Access Key (L)

Chapter 7, Slide 37Starting Out with Visual Basic 3 rd Edition Components of a Menu System  A disabled menu command can’t be selected at present and shows as a light color (grayed out)  A checked menu command toggles between the checked (if on) and unchecked (if off) states  A separator bar helps group similar commands Checked Menu Command Separator Bar

Chapter 7, Slide 38Starting Out with Visual Basic 3 rd Edition MainMenu Control  Add to form with double-click on MainMenu control in Toolbox Menus & Toolbars section  The MainMenu control is displayed in the component tray (bottom of Design window)  May have many MenuItem objects with the following key properties: MenuItem name used by VB to identify it MenuItem text displayed to the user Actions in the form of a MenuItem click event Submenu Separator bar

Chapter 7, Slide 39Starting Out with Visual Basic 3 rd Edition MenuItem Object Names  Should begin with mnu  Then by convention are spelled, specifying their hierarchical position: mnuFile mnuFileSave mnuFilePrint

Chapter 7, Slide 40Starting Out with Visual Basic 3 rd Edition MenuItem Text Properties  The text property holds the item description displayed to the user  If an access key is assigned, that letter must be preceded with an ampersand Object NameText PropertyAccess Key mnuFile&FileF mnuFileSave&SaveS mnuFileExitE&xitX

Chapter 7, Slide 41Starting Out with Visual Basic 3 rd Edition Menu Designer  The Menu Designer allows menu creation by filling in a box with the menu text: Menu Under ConstructionMenu Properties Enter first command in the File menu Enter the next menu name

Chapter 7, Slide 42Starting Out with Visual Basic 3 rd Edition Shortcut Keys  Keyboard based shortcuts that execute menu commands without using the menu system  For example, ctrl-c to Copy to the clipboard  These are set via the Shortcut property of each menu item  A shortcut is displayed to the user only if the ShowShortcut property is set to true

Chapter 7, Slide 43Starting Out with Visual Basic 3 rd Edition Disabled MenuItem Objects  A menu item is grayed out (disabled) with the Enabled property  Paste option is initially disabled and only enabled after something is cut or copied Code initially disables the Paste option Following a cut or copy, Paste is enabled mnuEditPaste.Enabled = True mnuEditPaste.Enabled = False

Chapter 7, Slide 44Starting Out with Visual Basic 3 rd Edition Adding Separator Bars  Right-click menu item, select Insert Separator Separator inserted above the menu item  Or create a menu item with one hyphen (-) as the text property

Chapter 7, Slide 45Starting Out with Visual Basic 3 rd Edition Submenus  When selecting a menu item in the designer, a Type Here box appears to its right Begin a submenu by setting up this menu item  If a menu item has a submenu, a solid right- pointing arrow will be shown for this item

Chapter 7, Slide 46Starting Out with Visual Basic 3 rd Edition Inserting, Deleting, & Rearranging  To insert a new menu item within the list Right-click the item to follow the new one Choose Insert New from the shortcut menu  Use Menu Designer to add new menu items at the end by entering the text to appear  To remove a menu item Right-click on the item Choose Delete from the shortcut menu  The Menu Designer can rearrange items using a click and drag approach

Chapter 7, Slide 47Starting Out with Visual Basic 3 rd Edition MenuItem Click Event Procedures  Menus and submenus require no code  Commands require a click event procedure Double click on the menu item Event procedure created in the code window Programmer supplies the code to execute  Double click the MenuItem object named mnuFileExit to create the following Private Sub mnuFileExit_Click(ByVal sender as System.Object, _ ByVal e as System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Programmer supplied code Click event procedure created by VB

Chapter 7, Slide 48Starting Out with Visual Basic 3 rd Edition Standard Menu Items  In general follow the conventions that most application menu systems use File is leftmost item with access key Alt-F File item has Exit command, access key Alt-X Help is the rightmost item Help menu has an About command  Tutorial 7-4 demonstrates how to create a menu system

Chapter 7, Slide 49Starting Out with Visual Basic 3 rd Edition Context Menus  A pop-up menu that appears on a right-click  Context menus are designed for a particular control or set of controls  To set up a Context Menu: Double-click ContextMenu control in the ToolBox to add it to the component tray Build menu system using Menu Designer Build Click event procedures as needed Use ContextMenu property of form controls to link desired control(s) to the menu

Chapter 7, Slide 50Starting Out with Visual Basic 3 rd Edition Section 7.4 The High Adventure Travel Agency Price Quote Application Build an application with multiple forms, a standard module, and a menu system

Chapter 7, Slide 51Starting Out with Visual Basic 3 rd Edition High Adventure Travel Main Form

Chapter 7, Slide 52Starting Out with Visual Basic 3 rd Edition High Adventure Travel Scuba Form

Chapter 7, Slide 53Starting Out with Visual Basic 3 rd Edition High Adventure Travel Sky Diving Form

Chapter 7, Slide 54Starting Out with Visual Basic 3 rd Edition High Adventure Travel Spelunking Form