Computer Programming - Visual Basic
-
Author
brosnan0426 -
Category
Documents
-
view
221 -
download
0
Embed Size (px)
Transcript of Computer Programming - Visual Basic

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 1/39
Computer StudiesChapter 1
Introduction to the Visual BasicLanguage and Environment

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 2/39
W hat is Visual Basic?
a tool that allows you to developWindows (Graphic User Interface - GU I)
applications.It is event-driven, meaning code remainsidle until called upon to respond to some
event (button pressing, menu selection, ...).It is governed by an event processor.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 3/39
Event Processor
Once an event is detected, the codecorresponding to that event (event procedure)is executed. Program control is then returnedto the event processor.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 4/39

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 5/39
Some Features of Visual Basic
Sequential and random access file supportUseful debugger and error-handling facilities
Powerful database access toolsActiveX supportPackage & Deployment Wizard makes
distributing your applications simple

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 6/39
Visual Basic 6.0 versus Other Versionsof Visual Basic
The original Visual Basic for DOS and VisualBasic For Windows were introduced in 1991.
Visual Basic 3.0 - 1993.Visual Basic 4.0 - 1995 (added 32 bitapplication support).
Visual Basic 5.0 - 1996. New environment,supported creation of ActiveX controls,deleted 16 bit application support.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 7/39
S tructure of a Visual Basic Application

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 8/39

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 9/39
Application (Project) is made up of:
Methods - Built-in procedure that can be invokedto impart some action to a particular object.Event Procedures - Code related to some object.This is the code that is executed when a certainevent occurs.G eneral Procedures - Code not related to objects.This code must be invoked by the application.Modules - Collection of general procedures,variable declarations, and constant definitionsused by application.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 10/39
S teps in Developing Application
1. Draw the user interface2. Assign properties to controls
3. Attach code to controls

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 11/39
Drawing the U ser Interface andS etting Properties
Visual Basic operates in three modes. ± Design mode - used to build application ± R
un mode - used to run the application ± Break mode - application halted and debugger is
available

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 12/39
Main W indow

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 13/39
Form W indow
Is central to developing Visual Basicapplications. It is where you draw yourapplication.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 14/39
Toolbox
Is theselectionmenu forcontrols usedin yourapplication.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 15/39
Properties W indow
is used toestablishinitialpropertyvalues forobjects.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 16/39
Form Layout W indow
shows where (uponprogram execution)your form will bedisplayed relative toyour monitor sscreen:

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 17/39
Project W indow
displays a list of all forms andmodulesmaking up yourapplication.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 18/39
Exercise 1Hello World!
Create a simple program that will displayHello World! using Visual Basic

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 19/39
Exercise 2S topwatch Application - Drawing
Controls

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 20/39
A Brief History of BasicLanguage developed in early 1960's atDartmouth College
± B (eginner's) ± A (All-Purpose) ± S (Symbolic) ± I (Instruction) ±
C (Code)Answer to complicated programming languages(FORTRAN, Algol, Cobol ...).First timesharelanguage.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 21/39
A Brief History of Basic
In the mid-1970's, two college students writefirst Basic for a microcomputer (Altair) - cost$350 on cassette tape. You may have heard of them: Bill Gates and Paul Allen!Every Basic since then essentially based onthat early version. Examples include: GW-Basic, QBasic, QuickBasic.Visual Basic was introduced in 1991.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 22/39
Visual Basic S tatements and
ExpressionsThe simplest statement is the assignmentstatement. It consists of a variable name,followed by the assignment operator (=),followed by some sort of expression.Examples:
± Start T ime = Now ± Explorer.Caption = "Captain Spaulding" ± BitCount = ByteCount * 8 ± Energy = Mass * LIGH TSPEE D ^ 2 ± NetWorth = Assets - Liabilities

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 23/39
Comment statements
begin with the keyword R em or a single quote(')Example
Rem T his is a remark' T his is also a remarkx = 2 * y ' another way to write a remark or comment

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 24/39
Visual Basic Operators
Operator Operation^ Exponentiation
* / Multiplication and division\ Integer division (truncates)Mod Modulus
+ - Addition and subtractionParentheses around expressions can change
precedence.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 25/39

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 26/39
Comparison operatorsOperator Comparison> Greater than< Less than> = Greater than or equal to<= Less than or equal to= Equal to<> Not equal toThe result of a comparison operation is a
Boolean value ( True or False).

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 27/39
Logical operators
Operator OperationNot Logical not
And Logical andOr Logical orT he Not operator simply negates an operand.T
he And operator returns aTrue if both operands areTrue. E lse, it returns a False.
T he Or operator returns a True if either of its operandsis True, else it returns a False.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 28/39
Common Visual Basic Functions
Format Date or number converted to atext string
Now Current time and dateSqr Square root of a numberStr Number converted to a text
stringT ime Current time as a text stringVal Numeric value of a given text
string

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 29/39
E xercise 3S avings Account

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 30/39
Visual Basic Branching
Branching statements are used to cause certainactions within a program if a certain conditionis met.
The if statement is used to check a conditionand if the condition is true, we run a block of statements (called the if-block ), else weprocess another block of statements (calledthe else-block ). The else clause is optional.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 31/39
If S tatements
The simplest is the If/ Then statement:If Balance - Check < 0 Then
Print "You are overdrawn³Here, if and only if Balance - Check is less thanzero, the statement You are overdrawn isprinted.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 32/39
If S tatements
You can also have If/ Then/End If blocks toallow multiple statements:
If Balance - Check < 0T
henPrint "You are overdrawn"Print "Authorities have been notified"
End If In this case, if Balance - Check is less thanzero, two lines of information are printed.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 33/39
If S tatementsIf/ Then/Else/End If blocks:If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"E lse
Balance = Balance - CheckEnd If
Here, the same two lines are printed if you are overdrawn (Balance -Check < 0),but, if you are not overdrawn ( Else), your new Balance is computed.

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 34/39
If S tatementsOr, we can add the ElseIf statement:If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"E lseIf Balance - Check = 0 Then
Print "Whew! You barely made it"
Balance = 0E lse
Balance = Balance - Check
End If

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 35/39
S elect Case - Another W ay toBranch
S elect Case format can be used when thereare multiple selection possibilities.
Runs one of several groups of statements,depending on the value of an expression.Select [ Case ] testexpression
[ Case expressionlist [ statements ] ][ Case E lse [ elsestatements ] ]
End Select

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 36/39
using the If statement:If Age = 5 T henCategory = "Five Year Old"
E lseIf Age > = 13 and Age <= 19 T henCategory = " Teenager"
E lseIf (Age > = 20 and Age <= 35) Or Age = 50 Or (Age > =60 and Age <= 65)
T henCategory = "Special Adult"
E lseIf Age > 65 T henCategory = "Senior Citizen"
E lseCategory = " Everyone E lse"
E nd If

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 37/39
code with S elect CaseSelect Case AgeCase 5
Category = "Five Year Old"Case 13 To 19
Category = " Teenager"
Case 20 To 35, 50, 60 To 65Category = "Special Adult"
Case Is > 65
Category = "Senior Citizen"Case E lse
Category = " Everyone E lse"E
nd Select

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 38/39
E xercise 4Password validation

8/8/2019 Computer Programming - Visual Basic
http://slidepdf.com/reader/full/computer-programming-visual-basic 39/39