chapter 7

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

Upload: lamar-hinton

Post on 31-Dec-2015

20 views

Category:

Documents


1 download

DESCRIPTION

Chapter 7. Multiple Forms, Standard Modules, And Menus. 7.1 Introduction. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Starting Out with Visual Basic .NET 2nd Edition

Chapter 7

Multiple Forms,

Standard Modules,

And Menus

Starting Out with Visual Basic .NET 2nd Edition

7.1Introduction

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

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

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

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

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

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

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()

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()

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()

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

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()

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;

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

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

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

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)

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()

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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