Upload
brittney-barton
View
217
Download
0
Embed Size (px)
Citation preview
Chapter 7 - Part 1 2
Form Names and File Names
Default Name Property: Form1 Default File Name: Form1.vb
Code stored in the .vb file Should assign descriptive form names for
multiple-form programs Need to do more than change the
Name property: Also change file name Also change Startup Object
Chapter 7 - Part 1 5
Adding a Form
If program crashes because it can’t find a form, you might need to add it again:
1. Choose Project | Add Existing Item or click and click Add Existing Item orpress Shift + Alt + A.
2. Choose .vb filename.
3. Click Open.
Chapter 7 - Part 1 6
Removing a Form
Right-click form in the Solution Explorer window Choose Delete from the shortcut menu to
remove the form from the project and delete the .vb file.
Choose Exclude From Project to remove the .vb file from the project but not delete it.
Chapter 7 - Part 1 7
Startup Object (p. 405)
Right-click Project name (in Solution Explorer window) and choose Properties.…or…
Choose Project | Properties.
Chapter 7 - Part 1 8
Startup Object
Click Startup object drop-down arrow and choose form name to be first object (form) that appears.
Chapter 7 - Part 1 10
Switching Between Forms
Active FormCode windowfor frmStates form
frmstates form
Chapter 7 - Part 1 11
Form File Contents
Coding Class declaration
Program structure describing object’s properties and methods.
All objects (such as form, list box, etc.) have properties (such as Name) and methods (such as AddItem).
•Name stored in form’s Name property
•Code between Public Class and End Class belong to class declaration for form.
Chapter 7 - Part 1 12
The Class Declaration…
Does not create the form. Describes the form. Acts like a house blueprint.
Describes the form (house). Used to build an instance of the form (house).
(see illustrations on p. 406). Creates 1+ instances of the form; use
instances to display form onscreen. Create class at Design time.
Instantiate class at Run time.
Chapter 7 - Part 1 13
Instance of Form
1st step to display form: create an instance.Dim ObjectVariable As New ClassName()
ObjectVariable Name that refers to instance of form, sort of
like an alias (not name of form itself though) Holds memory address of object (form) so that
you can use it ClassName()
Form’s class name—the name used in the Name property for a form
Chapter 7 - Part 1 14
Example
Dim errorForm As New frmError() errorForm is object variable name that
creates instance of actual form frmError is the class (name) of the actual
form As New causes instance of the form to be
created in memory Memory address assigned to object variable
name errorForm
Does not actually display form
Chapter 7 - Part 1 15
ShowDialog Method
Modal method (most common) No other form can receive focus until the
modal form is closed. Forces user (or timer) to acknowledge active
form ObjectVariable.ShowDialog() errorForm.ShowDialog()
Chapter 7 - Part 1 16
Show Method
Modeless form Allows user to switch focus to another form
while the modeless form is displayed ObjectVariable.Show() errorForm.Show()
Chapter 7 - Part 1 17
Close Method
Closes a form and releases it from memory. Me.Close()
Me—current instance of the form; refers to itself
Close—closes current instance Use to close a form in a multiple-form
program without terminating the application
Chapter 7 - Part 1 18
Hide Method
Removes form from the screen. Keeps form in memory; does not release it. Acts like changing form’s Visible property to
False.
Chapter 7 - Part 1 19
Other Events
Load Triggered immediately before a form displays. Example: populate a list box
Activated Triggered when user switches to a form from
another form. Triggered when for is initially displayed—after
the Load event. Base Class Events (class name drop-down) Activated (method drop-down)
Chapter 7 - Part 1 20
Closing Events
Triggered as form is being closed but before it finishes closing.
Useful to ask user if really wants to close form Event procedure parameter list includes
ByVal e As System.ComponentModel e has Boolean property named Cancel
True—prevents form from closing False—continues closing form
Chapter 7 - Part 1 21
Closing, Continued
Yes and No buttons DialogResult.Yes
Set e.Cancel property to False to close form Otherwise (i.e., DialogResult.No)
Set e.Cancel property to True; prevent closing
Chapter 7 - Part 1 22
Closed Event
Trigged after a form closes. Does not prevent a form from closing; only
responds once the form is closed. Refer to p. 415 for more information.
Chapter 7 - Part 1 23
Accessing Objects on Different Forms
Default Controls are on same form as the statement
that accesses it. lblOutput.Text = FormatCurrency(decTotal)
Allowance Write code to access objects on different
forms Dim resultsForm As New frmResults()
resultsForm.lblOutput.Text = decTotal
Chapter 7 - Part 1 24
Class-Level Variables
Accessible to all statements on that form. Not accessible, by default, to statements on
other forms. Class-level variables are private by default
Accessible outside the file containing the class, use the Public key word: Public decTotal As Decimal
Chapter 7 - Part 1 25
Procedures
Private Executable only from same form
Public (default setting) Also executable by statements outside of the
form
Chapter 7 - Part 1 26
Standard Modules
File (.vb) containing code Variable declarations Procedures Functions
Not associated with a particular form Useful to organize code in multi-form projects Assign descriptive names (p. 423) Add a standard module (p. 424)
Chapter 7 - Part 1 27
Private and Public Procedures
Private Accessible by statements only inside that
module Public
Also accessible by statements outside that module
Default if access specifier not provided
Chapter 7 - Part 1 28
Module-Level Variables
Module variable not declared within a procedure or function.
Accessible by an sub procedure or function in the module.
Not accessible to statements outside the module if declared with Dim or Private (module scope).
Accessible to statements outside module if declared with Public (global scope). Use g_ prefix for global variables. See warning on p. 426.