COPYRIGHT 2007: Dr. David Scanlan, CSUS
010-HELLO WORLD VB-Express.ppt
010-HELLO WORLD VB-Express.ppt
Purpose: A "Hello World" program's purpose is to eliminate as much complexity as possible so
that students successfully write and run their first program.
What these slides cover:
--How to write a simple program.
--How to reduce the complexity of the IDE
--How to place objects on a form
--How to use the Toolbox
--How to use a Textbox for output
--How to use a Button
--How to run a program
--How to save a program
Slide 1- 1
COPYRIGHT 2007: Dr. David Scanlan, CSUS
FIRST PROGRAM: HELLO WORLD VB Express
OBJECTIVES:
• To get started immediately so that you get the "big" picture right away.
• This will make VB 2008.NET textbook easier to understand.
• To reduce the complexity of the IDE.
• IDE: Integrated Development Environment.
• To quickly show how easy it is to write a simple program in VB 2008.NET.
• The program will illustrate:
• the use of the Toolbox,
• the use of a Textbox for output.
• the use of a Button,
• and the output of the words "Hello World" in a Textbox when a
button is pressed.
• To learn how to save a program.
Slide 1- 2
COPYRIGHT 2007: Dr. David Scanlan, CSUS
FIRST PROGRAM: HELLO WORLD VB Express
NOTE:
This program uses the default names for your button, label, textbox and
form: button1, label1, textbox1 and form1.
These names were not changed in order to make the following program as
simple as possible.
Later, we will make the names of buttons, labels, etc much more
meaningful.
Slide 1- 3
COPYRIGHT 2007: Dr. David Scanlan, CSUS
HELLO WORLD PROGRAM
BUTTON: Press the button and
"Hello World appears in the
LABEL and in the TEXTBOX.
LABEL
TEXTBOX
Dr. Scanlan: Program this with the class following.
Use 18 point font for the code.
FIRST PROGRAM: HELLO WORLD VB Express
Requirements
COPYRIGHT 2007: Dr. David Scanlan, CSUS
When you start VB Express, the Screen will look like this.
Click here to create a Project (a program)
Slide 1- 5
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Select this
This is not a very meaningful name for the Project.
See next slide.
Slide 1- 6
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Hello World is a much more meaningful name for this Project.
Enter Hello World here.
Next, press OK
Slide 1- 7
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Your Screen should now look like this.
Click on Data Sources.
We are going to get if off the screen
because we will not be using it in this
course.
Slide 1- 8
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Your screen should now look like this.
Click on the “x” to get the Data Source window
off the screen.
We did this to simplify the IDE (Integrated
Development Environment.
Slide 1- 9
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Your screen should now look like this.
We will use these two windows a lot during this semester.
1. Solution Explorer Window
2. Properties Window
Slide 1- 10
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Place the cursor on the Toolbox Tab to
cause the tools to be displayed.
Note: If you move the cursor away from
the Tab, the tools will disappear. Only
the Tab will remain.
You can keep the Toolbox in view if you click on
the horizontally oriented pin.
See next slide.
Slide 1- 11
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Note the vertical orientation of the pin.
The Toolbox is now “pinned down”.
Slide 1- 12
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Next, select a Button from the ToolBox and place it on
the form.
There are two ways to place a Button on the form:
1. Double Click on the Button.
2. Click on the Button and drag it onto the form.
Slide 1- 13
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Place the button about here
Next, place a Textbox on the form using
the same procedure you used for the
Button.
Slide 1- 14
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
The form should now look like this.
Form
Button
Textbox
Note: a Textbox can be used to display data
or to input data into the program.
Slide 1- 15
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Double click on the button to produce a
“Click Event” in the code view where we
do our code programming.
See next slide.
Slide 1- 16
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Your screen should now look like this.
We will place our code here.
See next slide.
Slide 1- 17
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Design View Tab
Code View Tab
Data
Object
Property
Now run the program by pressing here.
See next slide.
Slide 1- 18
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Your program should now look like this.
Next, press the button and watch
“Hello World” get displayed in the
Textbox.
Slide 1- 19
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
After pressing the button while the
program is running, Hello World is
displayed in the Textbox.
Slide 1- 20
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
An easy way to stop the program is to press the “x”
Slide 1- 21
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
An easy way to exit Visual Basic 2008 Express is to press this “x”
Slide 1- 22
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
If you want to save your Project, then choose Save.
Slide 1- 23
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
I would change the location to something easy to remember.
Project name
How about C:\MIS 15?
See next slide.
Solution Name
Solution files can contain several
projects. In this course we will have
only one project.
Slide 1- 24
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Slide 1- 25
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Your project has now been saved on your hard drive:
C:\MIS 15\Hello World
Take a look, it is there.
Slide 1- 26
HELLO WORLD PROGRAM
LABEL
TEXTBOX
FIRST PROGRAM: HELLO WORLD VB Express
COPYRIGHT 2007: Dr. David Scanlan, CSUS
Congratulations! You just wrote your first Visual Basic 2008 Program.
Slide 1- 28
How to Reset the IDE
back to the original settings.
Slide 1- 29
If your IDE looks like this and you want to reset
it. Follow the next slides…
Slide 1- 30
Click here.
Slide 1- 31
Click here.
Slide 1- 32
Click here.
Slide 1- 33
Click here.
Slide 1- 34
Click here.
Slide 1- 35
The IDE has been restored to its original settings.
Note: The Data Sources Tab is now present.
The End
Slide 1- 36
STARTING OUT WITH
Visual Basic 2008
FOURTH EDITION
Tony Gaddis
Haywood Community College
Kip Irvine
Florida International University
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 37
COPYRIGHT 2007: Dr. David Scanlan, CSUS
A GENTLE INTRODUCTION TO OBJECT-ORIENTED
PROGRAMMING (OOP)TERMS AND CONCEPTS.
This OOP stuff is
easy once you
get some
practice.
Slide 1- 38
COPYRIGHT 2007: Dr. David Scanlan, CSUS
OBJECTIVES
• To gain a very basic understanding of Object Oriented Programming (OOP)
terms and concepts using examples from everyday life.
These are the main OOP terms that we will cover:
• CLASS (CODE that is COPIED in order to create an object)
• PROPERTIES ( also called ATTRIBUTES)
• BEHAVIORS (also called METHODS)
• OBJECT (The copied code from a class)
• INSTANTIATION (The process of CREATING an object)
• INSTANCE (An OBJECT created from a class)
• INHERITANCE (Covered later in the course)
• POLYMORPHISM (Covered later in the course)
• ENCAPSULATION (Covered later in the course)
• These OOP terms and concepts will be learned using everyday items like
washing machines, caster wheels, and nerds. Actual OO computer
programming will delayed until you get a good understanding of its terms
and concepts.
Slide 1- 39
COPYRIGHT 2007: Dr. David Scanlan, CSUS
The Nerd CLASS
CLASS = TEMPLATE = BLUEPRINT = MODEL
A CLASS is easy to understand if you think of it as
a TEMPLATE, a BLUEPRINT, or a MODEL.
In my opinion, the word CLASS, was a poor choice for
this concept: Template or blueprint would have been a better choice.
Slide 1- 40
COPYRIGHT 2007: Dr. David Scanlan, CSUS
NERD CLASS
The Nerd CLASS
CLASS = TEMPLATE =
BLUEPRINT = MODEL
CLASS
• This is a CLASS (or template) called NERD.
• This object has PROPERTIES and
BEHAVIORS.
Nerd
Think of this as template for
making more nerds.
Smart
Thin
Social clod
Hair color
etc...
Read book
Turn on computer
Type on keyboard
Chew pizza
Put pencil in pocket
protector
etc...
CLASS name
PROPERITES
BEHAVIORS or
METHODS
Slide 1- 41
COPYRIGHT 2007: Dr. David Scanlan, CSUS
1.6 Concepts In OOP: Object
NERD CLASS
The Nerd CLASS
CLASS = TEMPLATE =
BLUEPRINT = MODEL
Think of this as template for
making more nerds.
Let’s choose this model as a template
from which to create new nerds.
OBJECT
• If we select one of these items in
the CLASS as a template for
creating another, we call the new
one an OBJECT.
• Using the template, we can create as
many new OBJECTS as we want.
Slide 1- 42
COPYRIGHT 2007: Dr. David Scanlan, CSUS
“I was INSTANTIATED by accident.
Yes, I admit it, I am an OBJECT from the
nerd CLASS.
You can even call me an
INSTANCE from the nerd
CLASS.
Slide 1- 43
COPYRIGHT 2007: Dr. David Scanlan, CSUS
The Nerd CLASS
CLASS = TEMPLATE = BLUEPRINT = MODEL
A CLASS is easy to understand if you think of it as
a TEMPLATE, a BLUEPRINT, or a MODEL.
In my opinion, the word CLASS, was a poor choice for
this concept: Template or blueprint would have been a better choice.
Slide 1- 44
COPYRIGHT 2007: Dr. David Scanlan, CSUS
What is a class in Visual Basic?
A Class is simply previously written code with
1. Properties
CLASSES
2. Methods (also called Behaviors)
An Object is a copy of the Class code with the same
1. Properties
2. Methods
NOTE: WE CAN MAKE AS MANY COPIES
(OBJECTS) OF A CLASS AS WE WANT.
OBJECTS
Example: The items in the Toolbox such as
Button, Labels, Textboxes, etc. are classes.
When we click on a class, we put a copy of
the class's code onto the Form.
Instructor: Give demo. Use Toolbox and create OBJECTS from CLASSES.
Slide 1- 45
Chapter
1
Introduction to Programming
and Visual Basic 2008
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 46
1.1
Computer Systems:
Hardware and Software
DR SCANLAN: SKIP PART 1.1AND JUMP
DIRECTLY TO PART 1.2
IT IS ASSUMED THAT STUDENTS
KNOW PART 1.1 FROM 1A, 1B, AND 1C
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 47
Computer Hardware



Refers to the physical components
Not one device but a system of many devices
Major types of components include:
 Central Processing Unit
 Main memory
 Secondary storage devices
 Input devices
 Output devices
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 48
Organization of a Computer System
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 49
The CPU







Fetches instructions from main memory
Carries out the operations commanded by the
instructions
Each instruction produces some outcome
CPU gets instructions from a program
A program is an entire sequence of instructions
Instructions are stored as binary numbers
Binary number - a sequence of 1’s and 0’s
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 50
Main Memory




Commonly known as random access memory, or
just RAM
Holds instructions and data needed for programs
that are currently running
RAM is usually a volatile type of memory
 Contents are lost when power is turned off
Used as temporary storage
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 51
Secondary Storage




A nonvolatile storage medium
 Contents retained while power is off
Hard disk drives are most common
 Records data magnetically on a circular disk
 Provides fast access to large amounts of data
Optical devices store data on CD’s as pits
USB flash memory devices
 High capacity device plugs into USB port
 Portable, reliable, and fits easily in a pocket
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 52
Input Devices


Any type of device that provides data to a
computer from the outside world
For example:
 Keyboard
 Mouse
 Scanner
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 53
Output Devices



Any type of device that provides data from a
computer to the outside world
Examples of output data:
 A printed report
 An image such as a picture
 A sound
Common output devices include:
 Monitor (display screen)
 Printer
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 54
Software


The programs that run on a computer
Two major categories
 Operating systems



Controls the processes within the computer
Manages the computer's hardware devices
Application Software




Solve problems or perform tasks needed by users
Examples include word processing, spreadsheets,
games, Internet browsers, playing music, etc)
Each program is referred to as an application
This book develops applications in Visual Basic
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 55
1.2
Programs and
Programming Languages
A Program Is a Set of Instructions a Computer
Follows in Order to Perform a Task
A Programming Language Is a Special Language
Used to Write Computer Programs
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
What Is a Program?



Computers only follow instructions.
A computer program is a set of instructions to
the computer.
For example, a program to compute gross pay.

We must program the steps.

Steps>> Next slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 57
Computing Gross Pay
STEPS:
1.
2.
3.
4.
5.
6.
7.
8.
Display message: “Hours worked?"
User enters hours
Put hours in memory
Display message: “Pay Rate?"
User enters pay rate
Store pay rate in memory
GrossPay = Hours * Rage
Display Gross Pay
Algorithm = steps for solving a problem.
Example: a recipe for baking a cake
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 58
Wage Calculator
 The label controls use the default names (Label1,
Textbox1, Button1, Form1, etc.)
 Text boxes, buttons, and the Gross Pay label play an
active role in the program and have been changed
Label1
Label2
Label3
txtHoursWorked
txtPayRate
lblGrossPay
btnCalcGrossPay
btnClose
Dr. Scanlan: Program this with the class following.
18 point text for the code.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 59
States and Transitions
Memory snapshots show
states of the program
3.
6.
7.
Store hours worked in memory
Store hourly pay rate in memory
Multiply hours worked by pay rate
and store amount earned in memory
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Program Starting State
hours worked
??
hourly pay rate
??
amount earned
??
Snapshot after Step 3
hours worked
20
hourly pay rate
??
amount earned
??
Snapshot after Step 6
hours worked
20
hourly pay rate
25
amount earned
??
Snapshot after Step 7
hours worked
20
hourly pay rate
25
amount earned
500
Slide 1- 60
Programming Languages

What the computer understands: Binary Numbers
 1001 0111 0111 0110
~0 volts
~5 volts



The CPU processes instructions as a series of 1’s
and 0’s called machine language
Programming languages allow us to use words
instead of a series of 1’s and 0’s
Compilers (software) translate VB statements into
machine language, so the computer can
understand the VB statements (code).
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 61
Common Programming Languages





Visual Basic
Python
Javascript
Java




C#
C
C++
PHP
Visual Basic is a:
 programming language
 programming environment with tools to:


Create objects on forms, etc.
Write programming language statements
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 62
Event Driven Programming: Events




Visual Basic is event-driven
An event is an action that takes place within a
program.
 Clicking a button (a Click event).
Visual Basic controls (buttons, textboxes, etc.)
are capable of detecting many, many events.
A program can respond to an event if the
programmer writes an event procedure.
 See Next Slide>>
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 63
Event Driven Programming: Events
When announced, this program will be written in the lab
with no notes. You must practice writing this
program.
Steps:
1. Create Form1 VERY similar to the one shown.
2. Set FORM1’s text property to: Wage Calculator
3. Set these three labels’ text properties to their
indicated values.
Set the name property of the fourth label with
$150.00 to: lblDisplayGrossPay
4. Place the two buttons on Form1
Set text property of one button to:
Calculate Gross Pay
Set text property of the second button to:
Close
- Set the Name Property of button Calculate
Gross Pay to: btnCalculateGrossPay
5. Double click on the CalculateGrossPay button.
You should see the following:
Public Class Form1
Private Sub btnCalculateGrossPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateGrossPay.Click
End Sub
End Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 64
Event Driven Programming: Events
When announced, this program will be written in the lab
with no notes. You must practice writing this
program.
Steps:
6. Double click on the Close button.
You should see the following:
Public Class Form1
Private Sub btnCalculateGrossPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateGrossPay.Click
End Sub
Place code here
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
End Sub
Place code here
End Class
7. You have just created two click events, but you now need to place code into these
two events.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 65
Event Driven Programming: Events
When announced, this program will be written in the lab
with no notes. You must practice writing this
program.
Steps:
8. Finish the program using the code below:
9. Run the program.
10. Check to be sure the results are correct.
Public Class Form1
Private Sub btnCalculateGrossPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateGrossPay.Click
lblDisplayGrossPay.Text = FormatCurrency(CDec(txtHourlyRate.Text) * CDec(txtHoursWorked.Text))
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 66
Event Driven Programming: Events
Event Procedure triggered by a
Click Event
-- Pressing the Calculate Gross Pay button triggers a click
event procedure which is code that must be written by the
programmer.
-- This click event procedure called btnCalculateGrossPay
will cause the code below to be executed.
When the button is CLICKED, this Event Procedure (code) is executed.
Event Procedure
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 67
Event Driven Programming: Events
Event Procedure triggered by a
Click Event
-- Pressing the Close button triggers a click event
procedure which is code that must be written by
the programmer.
-- This click event procedure called btnClose will
cause the code below to be executed.
When the button is CLICKED, this Event Procedure (code) is executed.
Event Procedure
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 68
Methods of Programming


Procedural Programming
 Step by step instructions.
 Similar to a step-by-step recipe for baking a
cake.
Object-Oriented Programming
 The step-by-step instruction have already
been written for us so that we can easily reuse
the code.
 Examples: Buttons, Labels, Textboxes, etc…


The code to produce these objects has already
been written.
Instructor: Create objects from the Toolbox
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 69
Methods of Programming


Procedural programming
 BUTTON – We would need to write hundreds
of lines of code to create a Button on a Form.
 This is like writing a procedure for creating a
chocolate cake.
Object-Oriented programming
 BUTTON – In OOP the code is already written.
All we need to do is use a copy of that code.
 Example: Double click on a Button in the Tool
box and a Button is created on the Form.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 70
Review on Objects
Examples of Objects
Form
Label
The form is the larger
rectangular object on
which other objects are
placed.
Textboxes
Buttons
Labels
Buttons
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 71
Review on Objects
Examples of Objects
 GUI: Graphical User Interface
 Objects consist of:
 Properties

Properties on this form:






Text
Color
Size
Position
Etc.
Methods




Calculate Gross Pay
Close
Min, Max
Etc.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 72
Review on Objects
Examples of Objects





Form elements are
objects called controls
This form has:
 Two TextBox controls
 Four Label controls
 Two Button controls
The value displayed by
a control is held in the text property of the control
Left button text property is Calculate Gross Pay
Buttons have methods attached to click events
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 73
1.3
More About Controls and
Programming
As a Visual Basic Programmer, You Must Design and
Create the Two Major Components of an Application:
the GUI Elements (Forms and Other Controls) and the
Programming Statements That Respond to And/or
Perform Actions (Event Procedures)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Visual Basic Controls

As a Windows user you’re already familiar with
many Visual Basic controls:








Label - displays text the user cannot change
TextBox - allows the user to enter text
- also allows the user to display text
Button – performs an action when clicked
RadioButton - A round button that is selected or
deselected with a mouse click
CheckBox – A box that is checked or unchecked
with a mouse click
Horizontal and Vertical Scroll Bars
Form - A window that contains these controls
Tutorial 1-3 demonstrates these controls
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 75
You must run this program and play with it.
See Tutorial 1-3 in the book for directions.
Tutorial 1-3, Visual Basic Controls
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 76
Name Property



All controls have properties
Properties have a value (values)
The Name Property allows the programmer to
access a control from within a program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 77
Examples of Names
 The label controls use the default names (Label1,
Textbox1, Button1, Form1, etc.)
 Text boxes, buttons, and the Gross Pay label play an
active role in the program and have been changed
Label1
Label2
txtHoursWorked
txtPayRate
lblGrossPay
Label3
btnCalcGrossPay
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
btnClose
Slide 1- 78
Naming Conventions






Control names must start with a letter
Remaining characters may be letters, digits, or
underscore
1st 3 lowercase letters indicate the type of control
 txt…
for Text Boxes
 lbl…
for Labels
 btn…
for Buttons
After that, capitalize the first letter of each word
txtHoursWorked is clearer than txthoursworked
NO spaces: txtHours Worked (Error)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 79
Event Handler – Compute Gross Pay
Private Sub btnCalcGrossPay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalcGrossPay.Click
'Define a variable to hold the gross pay.
Dim decGrossPay As Decimal
'Convert the values in the text boxes to decimal numbers,
'and calculate the gross pay.
decGrossPay = CDec(txtHoursWorked.Text) * CDec(txtPayRate.Text)
'Format the gross pay for currency display and
'assign it to the Text property of a label.
lblGrossPay.Text = FormatCurrency(decGrossPay)
End Sub
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 80
Event Handler - Close
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
'End the program by closing its window.
Me.Close()
End Sub
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 81
Language Elements

Keywords: Words with special meaning to Visual Basic
(e.g., Private, Sub)

Programmer-defined-names: Names created by the
programmer (e.g., decGrossPay, btnClose)

Operators: Special symbols to perform common
operations (e.g., +, -, *, and /)
Remarks: Comments inserted by the programmer – these
are ignored when the program runs (e.g., any text
preceded by a single quote)
 ‘This is a comment. Note the apostrophe (single quote).

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 82
Language Elements: Syntax



Syntax defines the correct use of key words,
operators, & programmer-defined names
Similar to the syntax (rules) of English that
defines correct use of nouns, verbs, etc.
A program that violates the rules of syntax will not
run until corrected
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 83
EXAMPLE OF AN "IN-CLASS LAB"
Points 0 or 5
Be sure to fill out the AS15 Form as shown on the next slide and have it ready for instructor
to collect. The form will not be collected if you have not completed the lab correctly.
You will be given 30 minutes to complete the Lab.
Steps:
1.
This program must be done totally from memory. Immediate failure of the course will result if
this rule is not followed. Remove all disks and flash drives from the system.
2.
Create a GUI as seen below.
3.
Use the object names as given below.
4.
Code the program so that it produces the correct result.
5.
Raise your hand as soon as you are finished so that instructor can grade you.
Label1
txtHoursWorked
txtPayRate
Label2
lblGrossPay
Label3
btnCalcGrossPay btnClose
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 84
How to fill out the 15AS FORM
ID FIELD
- Use the RFID card ID number that is on your attendance card.
Example: 12345
- TWO POINTS OFF IF NOT DONE CORRECTLY.
Left justify your ID number as
seen.
Place the following
answers on your
Scantron:
1.
a
2.
a
3.
a
4.
a
5.
a
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Last Name
First Name
Lab #
Date
Hour
Slide 1- 85
1.4
The Programming Process
The Programming Process Consists of Several
Steps, Which Include Design, Creation, Testing, and
Debugging Activities
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 86
Step 1 of Developing an Application


Clearly define what the program is to do
For example, the Wage Calculator program:
 Purpose: To calculate the user’s gross pay
 Input: Number of hours worked, hourly pay rate
 Process: Multiply number of hours worked by hourly
pay rate (result is the user’s gross pay)
 Output: Display a message indicating the user’s gross
pay
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 87
Step 2 of Developing an Application

Visualize the application running on the computer and
design its user interface
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 88
Step 3 of Developing an Application

Make a list of the controls needed
Type
TextBox
TextBox
Label
Name
txtHoursWorked
txtPayRate
lblGrossPay
Button
btnCalcGrossPay
Button
btnClose
Description
Allows the user to enter the number of hours worked.
Allows the user to enter the hourly pay rate
Displays the gross pay, after the btnCalcGrossPay
button has been clicked
When clicked, multiplies the number of hours worked
by the hourly pay rate
When clicked, terminates the application
Label
Label
Label
Form
(default)
(default)
(default)
(default)
Description for Number of Hours Worked TextBox
Description for Hourly Pay Rate TextBox
Description for Gross Pay Earned Label
A form to hold these controls
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 89
Step 4 of Developing an Application

Define values for each control's relevant properties:
Control Type
Form
Label
Label
Label
Label
Control Name
(Default)
(Default)
(Default)
(Default)
lblGrossPay
Text
"Wage Calculator"
"Number of Hours Worked"
"Hourly Pay Rate"
"Gross Pay Earned"
"$0.00"
TextBox
TextBox
txtHoursWorked
txtPayRate
"" (Called a null string)
"" (Called a null string)
Button
Button
btnCalcGrossPay
btnClose
"Calculate Gross Pay"
"Close"
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 90
Step 5 of Developing an Application

List the methods needed for each control:
Method
Description
btnCalcGrossPay_Click Multiplies hours worked by hourly pay rate
These values are entered into the
txtHoursWorked and txtPayRate TextBoxes
Result is stored in lblGrossPay Text property
btnClose_Click
Terminates the application
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 91
Step 6 of Developing an Application

Create pseudocode or a flowchart of each method:
 Pseudocode is an English-like description in
programming language terms
TWO EXAMPLES OF PSEUDOCODE:
Store Hours Worked x Hourly Pay Rate in decGrossPay.
Store the value of decGrossPay in lblGrossPay.Text.

A flowchart is a diagram that uses boxes and other
symbols to represent each step
Start
Multiply hours
worked by
hourly payrate.
Store result in
decGrossPay.
Copy value in
decGrossPay
to lblGrossPay
text property
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
End
Slide 1- 92
Step 7 of Developing an Application

Check the code for errors:
 Closely examine flowchart and/or pseudocode
 Step through each operation as though you are the
computer. Play being the computer.
 Use a piece of paper to jot down the values of
variables and properties as they change.
 Verify that the expected results are achieved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 93
Step 8 of Developing an Application

Use Visual Basic to create the forms and other
controls identified in step 3
 This is the first use of Visual Basic, all of the
previous steps have just been on paper
 Develop what the user will see on the form.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 94
Step 9 of Developing an Application

Use Visual Basic to write the code for the event
procedures and other methods created in step 6
 This is the second step on the computer
 In this step you develop the methods behind the
click event for each button
 Unlike the form developed on step 8, this portion of
the application is invisible to the user
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 95
Step 10 of Developing an Application

Attempt to run the application - find syntax errors
 Correct any syntax errors found
 Syntax errors are the incorrect use of an element
of the programming language
 Repeat this step as many times as needed
 All syntax errors must be removed before Visual
Basic will create a program that actually runs
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 96
Step 11 of Developing an Application

Run the application using test data as input
 Run the program with a variety of test data
 Check the results to be sure that they are correct
 Incorrect results are referred to as a runtime error
 Correct any runtime errors found
 Repeat this step as many times as necessary
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 97
1.5
Visual Studio and the
Visual Basic Environment
Visual Studio Consists of Tools That
You Use to Build Visual Basic
Applications
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 98
The Visual Studio IDE



Visual Studio is an integrated development
environment, often abbreviated as IDE
Provides everything needed to create, test, and
debug software including:
 The VB language
 Form design tools to create the user interface
 Debugging tools to help find and correct
programming errors
Visual Studio supports other languages beside
Visual Basic such as C++ and C#
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 99
The Visual Basic Environment

Tutorial 1-4 introduces elements of the IDE:








Customizing the IDE
Design window – a place to design and create a form
Solution Explorer window – shows all the files in the
solution
Properties window – modify properties of an object
Dynamic Help window – a handy reference tool
Toolbar – contains icons for frequently used functions
Toolbox window – Classes used in form design
Tooltips – a short description of button’s purpose
THE END
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 100
THREE TYPE OF ERRORS IN
PROGRAMMING
1. Syntax errors
2. Runtime errors
3. Logic errors
Generally speaking, the errors become more difficult to find and fix as
you move down the above list.
Syntax errors
--In effect, syntax errors represent grammar errors in the use of the
programming language. Common examples are:
--Misspelled variable and function names
--Missing semicolons
--Improperly matches parentheses, square brackets, and curly braces
--Incorrect format in selection and loop statements
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 101
THREE TYPE OF ERRORS IN
PROGRAMMING
Runtime errors
Runtime errors occur when a program with no syntax errors asks the
computer to do something that the computer is unable to reliably do.
Common examples are:
--Trying to divide by a variable that contains a value of zero
--Trying to open a file that doesn't exist
--There is no way for the compiler to know about these kinds of errors
when the program is compiled.
Logic errors
Logic errors occur when there is a design flaw in your program.
Common examples are:
--Multiplying when you should be dividing
--Adding when you should be subtracting
--Opening and using data from the wrong file
--Displaying the wrong message
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 1- 102
Descargar

No Slide Title