Скачать презентацию Chapter 7 Multiple Forms Standard Modules And Menus Скачать презентацию Chapter 7 Multiple Forms Standard Modules And Menus

e2bd068b7b855e409f236e8e50adf678.ppt

  • Количество слайдов: 45

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

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

Chapter 7 Topics § How to add multiple forms to a project § How 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 3 Starting Out with Visual Basic 3 rd Edition

Section 7. 1 Multiple Forms Visual Basic Projects May Have Multiple Forms A Form 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 4 Starting Out with Visual Basic 3 rd Edition

Form Names § Each form has its specific name • Programs refer to a Form Names § Each form has its specific name • Programs refer to a form by this name • VB assigns a default name Form 1 to forms • A form’s Name property allows us to set or change the form name • Standard prefix form names is frm § Each form also has a file name (. vb extension) • Forms are stored on disk using this name • To change the file name: ◦ Right click in Solution Explorer, select Rename Chapter 7, Slide 5 Starting Out with Visual Basic 3 rd Edition

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

Switching from Forms to Form Code § Design window has two tabs for each Switching from Forms to Form Code § Design window has two tabs for each form • One form design • One for the code associated with a form § For two forms named frm. Main and frm. Error, can select from the following tabs: • • frm. Main. vb[Design] Main form design frm. Main. vb Main form code frm. Error. vb[Design] Error form design frm. Error. vb Error form code Chapter 7, Slide 7 Starting Out with Visual Basic 3 rd Edition

Changing the Startup Form § First form created in a project becomes the startup Changing the Startup Form § First form created in a project becomes the startup object • The form displayed when application runs § To make another form the startup object • • • Right-click project name in Solution Explorer Click Properties Click down arrow in Startup Form box Select new startup form from drop-down list Click Ok Chapter 7, Slide 8 Starting Out with Visual Basic 3 rd Edition

Classes and Instances § The form design is a class • It’s only a Classes and Instances § The form design is a class • It’s only a design or description of a form • Think of it like a blueprint ◦ A blueprint is a detailed description of a house ◦ A blueprint is not a house § The 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 9 Starting Out with Visual Basic 3 rd Edition

Creating an Instance of a Form § Dim statement used to create instance of Creating an Instance of a Form § Dim statement used to create instance of form Dim Object. Variable As New Class. Name() § To create an instance of frm. Error: Dim error. Form As New frm. Error() • frm. Error is the form design name (the class) • New frm. Error creates an instance of the form • Variable error. Form 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 Show. Dialog makes the form visible Chapter 7, Slide 10 Starting Out with Visual Basic 3 rd Edition

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

Modeless Forms & Show Method § A modeless form allows the user to change 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: error. Form. Show() • Variable error. Form represents an instance of frm. Error as shown previously • The Show method displays the form instance named error. Form as a modeless form § Can change focus to other forms in the application while error. Form remains open Chapter 7, Slide 12 Starting Out with Visual Basic 3 rd Edition

Closing a Form § A form may close itself using the Close method and Closing a Form § A form may close itself using the Close method and referring to itself as "Me": Me. Close() § As in Private Sub btn. Close_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) _ Handles btn. Close. Click Me. Close() End Sub Chapter 7, Slide 13 Starting Out with Visual Basic 3 rd Edition

Hiding a Form § Closing a Form eliminates it from memory § To retain 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: Me. Hide() § To redisplay the form use the Show. Dialog or Show method Chapter 7, Slide 14 Starting Out with Visual Basic 3 rd Edition

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

The Form Load Event § The Load event is triggered just before the form 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 frm. Main_Load(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles My. Base. Load End Sub Chapter 7, Slide 16 Starting Out with Visual Basic 3 rd Edition

The Form Activated Event § The Activated event is triggered when focus switches to 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 17 Starting Out with Visual Basic 3 rd Edition

The Form Closing Event § The Closing event is triggered as the form is 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 frm. Main_Closing(By. Val sender As Object, _ By. Val e As System. Component. Model. Cancel. Event. Args) _ Handles My. Base. Closing If Message. Box. Show(“Are you Sure? ”, “Confirm”, _ Message. Box. Buttons. Yes. No) = Dialog. Result. Yes Then e. Cancel = False ‘continue, close form Else e. Cancel = True ‘cancel form close End If End Sub Chapter 7, Slide 18 Starting Out with Visual Basic 3 rd Edition

The Form Closed Event § Closed event triggered after a form is closed § 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 19 Starting Out with Visual Basic 3 rd Edition

Using Objects on a Different Form § When code in a form refers to 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 • frm. Greeting has a control named lbl. Message • Set Text property to Hello before displaying Dim greeting. Form As New frm. Greeting() greeting. Form. lbl. Message. Text = "Hello!" greeting. Form. Show. Dialog() Chapter 7, Slide 20 Starting Out with Visual Basic 3 rd Edition

Class-level Variables in a Form § Class-level variables are Private by default § This 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 sng. Total As Single ' Instead of the declaration ' Dim sng. Total As Single Chapter 7, Slide 21 Starting Out with Visual Basic 3 rd Edition

Public/Private Procedures in a Form § Procedures, by default, are Public § They can 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 § Tutorial 7 -2 provides an opportunity to work with a multiple form application Chapter 7, Slide 22 Starting Out with Visual Basic 3 rd Edition

Section 7. 2 Standard Modules A Standard Module Contains Code - Declarations and Procedures 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 23 Starting Out with Visual Basic 3 rd Edition

Standard Modules § § A separate. vb file not associated with a form Contains 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 24 Starting Out with Visual Basic 3 rd Edition

Standard Module Syntax Module. Name [Module Contents] End Module § Module. Name is normally Standard Module Syntax Module. Name [Module Contents] End Module § Module. Name 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 Chapter 7, Slide 25 Starting Out with Visual Basic 3 rd Edition

Adding a Standard Module § Click Add New Item on the toolbar • Or Adding a Standard Module § Click Add New Item on the toolbar • Or Project on menu, then Add Module § § § Add New Item dialog box appears Click on Module under Templates Change the default name if you choose Click the Open button A new empty module now appears in: • Code window • Solution Explorer Chapter 7, Slide 26 Starting Out with Visual Basic 3 rd Edition

Module Level Variables § These are declared within a module § But outside of 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) § Tutorial 7 -3 demonstrates the use of a standard module in an application Chapter 7, Slide 27 Starting Out with Visual Basic 3 rd Edition

Application with No Startup Form § § Must change the startup form to Sub Application with No Startup Form § § Must change the startup form to Sub 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 28 Starting Out with Visual Basic 3 rd Edition

Section 7. 3 Menus Visual Basic Allows You to Create a System of Drop-down 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 29 Starting Out with Visual Basic 3 rd Edition

Components of a Menu System Menu Name Menu Command Submenu § Each drop-down menu Components of a Menu System Menu Name Menu Command Submenu § 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 30 Starting Out with Visual Basic 3 rd Edition

Components of a Menu System Shortcut Key (F 7) Access Key (L) § Commands Components of a Menu System Shortcut Key (F 7) Access Key (L) § 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 Chapter 7, Slide 31 Starting Out with Visual Basic 3 rd Edition

Components of a Menu System Checked Menu Command Separator Bar § A disabled menu Components of a Menu System Checked Menu Command Separator Bar § 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 Chapter 7, Slide 32 Starting Out with Visual Basic 3 rd Edition

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

Menu. Item Object Names § Should begin with mnu § Then by convention are Menu. Item Object Names § Should begin with mnu § Then by convention are spelled, specifying their hierarchical position: • mnu. File. Save • mnu. File. Print Chapter 7, Slide 34 Starting Out with Visual Basic 3 rd Edition

Menu. Item Text Properties § The text property holds the item description displayed to Menu. Item 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 Name Text Property mnu. File &File F mnu. File. Save &Save. S E&xit X Chapter 7, Slide 35 Access Key mnu. File. Exit Starting Out with Visual Basic 3 rd Edition

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

Shortcut Keys § Keyboard based shortcuts that execute menu commands without using the menu 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 Show. Shortcut property is set to true Chapter 7, Slide 37 Starting Out with Visual Basic 3 rd Edition

Disabled Menu. Item Objects § A menu item is grayed out (disabled) with the Disabled Menu. Item 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 mnu. Edit. Paste. Enabled = False • Following a cut or copy, Paste is enabled mnu. Edit. Paste. Enabled = True Chapter 7, Slide 38 Starting Out with Visual Basic 3 rd Edition

Adding Separator Bars § Right-click menu item, select Insert Separator • Separator inserted above 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 39 Starting Out with Visual Basic 3 rd Edition

Submenus § When selecting a menu item in the designer, a Type Here box 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 rightpointing arrow will be shown for this item Chapter 7, Slide 40 Starting Out with Visual Basic 3 rd Edition

Inserting, Deleting, & Rearranging § To insert a new menu item within the list 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 41 Starting Out with Visual Basic 3 rd Edition

Menu. Item Click Event Procedures § Menus and submenus require no code § Commands Menu. Item 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 Menu. Item object named mnu. File. Exit to create the following Private Sub mnu. File. Exit_Click(By. Val sender as System. Object, _ By. Val e as System. Event. Args) Handles mnu. File. Exit. Click Me. Close() End Sub Chapter 7, Slide 42 Programmer supplied code Click event procedure created by VB Starting Out with Visual Basic 3 rd Edition

Standard Menu Items § In general follow the conventions that most application menu systems 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 43 Starting Out with Visual Basic 3 rd Edition

Context Menus § A pop-up menu that appears on a right-click § Context menus 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 Context. Menu control in the Tool. Box to add it to the component tray • Build menu system using Menu Designer • Build Click event procedures as needed • Use Context. Menu property of form controls to link desired control(s) to the menu Chapter 7, Slide 44 Starting Out with Visual Basic 3 rd Edition

Section 7. 4 The High Adventure Travel Agency Price Quote Application Build an application 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 45 Starting Out with Visual Basic 3 rd Edition