Starting Out w/Visual Basic 2012 Ch 01 PPT
-
Upload
corazon-tolentino -
Category
Documents
-
view
551 -
download
4
description
Transcript of Starting Out w/Visual Basic 2012 Ch 01 PPT
Slide 1- 2
Overview
Topics
Introduction
Computer Systems
Hardware and Software
Programs and Programming Languages
Controls and Programming
Programming Process
Visual Studio
Slide 1- 3
Introduction
With Visual Basic
You may create applications that
Provide graphical windows, dialog boxes, menus
Work with databases
Use internet technologies
Display graphics
Computer Systems:
Hardware and Software 1.1
Computer systems consist of similar
hardware devices and components
Computer Hardware
Computer hardware
Refers to physical components
Central Processing Unit (CPU)
Main memory
Secondary storage devices
Input devices
Output devices
Slide 1- 5
Organization of a Computer System
Input
keyboard
mouse
scanner
microphone
digital camera
disk drives
Output
monitor
printer
speakers
disk drives
Slide 1- 6
Central Processing Unit
Central Processing Unit (CPU)
Fetches instructions from program in main
memory
Part of program stored as binary numbers
(sequences of 0's and 1's)
Carries out operations commanded by
instructions
Each instruction produces some outcome
Slide 1- 7
Main Memory
RAM (Random Access Memory)
Sequentially numbered storage locations
Holds instructions and data needed for
programs currently running
Usually volatile (contents lost when power is
turned off)
Used as temporary storage
Slide 1- 8
Secondary Storage
Secondary storage
Nonvolatile storage (contents retained while
power is off)
Usually a disk drive (magnetic storage)
Hard disks, floppy disks
CDs, DVDs
Flash drives
Slide 1- 9
Input Devices
Input devices
Provide data to computer from outside world
Examples
Keyboard
Mouse
Scanner
Microphone
Slide 1- 10
Output Devices
Output devices
Provide data from computer information to
outside world
Image, printed report, sound
Examples
Monitor (display screen)
Printer
Speakers
Slide 1- 11
Software
Software
Programs that run on a computer
Operating systems
Controls processes within computer
Manages computer's hardware devices
Application software
Solves problems or performs tasks needed by users
Examples
Word processing, spreadsheets, games, Internet
browsers, playing music
Slide 1- 12
Programs and
Programming Languages 1.2
A program is a set of instructions that a computer
follows in order to perform a task.
A programming language is a special language used
to write computer programs.
What Is a Program?
Computer program
Set of instructions on how to solve a problem
or perform a task
Example
In order for a computer to calculate someone’s
gross pay, we must tell computer to perform the
following steps
Slide 1- 14
Computing Gross Pay
Steps to compute gross pay
1. Display message on screen: "How many
hours did you work?"
2. Allow user to enter hours worked
3. Store number in memory
4. Display message on screen: "How much are
you paid per hour?"
5. Allow user to enter hourly pay rate
6. Store number in memory
Slide 1- 15
Computing Gross Pay (cont.)
7. Multiply hours worked by hourly pay rate and
store result in memory
8. Display a message on screen with result of
previous step
Note: This well-defined, ordered set of steps for
solving a problem is called an algorithm
Slide 1- 16
Slide 1- 17
Pseudocode
Start
output "How many hours did you work?"
input hoursWorked
output "How much are you paid per hour?"
input hourlyPayRate
grossPay = hoursWorked * hourlyPayRate
output "You earned ", grossPay
Stop
Slide 1- 18
Flowchart Start
Print “How many
hours did you work?”
Input hoursWorked
grossPay = hoursWorked *
hourlyPayRate
Print “How much are
you paid per hour?”
Input hourlyPayRate
Print “You earned “,
grossPay
Stop
States and Transitions
Memory snapshots show states of program
3. Store hours worked in memory
6. Store hourly pay rate in memory
7. Multiply hours worked by hourly pay
rate and store gross pay in memory
Slide 1- 19
Program Starting State
hours worked
hourly pay rate
gross pay
??
??
??
Snapshot after Step 3
hours worked
hourly pay rate
gross pay
20
??
??
Snapshot after Step 6
hours worked
hourly pay rate
gross pay
20
25
??
Snapshot after Step 7
hours worked
hourly pay rate
gross pay
20
25
500
Slide 1- 20
Terms
Algorithm
Well-defined, ordered set of steps for solving a
problem
Algorithm
al·go·rithm noun
a set of rules for solving a problem in a finite number of
steps, as for finding the greatest common divisor.
http://dictionary.reference.com/browse/algorithm
Slide 1- 21
Algorithm (cont.)
In mathematics and computer science, an algorithm is an
effective method expressed as a finite list[1] of well-defined
instructions[2] for calculating a function.[3] Algorithms are
used for calculation, data processing, and automated
reasoning. In simple words an algorithm is a step-by-step
procedure for calculations.
http://en.wikipedia.org/wiki/Algorithm
Slide 1- 22
Computer Program
pro·gram noun
a. a systematic plan for the automatic solution of a
problem by a computer.
b. the precise sequence of instructions enabling a
computer to solve a problem.
http://dictionary.reference.com/browse/program
Slide 1- 23
Computer Program (cont.)
A computer program (also software, or just a program)
is a sequence of instructions written to perform a specified
task with a computer.[1] A computer requires programs to
function, typically executing the program's instructions in a
central processor.[2] The program has an executable form
that the computer can use directly to execute the
instructions. The same program in its human-readable
source code form, from which executable programs are
derived (e.g., compiled), enables a programmer to study
and develop its algorithms.
http://en.wikipedia.org/wiki/Computer_program
Slide 1- 24
Slide 1- 25
Terms (cont.)
Machine language
Series of 1’s and 0’s computer understands
Programming language
Allows programmers to use words instead of
numbers
Compiler
Converts programming language statements to
machine language
Slide 1- 26
Common Programming Languages
Procedural
BASIC
C
COBOL
FORTRAN
Pascal
Object-Oriented
C++
C#
Java
Visual Basic
Language Elements
Programs are comprised of
Keywords
Words with special meaning to Visual Basic (e.g., Private, Sub)
Programmer-defined names
Names created by programmer (e.g., grossPay, btnClose)
Operators
Symbols to perform common operations
(e.g., +, -, *, /)
Slide 1- 27
Language Elements (cont.)
Statements
Instructions made up of keywords, identifiers, and
operators
Procedures
Set of programming statements that perform a
single task
Comments
Program documentation inserted by developer and
ignored when program runs
Begin with ' and continue to end of line
Slide 1- 28
Language Elements: Syntax
Syntax
Rules of language
Syntax of Visual Basic
Defines correct use of keywords, operators, and
programmer-defined names
A program that violates the rules of syntax will not
run until corrected
Syntax of English
Defines correct use of nouns, verbs, etc.
Slide 1- 29
Slide 1- 30
Methods of Programming
Procedural
Constructed as set of procedures
(operational, functional units)
Each procedure is a set of instructions
Object-Oriented
Constructed as set of real-world objects
i.e. students, transcripts, and courses
Objects have data elements (attributes) and
perform actions (methods)
Example of an Object
Graphical User Interface (GUI) object called a
form
Data include
hours worked
hourly pay rate
gross pay
Actions include
Calculating gross pay in a method that
determines how form reacts
Slide 1- 31
Example of an Object (cont.)
Form elements are
objects called controls
Sample form has
2 TextBox controls
4 Label controls
2 Button controls
A controls’s Text property holds the value
displayed by the control
Text property of left button is Calculate Gross Pay
Buttons have methods attached to click events Slide 1- 32
Event Driven Programming
Event
Action that takes place within a program
Examples
Mouse click, text change
Visual Basic controls are capable of detecting
events
Program can respond to an event if
programmer writes an event procedure
Slide 1- 33
More About Controls and
Programming 1.3
As a Visual Basic programmer, you must design and
create the application’s
1) GUI elements (forms and other controls)
2) programming statements that respond to and/or
perform actions (event procedures)
Visual Basic Controls
Windows users are familiar with many controls
Label - displays text user cannot change
TextBox - allows user to enter text
Button - performs an action when clicked
RadioButton - round button selected or
deselected with a mouse click
CheckBox - box checked or unchecked with a
mouse click
Form - window that contains controls
Slide 1- 35
Tutorial 1-3, Visual Basic Controls
Slide 1- 36
Name Property
All controls have properties
Each property has a value (or values)
Every control has a Name property
Value of Name property is used to reference
the control in a program
Controls are assigned default names when created
Programmers usually change these names to
meaningful names
Slide 1- 37
Examples of Names
Labels use default names (i.e. Label1)
Text boxes, buttons, and Gross Pay label play an
active roles in program and have been changed
Slide 1- 38
btnCalcGrossPay btnClose
txtHoursWorked
txtPayRate
lblGrossPay
Label1
Label2
Label3
Form1
Slide 1- 39
Naming Rules
Naming rules
Control names must start with a letter
Remaining characters may be letters, digits, or
underscores
Slide 1- 40
Naming Conventions
Naming conventions
Use prefix to indicate type of control
txt for Text Boxes
lbl for Labels
btn for Buttons
Select a descriptive name
Use camel-casing
i.e. txtHoursWorked is clearer than txthoursworked
Event Handler – Compute Gross Pay
Slide 1- 41
' Procedure for click event on btnCalcGrossPay
Private Sub btnCalcGrossPay_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnCalcGrossPay.Click
' Define variable to hold gross pay
Dim grossPay As Double
' Convert values in text boxes to numbers, and calculate gross pay
grossPay = Convert.ToDouble(txtHoursWorked.Text) *
Convert.ToDouble (txtPayRate.Text)
' Format gross pay for currency display and assign it to Text
' property of label
lblGrossPay.Text = grossPay.ToString("C")
End Sub
Event Handler - Close
Slide 1- 42
' Procedure for click event on btnClose
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
Programming Process
1.4
The programming process consists of design,
creation, testing, and debugging activities
Step 1 of Developing an Application
Clearly define what the program is to do
Example Wage Calculator program
Purpose: to calculate user’s gross pay
Input: number of hours worked, hourly pay rate
Process: multiply number of hours worked by hourly pay
rate resulting in gross pay
Output: display a message indicating computed gross
pay
Slide 1- 44
Step 2 of Developing an Application
Visualize application running on the computer
and sketch its user interface
Slide 1- 45
Step 3 of Developing an Application
List controls needed
Slide 1- 46
Type Name Description
TextBox txtHoursWorked Allows the user to enter the number of hours worked.
TextBox txtPayRate Allows the user to enter the hourly pay rate
Label lblGrossPay Displays the gross pay, after the btnCalcGrossPay
button has been clicked
Button btnCalcGrossPay When clicked, multiplies the number of hours worked
by the hourly pay rate
Button btnClose When clicked, terminates the application
Label (default) Description for Number of Hours Worked TextBox
Label (default) Description for Hourly Pay Rate TextBox
Label (default) Description for Gross Pay Earned Label
Form (default) A form to hold these controls
Step 4 of Developing an Application
Define values for each control's relevant properties
Slide 1- 47
Control Type Control Name Text
Form (Default) "Wage Calculator"
Label (Default) "Number of Hours Worked"
Label (Default) "Hourly Pay Rate"
Label (Default) "Gross Pay Earned"
Label lblGrossPay "$0.00"
TextBox txtHoursWorked ""
TextBox txtPayRate ""
Button btnCalcGrossPay "Calculate Gross Pay"
Button btnClose "Close"
Step 5 of Developing an Application
List event handlers needed for each control
Slide 1- 48
Method Description
btnCalcGrossPay_Click Multiplies hours worked by hourly pay rate
These values are entered into txtHoursWorked
and txtPayRate TextBoxes. Result is stored in
lblGrossPay Text property.
btnClose_Click Terminates the application
Slide 1- 49
Step 6 of Developing an Application
Create pseudocode or flowchart of each method
Pseudocode is an English-like description in
programming language terms
Compute grossPay as numberOfHoursWorked times
hourlyPayRate
Assign to lblGrossPay.Text the value in grossPay
Slide 1- 50
Step 6 of Developing an Application (cont.)
Flowchart is a diagram that graphically depicts
the flow of the method
Start End
Multiply hours
worked by
hourly pay
rate, store
result in
grossPay
Copy value in
grossPay to
lblGrossPay
Text property
Step 7 of Developing an Application
Desk check code for errors
Read flowchart and/or pseudocode
Step through each operation as if you are the
computer
Use paper to jot down values of variables and
properties as they change
Verify that expected results are achieved
Slide 1- 51
Step 8 of Developing an Application
Use Visual Basic to create GUI (Graphical User
Interface)
Forms and other controls identified in step 3
Note: This is first use of Visual Basic. All previous steps
have just been on paper. In this step you develop
portion of application that the user will see.
Slide 1- 52
Step 9 of Developing an Application
Use Visual Basic to write code for
Event procedures
Other methods created in step 6
Note: This is second use of Visual Basic. In this step you
develop methods behind click events for buttons. This
portion of application the user will not see.
Slide 1- 53
Step 10 of Developing an Application
Attempt to run the application - find syntax errors
Correct any syntax errors found
Repeat this step as many times as needed
Note: All syntax errors must be removed before Visual
Basic will create a program that actually runs
Slide 1- 54
Step 11 of Developing an Application
Run application using test data as input
Run the program with a variety of test data
Check results to be sure that they are correct
(Incorrect results are referred to as runtime
errors)
Correct any runtime errors found
Repeat steps 10 and 11 as often as necessary
Slide 1- 55
Visual Studio and the
Visual Basic Environment 1.5
Visual Studio consists of tools that you use to build
Visual Basic applications
Visual Studio IDE
Visual Studio
Integrated Development Environment (IDE)
Provides tools needed to create, test, and debug software applications
Form designer to create the user interface
Code editor and compiler
Visual Basic, C# languages
Debugger to help find and correct programming
errors
Slide 1- 57
Visual Basic Environment
Tutorial 1-4 introduces elements of the IDE
Customizing the IDE
Windows
Design – design and create a form
Solution Explorer – shows files in solution
Properties – modify properties of an object
Dynamic Help – handy reference tool
Toolbox – objects used in form design
Toolbar – icons for frequently used functions
Tooltip – short description of icon’s purpose
Slide 1- 58
Creating a VB Application
To create an application
Open Visual Studio
Create a New Project
Create a New VB .NET Windows Application
Add Controls to Design Window
Add Code for Event Procedures
Build Project
Run Code
View Results
Slide 1- 59
Launching Visual Studio
Slide 1- 60
VS 2010 - Start Page
Create a New Project
New Project
New Project: VB Windows Forms Application
Design Window
Design Window
Solution Explorer
Properties Window
Toolbox
Slide 1- 65
Visual Basic Environment
Environment
Toolbox
Design Window
Solution Explorer Window
Properties Window
Dynamic Help Window
Docked and Floating Windows
Toolbox, Form with Controls, Properties
Title Bar
Menu Bar
Toolbox
Standard Toolbar
Properties Window
Code Window
Building the Code
Build Solution/Project
Running the Code
Run/Start Debugging
Sample Run
Code
' Compute Wages Code
Private Sub btnCalcGrossPay_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnCalcGrossPay.Click
' declare variable to hold computed wages
Dim hoursWorked As Double
Dim hourlyRate As Double
Dim grossPay As Double
Double.TryParse(txtHoursWorked.Text, hoursWorked)
Double.TryParse(txtHourlyPayRate.Text, hourlyRate)
' compute gross pay based on hours worked and hourly pay rate
grossPay = hoursWorked * hourlyRate
' display gross pay
lblGrossPayEarned.Text = grossPay.ToString("c")
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnClose.Click
' close form to end application
Me.Close()
End Sub
Slide 1- 71
Code
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' clear text fields
txtHoursWorked.Clear()
txtPayRate.Clear()
lblGrossPay.Text = String.Empty
' set focus to hours worked
txtHoursWorked.Focus()
End Sub
Slide 1- 72
Slide 1- 73
Summary
Topics covered
Introduction
Computer Systems
Hardware and Software
Programs and Programming Languages
Controls and Programming
Programming Process
Visual Studio