of 12
1
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 1
Visual Basic -1 Quiz #1 Review
OOP Terminology
OOP: Object-oriented programming
Class: blueprint used to create an object
Object:
Represents a real-world entity
Attributes (or properties): describe the object
Methods: behaviors or operations the object can
perform
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2
OOP Terminology
(continued) Object-oriented program: one that focuses on
objects the program can use to accomplish its goal
Integrates processes and data into objects
Procedure-oriented program: one that focuses on individual tasks and their sequence
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3
Visual Studio 2010
Integrated Development Environment (IDE):
Contains all the tools and features needed to create,
run, and test programs
Includes an editor and compiler
Visual Studio 2010:
IDE used to create Windows or Web-based programs
Includes Visual Basic 2010, Visual C++ 2010, &Visual
C# 2010, etc
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4
Visual Studio 2010 (continued)
Application: program or suite of programs
Windows-based application:
Has a Windows user interface
Runs on a desktop computer
Web-based application:
Has a Web user interface
Runs on a server
Use a computer browser to access it
User interface: what the user sees and interacts with
when using an application
2
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5
The Properties Window
Properties window: displays properties of selected object
Default property values are assigned when an object is created
Properties list:
Left column displays names of properties
Settings box: Right column containing the current value of each property
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6
The Me.Close Method
Me.Close method: closes the current form
Method: predefined VB procedure that can be
invoked (called)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7
IDE Windows
Toolbox: contains tools for creating the GUI (Controls)
Form control: acts as a container for other controls
Label control: contains text that a user cannot edit
Button control: performs an immediate action when clicked
Container controls: - Binds other controls visually and programmatically
Group Box Has Text Caption
Panel No Text Caption
Table Layout Panel Holds other controls in a table format of Rows and Columns
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8
Planning an Application
Plan the application before creating the user interface
Work jointly with the user
Define the tasks that need to be performed
Get copies of any currently used manual forms
Identify the Tasks your application must perform
Identify Objects which you will assign to the tasks
Identify the triggering Events
Make a TOE (Task, Object, Event) chart:
Shows applications tasks, objects, and events
Design the user interface
3
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9
Identifying the Applications Tasks
(continued)
First, review current user procedures and forms
Steps:
Identify the desired outputs and necessary inputs
Identify the processing needed to change the inputs
into the outputs
Identify the need to clear the screen between
transactions
Identify how the user will end the application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10
Identifying the Objects
Assign each task to an object in the TOE
TextBox tool: instantiates a text box control
TextBox:
Used to allow the user to input information
Use Labels to guide the user
Use Buttons to initiate the calculations
Generate Events
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
Identifying the Events
Text boxes: no special events needed for user to
enter the text (None that you have to code)
Labels: no special events needed to display the
prompts (None that you have to code)
Buttons:
Action must occur when each button is clicked
(Click Event) (You have to code these)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
TOE Chart
4
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
Designing the User Interface
Follow Windows standards for:
Consistency
Ease of use
Familiar look and feel
Primary window:
The main window in an application
Dialog boxes: windows used to support and
supplement a users activities in the primary window
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14
Designing the User Interface
(continued)
Primary Windows (MainForm)
Can be resized, minimized, maximized, and closed
by the user
Title Bar includes:
Minimize, Maximize, and Close buttons on the right
Control menu on the left
Dialog Boxes
Can be closed only
Title Bar includes:
Close button and optionally a Help button
No control menu
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
Arranging the Controls
Guidelines:
Information should flow either vertically or
horizontally
Group related controls together using white space or
container controls
Container controls:
GroupBox control
Panel control
TableLayoutPanel control
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
Arranging the Controls (continued)
Control Guidelines:
Use a label with each text box
Left-align the labels text (I prefer Right-align)
Position label to left of or above the text box it
identifies (I prefer to the left)
Labels and button captions should be 1 to 3 words only,
and appear on one line
Labels and captions should be meaningful
Use sentence capitalization for labels
5
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17
Including Graphics and Fonts
in the User Interface
Graphics Guidelines:
Human eye is drawn to pictures before text
Include graphics only if necessary
Use for aesthetic purposes
Use to clarify a portion of the screen
Font Guidelines:
Use only one font type for the text in a form
12-point font is easiest to read at high screen resolution
Avoid italics and underlining
Use bold only for titles, headings, and key terms
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18
Including Color in the User Interface
Human eye is drawn to color before B&W
Color Guidelines:
Use color sparingly
Some people have trouble distinguishing colors
What is acceptable in colors is subjective
Color may have specific meaning in certain cultures
Use black or dark text on a white or light background
Use maximum of 3 different colors that complement each other
Do not use color as the only means of identification
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19
Assigning Access Keys
Access key:
Allows user to select an object using Alt + access key
Appears underlined on the button caption
Is not case-sensitive
Include & in front of the character to be used as the access key:
&Calculate Order Calculate Order
Access Key Guidelines:
Assign access keys to each control that can accept user
input
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20
Assigning Access Keys (continued)
Advantages of using access keys:
User does not need mouse to navigate and activate controls
Allows fast typists to keep hands on keyboard
Facilitates use of the application by people with disabilities
6
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21
Setting the TabIndex Property
TabIndex property:
Determines the order in which a control receives the
focus when the Tab key is pressed
Starts at 0
Default TabIndex values are set according to the order
in which the controls were added to the form
When a form starts, the control with the lowest
TabIndex value gets Focus unless Focus is forced to
some other control by the code.
Focus: the state of being able to accept user input
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22
User Interface Summary
Align controls to flow horizontally or vertically
Group related controls visually with white space or
container controls, and maintain consistent margins
Use meaningful labels and captions of 1-3 words
Use graphics and colors sparingly
Use TabIndex property to control where the focus
goes when Tab key is used
BE CONSISTANT
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23 Microsoft Visual Basic 2010: Reloaded, 23
Chapter 3
Variables
Variables: computer memory locations used to store data (objects) while an application is running
Every variable has a:
Name
Data type
Scope
Lifetime
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24 Microsoft Visual Basic 24
Selecting a Data Type for a Variable
Each variable must be assigned a data type
Data type: the type of data the variable can store
Each data type is a CLASS
A group of instructions used to create an object
7
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25 Microsoft Visual Basic 2005: Reloaded, 25
Selecting a Name for a Variable
(continued)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26 Microsoft Visual Basic 2005: Reloaded, 26
Declaring a Variable
Declaration statement: used to declare, or create,
a variable
Declaration statement includes
Scope keyword: Dim or Private or Static
Public
Scope determines Lifetime
Name of the variable
Data type
Initial value (optional)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27 Microsoft Visual Basic 2005: Reloaded, 27
Initializing a Variables
Variables are initialized when they are Instantiated
If you do not provide a value, VB does it for you
VB sets a default value based on data type
Numeric types normally initialized to 0 (zero)
String types set to nothing
If your code expects variables to be initialized to a
particular value (even if 0) set it yourself in the
declaration
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28 Microsoft Visual Basic 2005: Reloaded, 28
Assigning Data to an Existing
Variable
Assignment statement:
Used to assign values to properties of controls
Used to assign values to variables
Assignment operator: (=)
Value on the right of the = operator is assigned to the
variable on the left of the = operator
8
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29 Microsoft Visual Basic 2005: Reloaded, 29
Assigning Data to an Existing
Variable (continued)
String: group of characters enclosed in quotation
marks
Literal constant:
An item of data whose value does not change while the
application is running
Can be a numeric or a string literal constant
A numeric literal with a decimal place is treated as a
Double type. (eg .03)
Literal type character: forces a literal constant to
assume a specific data type (Always use it)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30 Microsoft Visual Basic 2005: Reloaded, 30
Assigning Data to an Existing
Variable (continued)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31 Microsoft Visual Basic 2005: Reloaded, 31
Using the TryParse Method
Method: a specific portion of a classs instructions that performs a task for the class
TryParse method:
Part of every numeric data types class
Used to convert a string to that numeric data type
TryParse method has either 2 or 4 arguments
String: string value to be converted
NumberStyles (optional): allows formatting characters to be in the data to be converted
IFormatProvider (optional): specifies formatting
Variable: location to store the result
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32 Microsoft Visual Basic 2005: Reloaded, 32
Using the TryParse Method
(continued) Assign the TryParse methods return value to a UNIQUE
Boolean variable. (Data Validation)
If True, the conversion was successful
If False, the value could not be converted
Must have an Imports statement in the General Declarations section of code to use NumberStyles and
NumberformatInfo.CurrentInfo:
Imports System.Globalization
Line continuation character: the underscore (_)
Breaks up a long instruction into two or more lines
Must appear at end of line, preceded by a space
9
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33 Microsoft Visual Basic 2005: Reloaded, 33
Using the Convert Class
Convert class:
Contains methods for converting numeric values to
specific data types
Use the dot member access operator to separate
the class name from the method name
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34 Microsoft Visual Basic 2005: Reloaded, 34
Writing Arithmetic Expressions
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35 Microsoft Visual Basic 2005: Reloaded, 35
Writing Arithmetic Expressions
(continued) Precedence number: indicates the order in which
an operation in an expression is performed
If an expression has two operators with the same
precedence, they are evaluated from left to right
Use parentheses to change the order of evaluation
Consider not relying on precedence and always
use parentheses (precedence rules vary)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36 Microsoft Visual Basic 2005: Reloaded, 36
Writing Arithmetic Expressions
(continued)
Using Parentheses
Should be equal number of open ( and closed )
parentheses in an expression
Write your expression by hand before you code it
Make it part of your Pseudocode
Check your parentheses
Count the # of opens and closes
Evaluate your expression from the inside out
If its too complicated, break up your expression into
multiple expressions, use intermediate variables
10
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37 Microsoft Visual Basic 2005: Reloaded, 37
The Scope and Lifetime of a
Variable
Scope: indicates where the variable can be used
Lifetime: indicates how long the variable remains in
memory
Variables are usually declared in one of two places:
Within a procedure
In the forms Declarations section
Procedure-level variable: declared within a
procedure
Procedure scope: only the procedure can use the
variable
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38 Microsoft Visual Basic 2005: Reloaded, 38
The Scope and Lifetime of a
Variable (continued)
With procedure-level scope, two procedures can
each use the same variable names
Class scope: variable can be used by all procedures in the form
Class-level variable:
Declared in the forms Declarations section
Use Private keyword in declaration
Class-level variables retain their values until the application ends
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39 Microsoft Visual Basic 2005: Reloaded, 39
Static Variables
Static variable:
Procedure-level variable that retains its value even after the procedure ends
Retains its value until the application ends
Can be used instead of a Class-level variable
A static variable has:
Same lifetime as a Class-level variable
Narrower scope than a Class-level variable
Declared using the Static keyword
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40 Microsoft Visual Basic 2005: Reloaded, 40
Setting the Focus
Focus method: moves the focus to a specified
control at runtime.
Most form controls have a Focus method.
11
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41 Microsoft Visual Basic 2005: Reloaded, 41
Setting the Focus
(continued)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42 Microsoft Visual Basic 2005: Reloaded, 42
Important Concepts About
Variables and Constants
Variables and named constants are memory
locations that store data
Variables can change value at Runtime, but
constants cannot
Variables and constants have a name, data type,
scope, and lifetime
Use Dim to declare a variable at block or
procedure level
Use Private to declare a variable at Class level
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43 Microsoft Visual Basic 2005: Reloaded, 43
Important Concepts About
Variables and Constants
(continued)
Assignment statement is used to assign values to an existing variable
Assignment Operator is the = sign
Literals are constant items of data
Use the TryParse method to convert a user entered string to
a number
Use the Imports statement to import a namespace
The Convert class contains methods to convert values to a
specified data type. Use to convert datatypes within a
program
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44 Microsoft Visual Basic 2005: Reloaded, 44
Additional Concepts About
Variables
A procedure-level variable is usable only by the
procedure in which it is declared
A Class-level variable is usable by all procedures
in the form
A block-level variable is usable only within the
block in which it is declared
A static variable is a procedure-level variable that
retains its value when the procedure ends
12
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45 Microsoft Visual Basic 2005: Reloaded, Second Edition 45
Miscellaneous Concepts
Option Explicit On forces declaration of all
variables before use
Option Strict On disallows any implicit type
conversions that may cause a loss of data
Option Infer Off statement: ensures that every
variable is declared with a data type