VB Lecture 1
VB Lecture 1
1
WHAT IS VISUAL BASIC?
Visual Basic is a tool that allows you to develop Windows
(Graphic User Interface - GUI) applications. The
applications have a familiar appearance to the user.
Visual Basic is a graphical user interface programming
language for creating software systems that run under
Microsoft Windows environment.
VB has the following features:
2
CONT’ED
Visual Basic is governed by an event processor. Nothing
happens until an event is detected.
Once an event is detected, the code corresponding to that
event (event procedure) is executed. Program control is
then returned to the event processor.
3
CONT’ED
Event
Event processor
4
CONT’ED
2. It has a collection of tools that used by programmers. These
tools are called controls. the controls can be accessed easily
by clicking their icons on the toolbox to select them.
3. It has special objects called forms that have a title bar at
the top. The programmer can then add controls such as a
menu bar, status bar, toolbar, buttons, and slide bars to
the form when creating an application
5
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
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
6
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 (a vast improvement over previous versions)
was released in 1993.
Visual Basic 4.0 released in late 1995 (added 32 bit application
support).
Visual Basic 5.0 released in late 1996. New environment,
supported creation of ActiveX controls, deleted 16 bit
application support.
And, now Visual Basic 6.0 - some identified new features of
Visual Basic 6.0:
Faster compiler
New ActiveX data control object
Allows database integration with wide variety of applications
New data report designer
New Package & Deployment Wizard 7
Additional internet capabilities
STARTING MICROSOFT VISUAL BASIC
Start VB from the programs menu by pointing to microsoft
Visual Studio.
Then select Microsoft Visual Basic.
8
FEATURES OF VISUAL BASIC INTEGRATED
DEVELOPMENT ENVIRONMENT(IDE)
WINDOW
9
STRUCTURE OF A VISUAL BASIC APPLICATION
Project (.VBP, .MAK)
Form 1 (.FRM) Form 2 (.FRM) Form 3 (.FRM) Module 1 (.BAS)
Control 1
Control 1
Control 1
Control 1
Control 1
Control 1
Control 1
Control 1
Control 1
10
APPLICATION (PROJECT)
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 is specified by a
property. Example properties include names, captions, size, color,
position, and contents. Visual Basic applies default properties. You can
change properties at design time or run time.
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.
11
STEPS IN DEVELOPING APPLICATION
There are three primary steps involved in building a Visual
Basic application:
1. Draw the user interface
2. Assign properties to controls
3. Attach code to controls
13
Code Editor Tasks Object Form position
Run Stop Browser
Form dimensions
Pause Project Form
Menu Save
Add Explorer Layout
editor project Toolbox
project
Properties
New Open window
form project
14
CONT’ED
3.The Toolbox is the selection menu for controls used in
your application.
Pointer: this is not actually a control but a tool used to
select an object or objects to be worked on.
Picture box: a picture box is used to display graphical
objects or text and to initiate event actions. It is similar to
an image box but has more properties, redraws slower and
cannot be stretched.
Label: a label displays text on a form. The text cannot be
reassigned during program execution though its
appearance can be altered.
Text box: provides a means of entering or displaying text.
The text may be already available or can be entered
during the program execution. 15
Pointer Picture Box
Check Box
List Box
Option Button
Horizontal Scroll Bar
Shapes
Lines
16
VB TOOLBOX…CONT’ED
Frame: is a tool rather than a control used as a container
for a group of control.
Command button: a control used to activate an event
driven procedure.
Check box: as opposed to the option buttons a check box is
used if more than one selection are to be used.
Option button: are used when one selection must be made
before an action is carried out.
Combo box: is a special type of list box that combines the
capabilities of a text box and a list box. it provides a list of
text items for selection by the user during program
execution. items can also be added during program
execution.
17
VB TOOLBOX…CONT’ED
List box: provides a list of text items for selection by the
user.
Horizontal scroll bar: is used for drawing horizontal
scroll bars on a form.
Vertical scrollbar: is used for drawing vertical scroll bar
on a form.
Timer: allows timed events to occupy repeatedly at a
specific time intervals.
Drive list box: provides a means of selecting a drive from
a list of existing drives.
Directory list box: a directory list box provides a way of
selecting directory from a list.
File list box: it provides a way of selecting files within a
18
group of files.
VB TOOLBOX…CONT’ED
Shape tool: is used to draw circles, ellipses, squares and
rectangles within forms.
Line tool: is used when drawing straight lines within
forms.
Image box: is used to display graphical objects.
19
CONT’ED
4. The Properties Window is used to establish initial property values for
objects. The drop-down box at the top of the window lists all objects in
the current form.
Two views are available: Alphabetic and Categorized. Under this
box are the available properties for the currently selected object.
20
CONT’ED
5. The Form Layout Window shows where (upon program
execution) your form will be displayed relative to your
monitor’s screen.
It allows you to specify the screen location of the form when
the project is executed. This can be done by dragging the
form in the window to the desired location.
21
CONT’ED
6. The Project Window displays a list of all forms and
modules making up your application.
You can also obtain a view of the Form or Code windows
(window containing the actual Basic coding) from the
Project window.
22
SAVING A VISUAL BASIC PROJECT
To save a new VB project for the first time, select the Save
Project As command from the file menu.
The Save File As dialog box will appear.
You first have to enter a form name e.g. book and then
click the Save button.
23
WAYS TO PLACE CONTROLS ON A FORM
There are two ways to place controls on a form:
Double-click the tool in the toolbox and it is created with a
default size on the form. You can then move it or resize it.
Click the tool in the toolbox, then move the mouse pointer
to the form window. The cursor changes to a crosshair.
Place the crosshair at the upper left corner of where you
want the control to be, press the left mouse button and hold
it down while dragging the cursor toward the lower right
corner. When you release the mouse button, the control is
drawn.
24
CONT’ED
To move a control you have drawn, click the object in the
form window and drag it to the new location. Release the
mouse button.
To resize a control, click the object so that it is select and
sizing handles appear. Use these handles to resize the
object.
Use sizing
Click here to
handles to
move object
resize object
25
SETTING PROPERTIES OF OBJECTS AT DESIGN
TIME
Each form and control has properties assigned to it by default when
you start up a new project. There are two ways to display the
properties of an object. The first way is to click on the object (form or
control) in the form window. Then, click on the Properties Window
or the Properties Window button in the tool bar. The second way is
to first click on the Properties Window. Then, select the object from
the Object box in the Properties Window. Shown is the Properties
Window for the stopwatch application:
The drop-down box at the top of the Properties Window is the Object
box. It displays the name of each object in the application as well as its
type. This display shows the Form object. The Properties list is
directly below this box. In this list, you can scroll through the list of
properties for the selected object. You may select a property by
clicking on it. Properties can be changed by typing a new value or
choosing from a list of predefined settings (available as a drop down
list). Properties can be viewed in two ways: Alphabetic and
26
Categorized.
CONT’ED
27
CONT’ED
A very important property for each object is its name. The name
is used by Visual Basic to refer to a particular object in code.
A convention has been established for naming Visual Basic
objects. This convention is to use a three letter prefix
(depending on the object) followed by a name you assign. A few
of the prefixes are (we’ll see more as we progress in the class):
28
CONT’ED
Object Prefix Example
Form frm frmWatch
Command Button cmd, btn cmdExit,
btnStart
Label lbl lblStart, lblEnd
Text Box txt txtTime,
txtName
Menu mnu mnuExit,
mnuSave
Check box chk chkChoice
29
CONT’ED
cbo combo box chk check box
cmd command button dir directory
drv drive list box fil file list box
fra frame
frm form grd grid
hsb horizontal scrollbar img image
lbl label lin line
lst list box mnu menu
ole OLE client opt option button
pic picture box shp shape
tmr timer txt text box
vsb vertical scrollbar 30
Object names can be up to 40 characters long, must start
with a letter, must contain only letters, numbers, and the
underscore (_) character. Names are used in setting
properties at run time and also in establishing procedure
names for object events.
Example 1-1
Stopwatch Application - Drawing Controls
1. Start a new project. The idea of this project is to start a
timer, then stop the timer and compute the elapsed time (in
seconds).
2. Place three command buttons and six labels on the form.
Move and size the controls and form so it looks something
like this:
31
EXAMPLE
32
SETTING PROPERTIES AT RUN TIME
You can also set or modify properties while your
application is running. To do this, you must write some
code. The code format is:
ObjectName.Property = NewValue
frmStart.BackColor = BLUE
33
HOW NAMES ARE USED IN OBJECT
EVENTS
The names you assign to objects are used by Visual Basic to
set up a framework of event-driven procedures for you to
add code to. The format for each of these subroutines (all
object procedures in Visual Basic are subroutines) is:
Visual Basic provides the Sub line with its arguments (if
any) and the End Sub statement. You provide any needed
code.
34
VARIABLES
Is a memory location referred to by name used to hold a
value that is subject to change during program execution.
When a variable is declared in a program, the computer
sets aside memory space to hold a value in the variable.
The variable may be a numeric constant, a character, a
string or any other data item.
35
RULES USED IN NAMING VARIABLES
They may include letters, numbers, and underscore (_)
The first character must be a letter
36
CONT’ED
A variable is declared using the reserved word Dim.
Dim is the short form of the word dimension (size).
e.g.
Dim FirstName as string
this means that the variable Firstname is of the type string.
37
VISUAL BASIC DATA TYPES
Data Type Suffix
Boolean None
Integer %
Long (Integer) &
Single (Floating) !
Double (Floating) #
Currency @
Date None
Object None
String $
Variant None
38
VARIABLE DECLARATION
There are three ways for a variable to be typed (declared):
Default
Implicit
Explicit
If variables are not implicitly or explicitly typed, they are assigned the
variant type by default. The variant data type is a special type used
by Visual Basic that can contain numeric, string, or date data.
To implicitly type a variable, use the corresponding suffix shown
above in the data type table. For example,
TextValue$ = "This is a string"
creates a string variable, while
Amount% = 300
creates an integer variable.
39
CONT’ED
There are many advantages to explicitly typing variables.
Primarily, we insure all computations are properly done,
mistyped variable names are easily spotted, and Visual Basic
will take care of insuring consistency in upper and lower case
letters used in variable names. Because of these advantages,
and because it is good programming practice, we will
explicitly type all variables.
To explicitly type a variable, you must first determine its scope. The
scope refers to the level of the program that a variable, a constant or a
procedure is recognised. There are four levels of scope:
Procedure level
Procedure level, static
Form and module level
Global level
40
CONT’ED
Within a procedure, variables are declared using the Dim
statement:
Dim MyInt as Integer
Dim MyDouble as Double
Dim MyString, YourString as String
41
CONT’ED
Form (module) level variables retain their value and are
available to all procedures within that form (module). Form
(module) level variables are declared in the declarations part of
the general object in the form's (module's) code window. The
Dim keyword is used:
Dim MyInt as Integer
Dim MyDate as Date
Global level variables retain their value and are available to all
procedures within an application. Module level variables are
declared in the declarations part of the general object of a
module's code window. (It is advisable to keep all global
variables in one module.) Use the Global keyword:
Global MyInt as Integer
Global MyDate as Date
42