IN THIS U CAN FIND
* Integrated Development Environment
* IDE COMPONENTS
* Features of Visual Basic
* Steps in Developing Application
* creating controls
* Access keys
* VARIABLES AND DATA TYPES
* STRING MANIPULATION FUNCTIONS
1. Chapter 1
Introduction to Visual Basic
1
R. BHUVANESWARI
ASST.PROFESSOR
DEPT OF COMPUTER SCIENCE AND APPLICATION
DKM COLLEGE FOR WOMEN
(AUTONOMOUS)
VELLORE
2. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 2
Objectives
Machine language vs.. High-level language
Procedure-oriented, object-oriented, and
event-driven languages
Background of Visual Basic
VB Integrated Development Environment
Online documentation and Help features
3. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 3
Application Programs
Program - detailed set of instructions for a
computer to execute
Application programs (applications or apps) -
self-contained collection of programs that
perform a task for the end user
Programming language - formal language used
to give instructions to computers
4. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 4
History of Programming
Languages
Machine language
Procedure-oriented languages
Object-oriented languages
Event-driven languages
Natural languages
5. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 5
History of Programming
Languages
Machine Language
different for each computer processor
Procedure-Oriented Languages
FORTRAN
COBOL
Pascal
C
Ada
6. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 6
History of Programming
Languages
Object-oriented languages
Smalltalk
C++
Ada 95
Event-driven languages
Visual Basic
most Visual languages
7. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 7
History of Programming
Languages
Natural languages
computer will accept a user’s native or
natural language, such as English
Researchers continue to work in this
area
8. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 8
Background of Visual Basic
BASIC
Beginner’s All-purpose Symbolic
Instruction Code
By John Kemeny and Thomas Kurtz
Designed to teach programming to
beginners
BASIC language interpreter, one of
Microsoft’s first products
9. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 9
Background of Visual Basic
BASIC – (Beginners' All-purpose Symbolic Instruction Code)
Included with DOS
QBASIC - included with DOS Version 5 -
subset of BASIC
Visual Basic
1992 - Visual Basic 1 for Windows 3
Visual Basic 4 - for Windows 95
Visual Basic 5 - for Office 97
Visual Basic 6 - for Windows 98 & Office 2000
10. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 10
Visual Basic Editions
Learning Edition
Professional Edition
Enterprise Edition
11. Integrated Development Environment (IDE):
Contains all the tools and features
needed to create, run, and test programs
It is called integrated because we can
access virtually all of the development
tools that we need from one screen
called an interface.
The IDE is also commonly referred to
as the design environment, or
the program.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 11
12. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 12
VB IDE Components
Major windows
Form Designer
Form Layout
Toolbox
Project Explorer
Object Browser
Properties
Code Editor
Immediate, Locals, Watch
13. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 13
Toolbox
Form
Designer
Window
Code Editor
Window
Project
Explorer
Properties
window
Form Layout
Window
Visual Basic 6 Interactive Development Environment
14. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 14
VB IDE Components
Title bar: contains tile of the project/file
Menu Bar: contains all commands needed to run VB (File,
Edit, View etc…).
Toolbars: quick access to commonly used menu commands.
Project Explorer: displays the project’s components.
Toolbox: contains icons of the controls that can be placed on
the project’s Forms to create the application’s interface.
Properties Window: Shows the property settings of the
selected control (size, caption, color, etc…).
15. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 15
VB IDE Components
Code Window : The place where the code is written is Code
Window. To open the code window of a particular object,
just double click on that object.
Form Layout Window :Using this window we can position
forms as we want them to appear on the screen.
Form Designer window: These are just windows in which a
particular form appears. We can place objects on a form by
double clicking on them.
16. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 16
VB IDE Components (Tool box)
Control Description
Pointer Provides a way to move and resize the controls form
PictureBox Displays icons/bitmaps and metafiles. It displays text or acts as a visual container for other controls.
TextBox Used to display message and enter text.
Frame Serves as a visual and functional container for controls
CommandButton Used to carry out the specified action when the user chooses it.
CheckBox Displays a True/False or Yes/No option.
OptionButton OptionButton control which is a part of an option group allows the user to select only one option even it displays mulitiple choices.
ListBox Displays a list of items from which a user can select one.
ComboBox Contains a TextBox and a ListBox. This allows the user to select an ietm from the dropdown ListBox, or to type in a selection in the TextBox.
HScrollBar and VScrollBar These controls allow the user to select a value within the specified range of values
Timer Executes the timer events at specified intervals of time
DriveListBox Displays the valid disk drives and allows the user to select one of them.
DirListBox Allows the user to select the directories and paths, which are displayed.
FileListBox Displays a set of files from which a user can select the desired one.
Shape Used to add shape (rectangle, square or circle) to a Form
Line Used to draw straight line to the Form
Image used to display images such as icons, bitmaps and metafiles. But less capability than the PictureBox
Data Enables the use to connect to an existing database and display information from it.
OLE Used to link or embed an object, display and manipulate data from other windows based applications.
Label Displays a text that the user cannot modify or interact with.
17. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 17
VB IDE Components(project window)
The Project
Window displays a
list of all forms and
modules making up
your application.
18. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 18
VB IDE Components(property window)
Properties Window :
This is the place where we set the
object’s properties.
Ex : caption of a form or a command
button , text of a textbox
etc.
When we select an object, the
corresponding object’s properties will
appear in the properties window as
shown in above figure.
19. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 19
VB IDE Components(form design
window)
Form design window:
These are just windows in
which a particular form
appears.
We can place objects on a
form by double clicking on
them.
20. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 20
VB IDE Components(code window)
Code Window :
The place where the code is written is
Code Window.
To open the code window of a particular
object, just double click on that object.
There are two drop-down list boxes in
the code window.
1. Light list box lets to select the
object to add code into.
2. Right list box lets to select the
procedures to add all the methods the object
control supports.
21. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 21
VB IDE Components(form layout
window)
Form Layout Window :
Using this window we can
position forms as we want them
to appear on the screen.
For positioning the form , drag
the form on form layout
window to the required new
location.
22. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 22
Features of Visual Basic
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
23. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 23
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
24. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 24
Steps in Developing Application
There are three primary steps involved in building a
Visual Basic application:
1. Create the user interface
2. Assign properties to controls
3. Attach code to controls
25. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 25
Step 1:Create the user interface
Choose needed controls in Toolbox, drag it and drop it on the form at
the required location.
Creating controls on vb:
In two ways we can add controls to vb form
1. Double click
• We can place controls By double clicking the control on the tool box
(default size of the selected control will be displayed on the form design
window)
• it is created with a default size on the form.
• You can then move it or resize it.
2. Drag and drop
Using drag and drop method we can resize selected control by clicking and
dragging the mouse
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
26. 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.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 26
27. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 27
Step 2: Changing properties
Once the required controls are placed on the form,
we have to change the properties of the control to
get the required look and behavior.
Each control has a collection of properties.
Each property represents an attribute of the control.
For example, Name property identifies the name
used to reference the control in the code and so on
So to change the properties of control as needed
28. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 28
Step 3: Attach code to
controls
Write code on code window
The Visual Basic Code consists of
statements, and declarations.
29. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 29
creating controls
We can create many controls
Some of the controls are listed
below:
image controls – command buttons –
text boxes – message boxes –list
boxes – combo boxes.
30. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 30
creating controls (command buttons )
It is probably the most widely used control.
It is used to begin, interrupt, or end a particular process.
Command Button Properties:
Appearance - Selects 3-D or flat appearance.
Caption - String to be displayed on button.
Default Allows selection of button with Enter key (only one
button on a form can have this property True).
Font Sets font type, style, size.
Command Button Events:
Click Event – it is the widely used event on command button
it triggered when button is selected either by clicking on it or
by pressing the access key.
31. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 31
creating controls (text boxes )
A text box is used to display information
We can enter information at design time, by a user at
runtime, or assigned within code.
The displayed text may be edited.
32. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 32
Access keys
An access key is an underlined character in the text of a
menu, menu item, or the label of a control such as a button.
With an access key, the user can "click" a button by pressing
the Alt key in combination with the predefined key
Eg: File Edit Format
The underscored characters are access keys
& symbol is used to specify the access key character.
33. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 33
Editing tools
The Edit toolbar provides shortcuts to commonly used menu
items. These items are frequently used when editing code:
VBA/VB6 Edit Toolbar
34. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 34
ComboBox
A ComboBox combines the features of a TextBox and a
ListBox.
This enables the user to select either by typing text into
the ComboBox or by selecting an item from the list.
There are three types of ComboBox styles that are
represented as shown below.
1. Dropdown Combo (style 0)
2. Simple Combo (style 1)
3. Dropdown List (style 2)
35. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 35
Combo Box
Simple Combo (style 1)
It displays the contents of its list all the time. The user can
select an item from the list or type an item in the edit portion
of the combo box.
Dropdown Combo (style 0)
first appears as only an edit area with a down arrow button at
the right. The list portion stays hidden until the user clicks the
down-arrow button. The user can either select a value from the
list or type a value in the edit area.
Dropdown List combo (style 2)
The user can only select an item and cannot type anything in
the edit area. Anyway this area displays the selected item.
36. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 36
Variables
Variables are used by Visual Basic to hold information
needed by your application.
The value of the variable will change during the execution of
a program
The variable are used to store the value temporally in
memory location
Rules used in naming variables:
No more than 40 characters
They may include letters, numbers, and underscore (_)
The first character must be a letter
You cannot use a reserved word (word needed by Visual
Basic)
37. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 37
Visual Basic Data Types
VB SUPPORTS THE FOLLOWING FUNDAMENDAL DATA
TYPES:
1. Numeric 2. String 3. Date 4. Boolean 5. Variant
Numeric
Byte Store integer values in the range of 0 - 255
Integer Store integer values in the range of (-32,768) - (+ 32,767)
Long Store integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468)
Single Store floating point value in the range of (-3.4x10-38) - (+ 3.4x1038)
Double Store large floating value which exceeding the single data type value
Currency store monetary values. It supports 4 digits to the right of decimal point and 15 digits to the left
38. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 38
Visual Basic Data Types
2. String
Use to store alphanumeric values. A variable length string can store
approximately 4 billion characters
3.Date
Use to store date and time values.
A variable declared as date type can store both date and time
values
it can store date values 01/01/0100 up to 12/31/9999
4. Boolean
Boolean data types hold either a true or false value. Values are
internally stored as -1 (True) and 0 (False)
5. Variant
Stores any type of data and is the default Visual Basic data type.
In Visual Basic if we declare a variable without any data type by
default the data type is assigned as default.
39. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 39
Type declaration characters:
VB SUPPORTS THE FOLLOWING Type Declaration
Characters
One way to define any variables to use some special
symbol called type declaration character inside using the
data type names.
Data Type Type Declaration Character
String $
Integer %
Long &
Single !
Double #
Currency @
40. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 40
Variable Declaration
There are three ways for a variable to be typed (declared):
1. Default
2. Implicit
3. Explicit
Default
If variables are not implicitly or explicitly declared , 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.
Eg: Dim vntWhatever As Variant
Dim vntWhatever
.)
41. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 41
Variable Declaration
explicit
use the corresponding suffix shown above in the
data type table. For example,
TextValue$ = "This is a string“ (creates a string
variable)
Amount% = 300 ( creates an integer variable
Dim intCounter As Integer
Dim intCounter%
42. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 42
Variable Declaration
Implicit vs Explicit Declaration
dim x 'implicit
dim x as integer 'explicit
syntax for declaring a variable in VB :
[Dim Private | Public | Static |
Global] variablename [As datatype]
43. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 43
Scope of Variable
There are three levels of scope:
project-level (also called "global" or "application" scope) the
variable is accessible to all procedures in all modules of the
project.
module-level - the variable is accessible to all procedures in the
module in which it is declared
local-level - the variable is accessible only to the procedure in
which it is declared
Multiple Declarations on One Line
VB allows you to declare any number of variables on one line
Dim A, B, C As Integer
44. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 44
Constants
It is a space in memory filled with
fixed value that will not be changed.
Constant may be declared as:
Const constant name = value
EG:
Const p = 3.14159
45. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 45
Inputting variables
There are methods to input variable x
as stated in the following:
To enter many variables we usually
use the second method with loop
Method of input For all type of variable
text tool X=textno.text
input box X=inputbox("prompt","title"
)
46. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 46
Outputting variables
There are methods to output variable
x as stated in the following:
Method of output For all type of variable
On form Print x (in load event we
must use this statement)
to text tool textno.text =X
to label tool Labelno.caption=x
By message box msgbox (x)
Or msgbox ("remark"& x)
47. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 47
Displaying information on a
form
To display information on a form print statement is used.
The instruction print could be very helpful to display data
and used as follows:
Code Description example
print To leave one line and
print on next
Print "a", "b",
"c"
Use (,) to print a
distance
between outputs
a b c
Print "a"; "b";
"c"
Use (;) to print the
outputs adjacent
abc
Print "a","b";
Print "c"
Print a, b then print c
on
the same line
abc
48. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 48
String Manipulation Functions
1. Len Function
The Len function returns an integer value which is the
length of a phrase or a sentence, including the empty spaces.
Syntax:
Len (“Phrase”)
EG:
Len (“VisualBasic”) = 11
Len (“welcome to VB tutorial”) = 22
49. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 49
String Manipulation Functions
2.The Right Function
The Right function extracts the right portion of a phrase.
syntax:
Right (“Phrase”, n)
Where n is the starting position from the right of the
phrase where the portion of the phrase is going to be
extracted
example,
Right(“Visual Basic”, 4) = asic
50. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 50
String Manipulation Functions
3. The Left Function
The Left function extract the left portion of a
phrase.
syntax:
Left(“Phrase”, n)
Where n is the starting position from the left of
the phase where the portion of the phrase is going
to be extracted.
example,
Left (“Visual Basic”, 4) = Visu
51. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 51
String Manipulation Functions
4. The Ltrim Function
The Ltrim function trims the empty spaces of the left portion of the phrase. syntax is
Ltrim(“Phrase”)
example,
Ltrim (“ Visual Basic”, 4)= Visual basic
5.The Rtrim Function
The Rtrim function trims the empty spaces of the right portion of the phrase. The syntax
is
Rtrim(“Phrase”)
example,
Rtrim (“Visual Basic ”, 4) = Visual basic
6.The Trim function
The Trim function trims the empty spaces on both side of the phrase. The syntax is
Trim(“Phrase”)
example,
Trim (“ Visual Basic ”) = Visual basic
52. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 52
String Manipulation Functions
7. The Mid Function
The Mid function extracts a substring from the original phrase or string. It takes the
following format:
Mid(phrase, position, n)
Where position is the starting position of the phrase from which the extraction process
will start and n is the number of characters to be extracted. For example,
Mid(“Visual Basic”, 3, 6) = ual Ba
8. The Ucase and the Lcase functions
The Ucase function converts all the characters of a string to capital letters. On the other
hand, the Lcase function converts all the characters of a string to small letters. For
example,
Ucase(“Visual Basic”) =VISUAL BASIC
Lcase(“Visual Basic”) =visual basic
10. THE CHR AND ASC FUNCTIONS:
Chr(charcode)
and the syntax of the Asc function is
Asc(Character)
The following are some examples:
Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(“B”)=66, Asc(“&”)=38