STARTING OUT WITH Visual Basic 2008 FOURTH EDITION
STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis Haywood Community College Kip Irvine Florida International University Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 1
Chapter 7 Multiple Forms, Standard Modules, And Menus Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction n How to add multiple forms to a project How to create a standard module n Holds procedures and functions not associated with a specific form Creating a menu system n Context menus n With commands and submenus that the user may select from Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 3
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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Form Names n Each form has Name property n Programs refer to a form by this name n VB assigns name Form 1 n n n Name property allows us to change form name Standard prefix is frm Each form also has a file name (. vb extension) n Forms are stored on disk using this name n Right click in Solution Explorer, and select Rename to change the file name Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 5
Adding a New Form to a Project n n n Click Add New Item on the toolbar n Or Project on menu, then Add Windows Form Add New Item dialog box appears Click on Windows Form Change the default name Click the Add button New form now appears in: n Design window n Solution Explorer Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 6
Switching from Forms to Form Code n n Design window has two tabs for each form n One form design n One for the code associated with a form If you have two forms frm. Main & frm. Error, you may select these tabs: n Error form design n Error form code n Main form design n Main form code Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 7
Removing a Form n n n A form may also be removed from a project To remove a form and delete its file from disk: n Right-click on the form in Solution Explorer n Click Delete on the pop-up menu To remove a form but leave its file on disk: n Right-click on the form in Solution Explorer n Click Exclude from Project on the pop-up menu Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 8
Changing the Startup Form n n First form in a project becomes startup object n Form displayed when application starts Right-click project in Solution Explorer to change startup form n Click Properties n Click down arrow in Startup Form box n Select new startup form from list n Click Ok Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 9
Classes and Instances n The form design is a class n It’s only a design or description of a form n Think of it like a blueprint n n 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 n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 10
Creating an Instance of a Form n Dim statement creates an instance of a form Dim Object. Variable As New Class. Name() n To create an instance of frm. Error: Dim error. Form As New frm. Error() n frm. Error is the form design name (the class) n New frm. Error creates an instance of the form Variable error. Form refers to the form in RAM n error. Form used to perform actions on the form The form is not yet visible, but it now exists Show or Show. Dialog makes the form visible n n n Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 11
Modal Forms & Show. Dialog Method n n 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 n 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 n n Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 12
Modeless Forms & Show Method n n A modeless form allows the user to change focus at will to another form in the application while the modeless form remains open For example: error. Form. Show() Variable error. Form represents an instance of frm. Error as shown previously n 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 n n Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 13
Closing a Form n A form may close itself using the Close method and referring to itself using the keyword "Me": Me. Close() n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 14
Hiding a Form n n Closing a Form removes it from memory To retain the form in memory but remove it from the display, use the Hide Method: Me. Hide() n To redisplay a hidden form use the Show. Dialog or Show method Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 15
More on Modal and Modeless Forms n 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; n Display of a modeless form allows execution to continue statement; message. Form. Show() ' Statements below will execute ‘ immediately after Form is displayed statement; n Tutorial 7 -1 demonstrates these differences Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 16
The Form Load Event n n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 17
The Form Activated Event n n 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 n Occurs immediately after the Load event The Activated event may be triggered many more times, each time focus shifts back to the form Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 18
Creating an Activated Event Handler n n Create an Activated event handler by selecting frm. Main events from the class name drop-down list Then select the Activated Event from the method name drop-down list Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 19
The Form Closing Event n n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 20
The Form Closed Event n n Closed event triggered after a form is closed Note that it is now too late to prevent the form from being closed n n Form is already closed when event fires Create the Closing and Closed events in the same way as the Activated event n Click the class name drop-down list Select formname Events n Click desired event from the method drop-down list n Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 21
Using Objects on a Different Form n n 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 n Simply preface the object name with the variable name associated with that form n frm. Greeting has a control named lbl. Message n Set Text property to Hello before displaying Dim greeting. Form As New frm. Greeting() greeting. Form. lbl. Message. Text = "Hello!" greeting. Form. Show. Dialog() Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 22
Class-level Variables in a Form n n n Class-level variables are Private by default Private variables are not accessible by code in other forms To gain access to variables from other forms, a variable must be declared as: n Class level n Public sng. Total As Single ' Instead of the declaration ' Dim sng. Total As Single Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 23
Public/Private Procedures in a Form n n Procedures, by default, are Public They can be accessed by code outside 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 24
7. 2 Standard Modules A Standard Module Contains Code - Declarations and Procedures That Are Used by Other Files in a Project Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Standard Modules n n n A separate. vb file not associated with a form Contains no Event Procedures Used for code to be shared by multiple forms Procedures, functions, or variables used in one form should be declared in that form Procedures, functions, or variables used by many forms may be declared in a standard module Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 26
Standard Module Syntax Module. Name [Module Contents] End Module n n n Module. Name is normally same as. vb file Module Contents are sub procedures and functions which can be n Private - used only by procedures or functions in that module n Public - can be called from anywhere in your Visual Studio project If not specified, a procedure is public Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 27
Adding a Standard Module n n n Click Add New Item on the toolbar n 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 Add button A new empty module now appears in: n Code window n Solution Explorer Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 28
Module Level Variables n n n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 29
Application with No Startup Form n n 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 n No Form will be displayed n Main will be given control Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 30
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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Components of a Menu System n n n Each drop-down menu has a menu name Each drop-down menu has a list of actions or menu commands that can be performed Some commands may lead to a submenu Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 32
Components of a Menu System n n n Actions may be performed using a key or key combination called a shortcut key A checked menu command toggles between the checked (if on) and unchecked (if off) states A separator bar helps group similar commands Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 33
Menu. Strip Control n n n A Menu. Strip control adds a menu to a form n Double-click on the Menu. Strip icon in the Menus & Toolbars section of the Toolbox The Menu. Strip control is displayed in the component tray (bottom of Design window) A Menu. Strip can have many Tool. Strip. Menu. Item objects: n Each represents a single menu command n Name property - used by VB to identify it n Text property – text displayed to the user Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 34
Tool. Strip. Menu. Item Object Names n n Should begin with mnu Then by convention are named based on their text property and position in the menu hierarchy n mnu. File. Save n mnu. File. Print n mnu. File. Exit Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 35
Tool. Strip. Menu. Item Text Properties n n The text property holds the menu item description that is displayed to the user If an access key is assigned, that letter must be preceded with an ampersand Object Name Access Key mnu. File. Save F S &File &Save mnu. File. Print mnu. File. Exit P X &Print E&xit Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Text Property Slide 7 - 36
Menu Designer n The Menu Designer allows visual menu creation by filling in boxes with the menu text: Enter first command in the File menu Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Enter the next menu name Slide 7 - 37
Shortcut Keys n n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 38
Checked Menu Items n n n Some menu items just turn a feature on or off n For example, an alarm for a clock To create a checked menu item: n Set Check. On. Click property to true n Set Checked property to True if feature should be on when the form is initially displayed Can test a checked menu item in code If mnu. Settings. Alarm. Checked = True Then Message. Box. Show(“Wake UP!”) End If Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 39
Disabled Menu Items n n 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 n Code initially disables the Paste option mnu. Edit. Paste. Enabled = False n Following a cut or copy, Paste is enabled mnu. Edit. Paste. Enabled = True Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 40
Adding Separator Bars n n Right-click menu item, select Insert Separator n Separator inserted above the menu item Or create a menu item with one hyphen (-) as the text property Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 41
Submenus n n When selecting a menu item in the designer, a Type Here box appears to the right n 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 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 42
Inserting, Deleting, & Rearranging n n To insert a new menu item within the list n Right-click the item to follow the new one n Select Insert then Menu. Item from pop-up menu Use Menu Designer to add new menu items at the end by entering the text to appear To remove a menu item n Right-click on the item n Choose Delete from the pop-up menu The Menu Designer can rearrange items using a click and drag approach Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 43
Tool. Strip. Menu. Item Click Events n n n Menus and submenus require no code Commands must have a click event procedure n Double click on the menu item n Event procedure created in the code window n Programmer supplies the code to execute Double click the menu item object mnu. File. Exit, then add a Me. Close command as shown below 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 Programmer supplied code Click event procedure created by VB Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 44
Standard Menu Items n n In general follow the conventions that most application menu systems use n File is leftmost item with access key Alt-F n File item has Exit command, access key Alt-X n Help is the rightmost item n Help menu has an About command Tutorial 7 -4 demonstrates how to create a menu system Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 45
Context Menus n n n 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: n Double-click Context. Menu. Strip control in the Tool. Box to add it to the component tray n Build menu system using Menu Designer n Build Click event procedures as needed n Use Context. Menu. Strip property of form controls to link desired control(s) to the menu Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 46
7. 4 The High Adventure Travel Agency Price Quote Application Build an application with multiple forms, a standard module, and a menu system Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
High Adventure Travel Main Form Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 48
High Adventure Travel Scuba Form Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 49
High Adventure Travel Sky Diving Form Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 50
High Adventure Travel Spelunking Form Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7 - 51
- Slides: 51