Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis Haywood Community College Kip Irvine Florida International University

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Multiple Forms, Standard Modules, And Menus 7

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 3 Introduction 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

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Multiple Forms 7.1 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

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 5 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 Standard 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

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 6 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 Change the default name Click the Add button New form now appears in: Design window Solution Explorer

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 7 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 If you have two forms frmMain & frmError, you may select these tabs: Error form design Error form code Main form design Main form code

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Removing a Form A form may also be removed from a project To remove a form and delete its file from disk: Right-click on the form in Solution Explorer Click Delete on the pop-up menu To remove a form but leave its file on disk: Right-click on the form in Solution Explorer Click Exclude from Project on the pop-up menu Slide 7- 8

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 Slide 7- 9 Changing the Startup Form

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 10 Classes and Instances The form design is a class Its 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

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 11 Creating an Instance of a Form Dim statement creates an instance of a 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 in RAM errorForm used to perform actions 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()

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 12 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()

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 13 Modeless Forms & Show Method 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: 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()

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 14 Closing a Form A form may close itself using the Close method and referring to itself using the keyword "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

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 15 Hiding a Form Closing a Form removes it from memory To retain the form in memory but remove it from the display, use the Hide Method: To redisplay a hidden form use the ShowDialog or Show method Me.Hide()

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 16 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 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 immediately after Form is displayed statement;

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 17 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

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 18 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, each time focus shifts back to the form

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Creating an Activated Event Handler Slide 7- 19 Create an Activated event handler by selecting frmMain events from the class name drop-down list Then select the Activated Event from the method name drop-down list

20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 20 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 = Falsecontinue, close form Else e.Cancel = Truecancel form close End If End Sub

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 21 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 Form is already closed when event fires Create the Closing and Closed events in the same way as the Activated event Click the class name drop-down list Select formname Events Click desired event from the method drop-down list

22 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 22 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()

23 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 23 Class-level Variables in a Form 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: Class level Public Public sngTotal As Single ' Instead of the declaration ' Dim sngTotal As Single

24 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 24 Public/Private Procedures in a Form 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

25 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Standard Modules 7.2 A Standard Module Contains Code - Declarations and Procedures - That Are Used by Other Files in a Project

26 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 26 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, 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

27 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 27 Standard Module Syntax ModuleName is normally same as.vb file Module Contents are sub procedures and functions which can be Private - used only by procedures or functions in that module Public - can be called from anywhere in your Visual Studio project If not specified, a procedure is public Module ModuleName [Module Contents] End Module

28 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 28 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 Add button A new empty module now appears in: Code window Solution Explorer

29 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 29 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

30 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 30 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

31 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Menus 7.3 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

32 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 32 Components of a Menu System 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

33 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 33 Components of a Menu System 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

34 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 34 MenuStrip Control A MenuStrip control adds a menu to a form Double-click on the MenuStrip icon in the Menus & Toolbars section of the Toolbox The MenuStrip control is displayed in the component tray (bottom of Design window) A MenuStrip can have many ToolStripMenuItem objects: Each represents a single menu command Name property - used by VB to identify it Text property – text displayed to the user

35 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 35 ToolStripMenuItem Object Names Should begin with mnu Then by convention are named based on their text property and position in the menu hierarchy mnuFile mnuFileSave mnuFilePrint mnuFileExit

36 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 36 ToolStripMenuItem Text Properties 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 NameAccess KeyText Property mnuFileF&File mnuFileSaveS&Save mnuFilePrintP&Print mnuFileExitXE&xit

37 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 37 Menu Designer The Menu Designer allows visual menu creation by filling in boxes with the menu text: Enter first command in the File menu Enter the next menu name

38 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 38 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

39 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Checked Menu Items Some menu items just turn a feature on or off For example, an alarm for a clock To create a checked menu item: Set CheckOnClick property to true 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 mnuSettingsAlarm.Checked = True Then MessageBox.Show(Wake UP!) End If Slide 7- 39

40 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 40 Disabled Menu Items 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

41 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 41 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

42 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 42 Submenus When selecting a menu item in the designer, a Type Here box appears to the 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

43 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 43 Inserting, Deleting, & Rearranging To insert a new menu item within the list Right-click the item to follow the new one Select Insert then MenuItem 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 Right-click on the item Choose Delete from the pop-up menu The Menu Designer can rearrange items using a click and drag approach

44 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 44 ToolStripMenuItem Click Events Menus and submenus require no code Commands must have 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 mnuFileExit, then add a Me.Close command as shown below 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

45 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 45 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

46 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 46 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 ContextMenuStrip control in the ToolBox to add it to the component tray Build menu system using Menu Designer Build Click event procedures as needed Use ContextMenuStrip property of form controls to link desired control(s) to the menu

47 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The High Adventure Travel Agency Price Quote Application 7.4 Build an application with multiple forms, a standard module, and a menu system

48 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley High Adventure Travel Main Form Slide 7- 48

49 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley High Adventure Travel Scuba Form Slide 7- 49

50 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley High Adventure Travel Sky Diving Form Slide 7- 50

51 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley High Adventure Travel Spelunking Form Slide 7- 51


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

Similar presentations


Ads by Google