The Integrated Development Environment: Microsoft Visual Basic 6.0 History of Basic
The Integrated Development Environment: Microsoft Visual Basic 6.0 History of Basic
The Integrated Development Environment: Microsoft Visual Basic 6.0 History of Basic
0
History of Basic
Basic Beginners All-purpose Symbolic Instruction Code (May 1964)
MS-Basic 2.0 (July 1975)
Visual Basic 1.0 (1991)
Visual Basic 6.0 (1998)
VISUAL BASIC
is a high level programming language which evolved from the earlier DOS
version called BASIC.
is an object-oriented programming language.
Menu Bar
Tool Bar
Project Explorer
Properties window
Toolbox
Form Designer
Object Browser
In previous versions of Visual Basic, the IDE was designed as a Single Document
Interface (SDI). In a Single Document Interface, each window is a free-floating window
that is contained within a main window and can move anywhere on the screen as long
as Visual Basic is the current application. But, in Visual Basic 6.0, the IDE is in a
Multiple Document Interface (MDI) format. In this format, the windows associated with
the project will stay within a single container known as the parent. Code and form-based
windows will stay within the main container form.
Figure 1 The Visual Basic startup dialog box
Menu Bar
This Menu Bar displays the commands that are required to build an application. The
main menu items have sub menu items that can be chosen when needed. The toolbars
in the menu bar provide quick access to the commonly used commands and a button in
the toolbar is clicked once to carry out the action represented by it.
Toolbox
The Toolbox contains a set of controls that are used to place on a Form at design time
thereby creating the user interface area. Additional controls can be included in the
toolbox by using the Components menu item on the Project menu. A Toolbox is
represented in figure 2 shown below.
Figure 2 Toolbox window with its controls available commonly.
Control
Description
Pointer
PictureBox
TextBox
Frame
CommandButton
CheckBox
OptionButton
ListBox
ComboBox
HScrollBar and
VScrollBar
Timer
DriveListBox
Displays the valid disk drives and allows the user to select
one of them.
DirListBox
FileListBox
Shape
Line
Image
Data
OLE
Label
Project Explorer
Docked on the right side of the screen, just under the tollbar, is the Project Explorer
window. The Project Explorer as shown in in figure servres as a quick reference to the
various elements of a project namely form, classes and modules. All of the object that
make up the application are packed in a project. A simple project will typically contain
one form, which is a window that is designed as part of a program's interface. It is
possible to develop any number of forms for use in a program, although a program may
consist of a single form. In addition to forms, the Project Explorer window also lists code
modules and classes.
Figure 3 Project Explorer
Properties Window
The Properties Window is docked under the Project Explorer window. The Properties
Window exposes the various characteristics of selected objects. Each and every form in
an application is considered an object. Now, each object in Visual Basic has
characteristics such as color and size. Other characteristics affect not just the
appearance of the object but the way it behaves too. All these characteristics of an
object are called its properties. Thus, a form has properties and any controls placed on
it will have propeties too. All of these properties are displayed in the Properties Window.
Object Browser
The Object Browser allows us to browse through the various properties, events and
methods that are made available to us. It is accessed by selecting Object Browser from
the View menu or pressing the key F2. The left column of the Object Browser lists the
objects and classes that are available in the projects that are opened and the controls
that have been referenced in them. It is possible for us to scroll through the list and
select the object or class that we wish to inspect. After an object is picked up from the
Classes list, we can see its members (properties, methods and events) in the right
column.
A property is represented by a small icon that has a hand holding a piece of paper.
Methods are denoted by little green blocks, while events are denoted by yellow lightning
bolt icon.
Object naming conversions of controls (prefix)
Form -fmr
Label-lbl
TextBox-txt
CommandButton-cmd
CheckBox -chk
OptionButton -opt
ComboBox -cbo
ListBox-lst
Frame-fme
PictureBox -pic
Image-img
Shape-shp
Line -lin
HScrollBar -hsb
VScrollBar -vsb
Visual Basic uses building blocks such as Variables, Data Types, Procedures, Functions
and Control Structures in its programming environment. This section concentrates on
the programming fundamentals of Visual Basic with the blocks specified.
Modules
Code in Visual Basic is stored in the form of modules. The three kind of modules are
Form Modules, Standard Modules and Class Modules. A simple application may contain
a single Form, and the code resides in that Form module itself. As the application
grows, additional Forms are added and there may be a common code to be executed in
several Forms. To avoid the duplication of code, a separate module containing a
procedure is created that implements the common code. This is a standard Module.
Class module (.CLS filename extension) are the foundation of the object oriented
programming in Visual Basic. New objects can be created by writing code in class
modules. Each module can contain:
Declarations : May include constant, type, variable and DLL procedure declarations.
Procedures : A sub function, or property procedure that contain pieces of code that can
be executed as a unit.
These are the rules to follow when naming elements in VB - variables, constants,
controls, procedures, and so on:
May be as much as 255 characters long (but don't forget that somebody has to
type the stuff!).
Must not be a reserved word (that is part of the code, like Option, for example)
The dash, although legal, should be avoided because it may be confused with
the minus sign. Instead of First-name use First_name or FirstName.
Data types in Visual Basic 6
By default Visual Basic variables are of variant data types. The variant data type can
store numeric, date/time or string data. When a variable is declared, a data type is
supplied for it that determines the kind of data they can store. The fundamental data
types in Visual Basic including variant are integer, long, single, double, string, currency,
byte and boolean. Visual Basic supports a vast array of data types. Each data type has
limits to the kind of information and the minimum and maximum values it can hold. In
addition, some types can interchange with some other types. A list of Visual Basic's
simple data types are given below.
1. Numeric
Byte
Integer
Long
Single
Double
Store large floating value which exceeding the single data type
value
Currency
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 and 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. These are not stored as numeric
values and cannot be used as such. Values are internally stored as -1 (True) and 0
(False) and any non-zero value is considered as true.
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.
Operators in Visual Basic
Arithmetical Operators
Operators
Description
Example
Result
Add
5+5
10
Substract
10-5
Divide
25/5
Integer Division
20\3
Multiply
5*4
20
27
Mod
Remainder of
division
20 Mod 6
&
String
concatenation
"George"&"
"&"Bush"
"George Bush"
Relational Operators
Operators
Description
Example
Result
>
Greater than
10>8
True
<
Less than
10<8
False
>=
Greater than or
equal to
20>=10
True
<=
True
<>
Not Equal to
5<>4
True
Equal to
5=7
False
Logical Operators
Operators
OR
Description
Operation will be true if either of the operands is true
AND
For example,
Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22
The Len function can also return the number of digits or memory locations of a
number that is stored in the computer. For example,
Private sub Form_Activate ( )
X=sqr (16)
Y=1234
Z#=10#
Print Len(x), Len(y), and Len (z)
End Sub
will produce the output 1, 4 , 8. The reason why the last value is 8 is because z# is a
double precision number and so it is allocated more memory spaces.
(ii) The Right Function
The Right function extracts the right portion of a phrase. The format is
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. For example,
Right(Visual Basic, 4) = asic
(iii)The Left Function
The Left$ function extract the left portion of a phrase. The format is
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. For example,
Left (Visual Basic, 4) = Visu
(iv) The Ltrim Function
The Ltrim function trims the empty spaces of the left portion of the phrase. The format is
Ltrim(Phrase)
.For example,
Ltrim ( Visual Basic, 4)= Visual basic
, 4) = Visual basic
The Ttrim function trims the empty spaces on both side of the phrase. The format is
Trim(Phrase)
.For example,
Trim ( Visual Basic
) = Visual basic
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 Bas
(ix) The InStr function
The InStr function looks for a phrase that is embedded within the original phrase and
returns the starting position of the embedded phrase. The format is
Instr (n, original phase, embedded phrase)
Where n is the position where the Instr function will begin to look for the embedded
phrase. For example
Instr(1, Visual Basic, Basic)=8
(x) 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
(xi) The Str and Val functions
The Str is the function that converts a number to a string while the Val function converts
a string to a number. The two functions are important when we need to perform
mathematical operations.
(xii) The Chr and the Asc functions
The Chr function returns the string that corresponds to an ASCII code while
the Asc function converts an ASCII character or symbol to the corresponding ASCII
code. ASCII stands for American Standard Code for Information Interchange.
Altogether there are 255 ASCII codes and as many ASCII characters. Some of the
characters may not be displayed as they may represent some actions such as the
pressing of a key or produce a beep sound. The format of the Chr function is
Chr(charcode)
and the format 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
A function is similar to a normal procedure but the main purpose of the function is to
accept a certain input from the user and return a value which is passed on to the main
program to finish the execution. There are two types of functions, the built-in functions
(or internal functions) and the functions created by the programmers.
10.1
10.2
MsgBox ( ) Function
The objective of MsgBox is to produce a pop-up message box and prompt the user to
click on a command button before he /she can continues. This format is as follows:
yourMsg=MsgBox(Prompt, Style Value, Title)
The first argument, Prompt, will display the message in the message box. The Style
Value will determine what type of command buttons appear on the message box,
please refer Table 10.1 for types of command button displayed. The Title argument will
display the title of the message board.
Table 10.1: Style Values
Style Value Named Constant
Buttons Displayed
vbOkOnly
Ok button
vbOkCancel
vbAbortRetryIgnore
vbYesNoCancel
vbYesNo
vbRetryCancel
We can use named constant in place of integers for the second argument to make the
programs more readable. In fact, VB6 will automatically shows up a list of names
constant where you can select one of them.
Example: yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")
and yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")
are the same.
yourMsg is a variable that holds values that are returned by the MsgBox ( ) function.
The values are determined by the type of buttons being clicked by the users. It has to
be declared as Integer data type in the procedure or in the general declaration
section. Table 10.2 shows the values, the corresponding named constant and buttons.
Button Clicked
vbOk
Ok button
vbCancel
Cancel button
vbAbort
Abort button
vbRetry
Retry button
vbIgnore
Ignore button
vbYes
Yes button
vbNo
No button
Value
Example 10.1
i. The Interface:
You draw three command buttons and a label as shown in Figure 10.1
Figure 10.1
To make the message box looks more sophisticated, you can add an icon besides the
message. There are four types of icons available in VB as shown in Table 10.3
Table 10.3
Value
Named Constant
16
vbCritical
32
vbQuestion
48
vbExclamation
64
vbInformation
Icon
Example 10.2
You draw the same Interface as in example 10.1 but modify the codes as follows:
The
InputBox( ) Function
An InputBox( ) function will display a message box where the user can enter a value or
a message in the form of text. The format is
myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)
myMessage is a variant data type but typically it is declared as string, which accept the
message input by the users. The arguments are explained as follows:
Prompt
Title
default-text - The default text that appears in the input field where users can use
it as his intended input or he may change to the message he wish to key in.
x-position and y-position - the position or the coordinate of the input box.
Example 10.3
i. The Interface
Figure 10.4