0% found this document useful (0 votes)
78 views

Computer Programming - Visual Basic

The document discusses the basics of developing a Visual Basic application, including drawing the user interface with forms and controls, setting control properties, and attaching code to events. It provides examples of

Uploaded by

brosnan0426
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Computer Programming - Visual Basic

The document discusses the basics of developing a Visual Basic application, including drawing the user interface with forms and controls, setting control properties, and attaching code to events. It provides examples of

Uploaded by

brosnan0426
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Computer Studies

Chapter 1
Introduction to the Visual Basic
Language and Environment
What is Visual Basic?
• a tool that allows you to develop
Windows (Graphic User Interface - GUI)
applications.
• It is event-driven, meaning code remains
idle until called upon to respond to some
event (button pressing, menu selection, ...).
• It is governed by an event processor.
Event Processor

Once an event is detected, the code


corresponding to that event (event procedure)
is executed. Program control is then returned
to the event processor.
Some Features of Visual Basic
• Full set of objects - you 'draw' the application
• Lots of icons and pictures for your use
• Response to mouse and keyboard actions
• Clipboard and printer access
• Full array of mathematical, string handling,
and graphics functions
• Can handle fixed and dynamic variable and
control arrays
Some Features of Visual Basic
• Sequential and random access file support
• Useful debugger and error-handling facilities
• Powerful database access tools
• ActiveX support
• Package & Deployment Wizard makes
distributing your applications simple
Visual Basic 6.0 versus Other Versions
of Visual Basic
• The original Visual Basic for DOS and Visual
Basic For Windows were introduced in 1991.
• Visual Basic 3.0 - 1993.
• Visual Basic 4.0 - 1995 (added 32 bit
application support).
• Visual Basic 5.0 - 1996. New environment,
supported creation of ActiveX controls,
deleted 16 bit application support.
Structure of a Visual Basic Application
Application (Project) is made up of:
• Forms - Windows that you create for user
interface.
• Controls - Graphical features drawn on forms
to allow user interaction (text boxes, labels,
scroll bars, command buttons, etc.) (Forms
and Controls are objects.)
• Properties - Every characteristic of a form or
control. Example properties include names,
captions, size, color, position, and contents.
Application (Project) is made up of:
• Methods - Built-in procedure that can be invoked
to impart some action to a particular object.
• Event Procedures - Code related to some object.
This is the code that is executed when a certain
event occurs.
• General Procedures - Code not related to objects.
This code must be invoked by the application.
• Modules - Collection of general procedures,
variable declarations, and constant definitions
used by application.
Steps in Developing Application
1. Draw the user interface
2. Assign properties to controls
3. Attach code to controls
Drawing the User Interface and
Setting Properties
• Visual Basic operates in three modes.
– Design mode - used to build application
– Run mode - used to run the application
– Break mode - application halted and debugger is
available
Main Window
Form Window
• Is central to developing Visual Basic
applications. It is where you draw your
application.
Toolbox
• Is the
selection
menu for
controls used
in your
application.
Properties Window
• is used to
establish
initial
property
values for
objects.
Form Layout Window
• shows where (upon
program execution)
your form will be
displayed relative to
your monitor’s
screen:
Project Window
• displays a list of
all forms and
modules
making up your
application.
Exercise 1
Hello World!
• Create a simple program that will display
“Hello World!” using Visual Basic
Exercise 2
Stopwatch Application - Drawing
Controls
A Brief History of Basic
• Language developed in early 1960's at
Dartmouth College
– B (eginner's)
– A (All-Purpose)
– S (Symbolic)
– I (Instruction)
– C (Code)
• Answer to complicated programming languages
(FORTRAN, Algol, Cobol ...).First timeshare
language.
A Brief History of Basic
• In the mid-1970's, two college students write
first 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 on
that early version. Examples include: GW-
Basic, QBasic, QuickBasic.
• Visual Basic was introduced in 1991.
Visual Basic Statements and
Expressions
• The simplest statement is the assignment
statement. It consists of a variable name,
followed by the assignment operator (=),
followed by some sort of expression.
• Examples:
– StartTime = Now
– Explorer.Caption = "Captain Spaulding"
– BitCount = ByteCount * 8
– Energy = Mass * LIGHTSPEED ^ 2
– NetWorth = Assets - Liabilities
Comment statements
begin with the keyword Rem or a single quote(')
Example
Rem This is a remark
' This is also a remark
x = 2 * y ' another way to write a remark or comment
Visual Basic Operators
Operator Operation
^ Exponentiation
* / Multiplication and division
\ Integer division (truncates)
Mod Modulus
+ - Addition and subtraction
Parentheses around expressions can change
precedence.
Concatenation Operator
• To concatentate two strings, use the &
symbol or the + symbol:

Example:
lblTime.Caption = "The current time is" &
Format(Now, “hh:mm”)
txtSample.Text = "Hook this “ + “to this”
Comparison operators
Operator Comparison
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
The result of a comparison operation is a
Boolean value (True or False).
Logical operators
Operator Operation
Not Logical not
And Logical and
Or Logical or
The Not operator simply negates an operand.
The And operator returns a True if both operands are
True. Else, it returns a False.
The Or operator returns a True if either of its operands
is True, else it returns a False.
Common Visual Basic Functions
• Format Date or number converted to a text
string
• Now Current time and date
• Sqr Square root of a number
• Str Number converted to a text string
• Time Current time as a text string
• Val Numeric value of a given text
string
Exercise 3
Savings Account
Visual Basic Branching
• Branching statements are used to cause
certain actions within a program if a certain
condition is met.

• The if statement is used to check a condition


and if the condition is true, we run a block of
statements (called the if-block), else we
process another block of statements (called
the else-block). The else clause is optional.
If Statements
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 than
zero, the statement “You are overdrawn” is
printed.
If Statements
• You can also have If/Then/End If blocks to
allow multiple statements:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
End If
• In this case, if Balance - Check is less than
zero, two lines of information are printed.
If Statements
If/Then/Else/End If blocks:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
Else
Balance = Balance - Check
End 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.
If Statements
Or, we can add the ElseIf statement:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
ElseIf Balance - Check = 0 Then
Print "Whew! You barely made it"
Balance = 0
Else
Balance = Balance - Check
End If
Select Case - Another Way to
Branch
• Select Case format can be used when there
are multiple selection possibilities.
• Runs one of several groups of statements,
depending on the value of an expression.
Select [ Case ] testexpression
[ Case expressionlist [ statements ] ]
[ Case Else [ elsestatements ] ]
End Select
If Age = 5 Then using the If statement:
Category = "Five Year Old"
ElseIf Age >= 13 and Age <= 19 Then
Category = "Teenager"
ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >=
60 and Age <= 65)
Then
Category = "Special Adult"
ElseIf Age > 65 Then
Category = "Senior Citizen"
Else
Category = "Everyone Else"
End If
Select Case Age
code with Select Case
Case 5
Category = "Five Year Old"
Case 13 To 19
Category = "Teenager"
Case 20 To 35, 50, 60 To 65
Category = "Special Adult"
Case Is > 65
Category = "Senior Citizen"
Case Else
Category = "Everyone Else"
End Select
Exercise 4
Password validation

You might also like