Slide 1 : Multiple Forms,
Standard Modules,
And Menus
Introduction : Introduction
Chapter Topics : Chapter 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
Multiple Forms : Multiple 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
Form Names : 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
Adding a New Form : 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
Switching between Design and Code : 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
Classes and Instances : Classes and Instances When you create a Form, 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
Analogy: What the blueprint describes is actually built
Creating an Instance of a Form : Creating an Instance of a Form It must be declared, syntax:
For example:
It is still not visible, but it is there now
Refer to it using the variable errorForm Dim ObjectVariable As New ClassName() Dim errorForm As New frmError()
Modal Form/ShowDialog Method : 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()
Modeless Form/Show Method : 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()
Closing a Form : 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
Hiding a Form : 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()
More About Modal and Modeless : 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;
A Form's Load Event : 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
A Form's Activated Event : 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
A Form's Closing Event : 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
A Form's Closed Event : 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)
Accessing Objects on a Different Form : 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()
Class-level Variables and Multiple Forms : 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: Private total As Single
' Instead of the declaration
' Dim total As Single
Procedures Have Similar Access Rules : 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
Standard Modules : Standard Modules A Standard Module Contains Code - Declarations and Procedures - That Are Used by Other Files in a Project
Furthermore, Standard Modules : 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
Standard Module Syntax : 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
Module Level Variables : 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)
Application with No Startup Form : 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