Sunteți pe pagina 1din 73

Chapter

Introduction to Programming and Visual Basic 2010

Print

Overview

Topics Introduction Computer Systems

Hardware and Software

Programs and Programming Languages Controls and Programming Programming Process Visual Studio

Slide 1- 2

Introduction

With Visual Basic You may create applications that

Provide graphical windows, dialog boxes, menus Work with databases Use internet technologies Display graphics

Slide 1- 3

1.1

Computer Systems: Hardware and Software

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


Monitor (display screen) Printer Speakers

Examples

Slide 1- 11

Software

Software Programs that run on a computer Operating systems

Controls processes within computer Manages computer's hardware devices Solves problems or performs tasks needed by users Examples

Application software

Word processing, spreadsheets, games, Internet browsers, playing music


Slide 1- 12

1.2

Programs and Programming Languages

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 someones 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.

8.

Multiply hours worked by hourly pay rate and store result in memory 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

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- 17

Flowchart

Start

Print How many hours did you work?

Input hoursWorked

Print How much are you paid per hour?

Input hourlyPayRate

grossPay = hoursWorked * hourlyPayRate

Print You earned , grossPay

Stop

Slide 1- 18

States and Transitions


Program Starting State

Memory snapshots show states of program


3.

hours worked hourly pay rate

?? ??

gross pay

??

Snapshot after Step 3


hours worked 20 ?? ??

Store hours worked in memory

hourly pay rate gross pay

Snapshot after Step 6


hours worked

6.

Store hourly pay rate in memory

20 25 ??

hourly pay rate


gross pay

Snapshot after Step 7

7.

Multiply hours worked by hourly pay rate and store gross pay in memory

hours worked
hourly pay rate

20 25 500

gross pay

Slide 1- 19

Terms

Algorithm Well-defined, ordered set of steps for solving a problem

Slide 1- 20

Algorithm
algorithm 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
program 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

Terms (cont.)

Machine language Series of 1s and 0s computer understands Programming language Allows programmers to use words instead of numbers Compiler Converts programming language statements to machine language

Slide 1- 25

Common Programming Languages

Procedural BASIC C COBOL FORTRAN Pascal

Object-Oriented C++ C# Java Visual Basic

Slide 1- 26

Language Elements

Programs are comprised of Keywords

Words with special meaning to Visual Basic (e.g., Private, Sub) Names created by programmer (e.g., grossPay, btnClose)

Programmer-defined names

Operators

Symbols to perform common operations (e.g., +, -, *, /)


Slide 1- 27

Language Elements (cont.)

Statements

Instructions made up of keywords, identifiers, and operators Set of programming statements that perform a single task Program documentation inserted by developer and ignored when program runs Begin with ' and continue to end of line
Slide 1- 28

Procedures

Comments

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 Defines correct use of nouns, verbs, etc.

Syntax of English

Slide 1- 29

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)


Slide 1- 30

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 controlss 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

1.3

More About Controls and Programming

As a Visual Basic programmer, you must design and create the applications 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
Form1

Label1 Label2 Label3

txtHoursWorked txtPayRate lblGrossPay

btnCalcGrossPay

btnClose
Slide 1- 38

Naming Rules

Naming rules Control names must start with a letter Remaining characters may be letters, digits, or underscores

Slide 1- 39

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

Slide 1- 40

Event Handler Compute Gross Pay


' 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

Slide 1- 41

Event Handler - Close


' 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

Slide 1- 42

1.4

Programming Process

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 users 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


Name txtHoursWorked txtPayRate lblGrossPay
btnCalcGrossPay btnClose (default) (default) (default) (default)

Type TextBox TextBox Label


Button Button Label Label Label Form

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
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

Slide 1- 46

Step 4 of Developing an Application

Define values for each control's relevant properties


Control Type Form Label Label Label Label TextBox TextBox Button Button Control Name (Default) (Default) (Default) (Default) lblGrossPay txtHoursWorked txtPayRate btnCalcGrossPay btnClose Text "Wage Calculator" "Number of Hours Worked" "Hourly Pay Rate" "Gross Pay Earned" "$0.00" "" "" "Calculate Gross Pay" "Close"

Slide 1- 47

Step 5 of Developing an Application

List event handlers needed for each control


Method btnCalcGrossPay_Click Description Multiplies hours worked by hourly pay rate These values are entered into txtHoursWorked and txtPayRate TextBoxes. Result is stored in lblGrossPay Text property.
Terminates the application

btnClose_Click

Slide 1- 48

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- 49

Step 6 of Developing an Application (cont.)

Flowchart is a diagram that graphically depicts the flow of the method


Multiply hours worked by hourly pay rate, store result in grossPay Copy value in grossPay to lblGrossPay Text property

Start

End

Slide 1- 50

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

1.5

Visual Studio and the Visual Basic Environment

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 icons 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

Solution Explorer
Design Window Toolbox Properties Window

Visual Basic Environment

Environment Toolbox Design Window Solution Explorer Window Properties Window Dynamic Help Window Docked and Floating Windows

Slide 1- 65

Toolbox, Form with Controls, Properties


Title Bar Menu Bar Standard Toolbar

Toolbox

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

Summary

Topics covered Introduction Computer Systems

Hardware and Software

Programs and Programming Languages Controls and Programming Programming Process Visual Studio

Slide 1- 73

S-ar putea să vă placă și