41
Starting Out with Visual Basic .NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Embed Size (px)

Citation preview

Page 1: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Chapter 7

Multiple Forms,

Standard Modules,

And Menus

Page 2: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

7.1Introduction

Page 3: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Chapter 7 Topics

• How to add multiple forms to a project • How to create a standard module

• To hold procedures and functions• That is not associated with a specific form

• Creating a menu system• Context menus• With commands and submenus that the user

may select from

Page 4: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

7.2Multiple Forms

Visual Basic .NET Projects May Have Multiple Forms

If One of the Forms Is the Startup Object, It Is Displayed When the Project Executes

The Other Forms Are Displayed by Programming Statements

Page 5: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Form Names

• Each Form has its specific name• This name is set/changed with the Name

Property

• Each form also has a file name (.vb)• To change the file name:

• Right click its entry in the Solution Explorer

• Choose Rename

Page 6: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Adding a New Form

• Add New Item on the tool bar

• The Add New Item dialog box appears

• Click on Windows Form under Templates

• Change the default name if you wish

• Click Open

Page 7: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Switching between Design and Code

• The Design window has the following tabs• frmMain.vb[Design] Form Design

Itself• frmError.vb[Design] Error Form

Design• frmMain.vb Main Code• frmError.vb Error Code

Page 8: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Classes and Instances

• When you create a Form at design time, you are just creating a general description of a Form(It does not actually exist at that moment)• Analogy: It is a blueprint of a Form

• An Instance must be created to actually have a Form at run time• Analogy: What the blueprint describes is

actually built

Page 9: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Creating an Instance of a Form

• It must be declared, syntax:

• For example:

• It is still not visible, but a form object has been instantiated

• Refer to it using the variable errorForm

Dim ObjectVariable As New ClassName()

Dim errorForm As New frmError()

Page 10: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Modal Form/ShowDialog Method

• When a Modal Form is displayed, no other form in the application can receive the focus until the modal form is closed

• Use the ShowDialog Method to display a modal form:

errorForm.ShowDialog()

Page 11: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Modeless Form/Show Method

• A Modeless Form allows the user to switch focus to another form in the same application

• Use the Show Method to display a modeless form:

errorForm.Show()

Page 12: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd 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

Page 13: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Hiding a Form

• Closing a Form eliminates it from memory

• To simply make it not visible, use the Hide Method:

• To bring it back use the ShowDialog() or Show() Methods

Me.Hide()

Page 14: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

More About Modal and Modeless

statement;messageForm.ShowDialog()

' Statements below will' not execute until the' Form is closed

statement;

statement;messageForm.Show()

' Statements below will' execute right after the' Form is displayed

statement;

Page 15: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

A Form's Load Event

• The Load Event is triggered just before the form is initially displayed

• If code needs to execute at this time, it must be contained in that event handling procedure:

Private Sub frmMain_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Page 16: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

A Form's Activated Event

• The Activated Event is triggered when the user switches to the from from another form or another application

• It is also triggered after the Load Event

Page 17: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

A Form's Closing Event

• This Event is triggered as the form is in the process of closing, but before it has closed

• For instance, one might want to ask the user if they really want the form closed

Page 18: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

A Form's Closed Event

• The Closed Event is triggered after a form is closed

• Note that it is now too late to prevent the Form from being closed (it is already)

Page 19: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Accessing Objects on a Different Form

• When code in a form refers to an object, it is assumed that that object is in the same form

• To refer to an object in another form, preface the object name with the variable name associated with that form:

Dim greetingForm As New frmGreeting()greetingForm.lblMessage.Text = "Hello!"greetingForm.ShowDialog()

Page 20: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Class-level Variables and Multiple Forms

• Class-level variables are Private by default

• This means they are not accessible by code in other forms

• If you wish them to be, they must be declared with the Public qualifier:

Public total As Single' Instead of the declaration' Dim total As Single

Page 21: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Procedures Have Similar Access Rules

• Procedures, by default, are Public

• They can be accessed by code outside of their Form

• To prevent this from happening, declare them to be Private

Page 22: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

7.3Standard Modules

A Standard Module Contains Code - Declarations and Procedures -

That Are Used by Other Files in a Project

Page 23: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Furthermore, Standard Modules

• Are not associated with a Form

• Contain no Event Procedures

• Are saved in files with a .vb extension

• Are used to hold code that is used by multiple Forms, or for command-line programs

Page 24: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Standard Module Syntax

• A Module will contain Sub procedures and Functions, they can be• Private - only used by functions in that Module• Public - can be called from outside of the Module

Module ModuleName[Module Contents]

End Module

Page 25: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Module Level Variables

• These are declared within a Module

• But not within Functions or Sub procedures in that Module

• If declared Dim or Private, their scope is the Module (called module scope)

• If declared Public, their scope is the application (called global scope)

Page 26: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Application with No Startup Form

• Designate a Public Sub procedure named "Main" as the startup object

• It must be in a Standard Module

• When the application starts• No Form will be displayed• "Main" will be given control

Page 27: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

7.4Menus

Visual Basic .NET 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

Page 28: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Components of a Menu System, I

Checked Menu Command

Menu NameShortcut Key

Submenu

Separator Bar

Menu Command

Page 29: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Components of a Menu System, II

• Menu Names - each drop-down menu has a name

• Menu Command - the list of actions in the drop-down list

• Shortcut Key - a key or combinations of keys to activate the command from the keyboard

Page 30: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Components of a Menu System, III

• Disabled Menu Command - a command that is not applicable is grayed out (and is not selectable)

• Checked Menu Command - some commands are toggles - checked when on, unchecked when off

Page 31: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Components of a Menu System, IV

• Submenu - a command may lead to another menu

• Separator Bar - a horizontal bar used to separate groups of commands

Page 32: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

MainMenu Control

• When used, the MainMenu control appears in the component tray (at the bottom of the Design Window)

• It is composed of MenuItem Objects, such as:• Menu Name• Menu Command• Separator Bar• Sub-Menu Command

Page 33: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

MenuItem Names

• Should begin with "mnu"

• Then by convention are spelled, specifying their hierarchical position:• mnuFile• mnuFileSave• mnuFilePrint

Page 34: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

MenuItem Text Properties

• The text property is the text that appears on the menu

• Plus if there is a access key, it is preceded with an ampersand, e.g.

Object Name Text PropertymnuFile &FilemnuFileSave &SavemnuFileExit E&xit

Page 35: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Menu Designer

• The Menu Designer speeds creation of menus with a fill in the box with text scheme:

Enter firstcommand inthe File menu

Enter thenext menuname

Page 36: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Shortcut Keys

• These are the key shortcuts that get one directly to the command action without going through the menu system(e.g., CTRL-C for Edit/Copy)

• These are set via the Shortcut Property of each menu item

Page 37: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Disabled MenuItem Objects

• The status of a menu item (enabled vs. disabled) is controlled by the Enabled property: True is Enabled

mnuEditPaste.Enabled = True

Page 38: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Adding Separator Bars

• Either make the menu item’s Text property equal to "-" (hyphen)

• Or right-click an existing menu item and select Insert Separator (it will be inserted above the menu item)

Page 39: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Inserting, Deleting, RearrangingMenu Items

• To Insert a new menu item above an existing one, right-click to use the context sensitive menu

• To Insert a new item at the end, use the Menu Designer

• To Delete a menu item, right-click/Delete• To Rearrange menu items, click and drag

them in the Menu Designer

Page 40: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Standard Menu Items

• In general follow the conventions that the majority of application menu systems use

• Specifically:• 'File' is the leftmost menu item• The File menu has an 'Exit' command• 'Help' is the rightmost menu item• The Help menu has an 'About' command

Page 41: Starting Out with Visual Basic.NET 2 nd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus

Starting Out with Visual Basic .NET 2nd Edition

Context Menus

• To establish a pop-up menu when a control is right-clicked:• Add the ContextMenu control to the component

tray• Build the menu system with the Menu Designer• Add a Click Event procedure • Then associate the menu with the control by

setting the control's ContextMenu property to the established menu