Visual Basic Programming
Visual Basic Programming
1991. The final version was VB 6.0 before the released of VB.NET. VB is a user-friendly
programming language designed for beginners that enables anyone to develop GUI window
applications easily.
Step 1: Design the interface by adding controls to the form and set their properties
The Visual Basic environment consists of seven main windows which are listed as follows
1. Menu Bar
2. Toolbar
3. Toolbox
4. Form window
5. Code window
7. Properties window
Working With Controls in VISUAL BASIC PROGRAMMING 6.0
Before writing an event procedure for the control to response to an event, you have to set
certain properties for the control to determine its appearance and how will it work with the
event procedure. You can set the properties of the controls in the properties window or at
runtime.
The TextBox
The text box is the standard control for accepting input from the user as well as to display the
output. It can handle string (text) and numeric data but not images or pictures. A string
entered into a text box can be converted to a numeric data by using the function Val(text).
The following example illustrates a simple program that processes the input from the user.
The Label
The label is a very useful control for Visual Basic, as it is not only used to provide
instructions and guides to the users, it can also be used to display outputs. One of its most
important properties is Caption. Using the syntax Label.Caption, it can display text and
numeric data . You can change its caption in the properties window and also at runtime.
The Command Button
The command button is one of the most important controls as it is used to execute commands.
It displays an illusion that the button is pressed when the user click on it. The most common
event associated with the command button is the Click event, and the syntax for the
procedure is
In this program, we want to crack a secret passoword entered by the user. In the design phase,
insert a command button and change its name to cmd_ShowPass. Next, insert a TextBox and
rename it as TxtPassword and delete Text1 from the Text property. Besides that, set its
PasswordChr to *. Now, enter the following code in the code window.
The PictureBox
The Picture Box is one of the controls that is used to handle graphics. You can load a picture
at design phase by clicking on the picture item in the properties window and select the picture
from the selected folder. You can also load the picture at runtime using
the LoadPicture method. For example, the statement will load the picture grape.gif into the
picture box.
The Image Control is another control that handles images and pictures. It functions almost
identically to the pictureBox. However, there is one major difference, the image in an Image
Box is stretchable, which means it can be resized. This feature is not available in the
PictureBox. Similar to the Picture Box, it can also use the LoadPicture method to load the
picture. For example, the statement loads the picture grape.gif into the image box.
The ListBox
The function of the ListBox is to present a list of items where the user can click and select the
items from the list. In order to add items to the list, we can use the AddItem method. For
example, if you wish to add a number of items to list box 1, you can key in the following
statements
The ComboBox
The function of the Combo Box is also to present a list of items where the user can click and
select the items from the list. However, the user needs to click on the small arrowhead on the
right of the combo box to see the items which are presented in a drop-down list. In order to
add items to the list, you can also use the AddItem method. For example, if you wish to add
a number of items to Combo box 1, you can key in the following statements
The CheckBox
The Check Box control lets the user selects or unselects an option. When the Check Box is
checked, its value is set to 1 and when it is unchecked, the value is set to 0. You can include
the statements Check1.Value=1 to mark the Check Box and Check1.Value=0 to unmark the
Check Box, as well as use them to initiate certain actions. For example, the program in
Example 3.4 will show which items are selected in a message box.
The OptionButton
The OptionButton control also lets the user selects one of the choices. However, two or more
Option buttons must work together because as one of the option buttons is selected, the other
Option button will be unselected. In fact, only one Option Box can be selected at one time.
When an option box is selected, its value is set to “True” and when it is unselected; its value
is set to “False”.
In the following example, the shape control is placed in the form together with six
OptionButtons. To determine the shape of the shape control, we use the shape property. The
property values of the shape control are 0, 1, and 2,3,4,5 which will make it appear as a
rectangle, a square, an oval shape, a circle, a rounded rectangle and a rounded square
respectively.
The DriveListBox
The DriveListBox is for displaying a list of drives available in your computer. When you
place this control into the form and run the program, you will be able to select different drives
from your computer
The DirListBox
The DirListBox means the Directory List Box. It is for displaying a list of directories or
folders in a selected drive. When you place this control into the form and run the program,
you will be able to select different directories from a selected drive in your computer
VB 6 Variable Declarations
The coding part in VB6 starts with a variable declaration. A variable in vb6 is a name that
hold a value. Visual basic 6 is a typed language, similar to C/C++, Java that supports all
kinds of data types.
Syntax:
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
Double Store large floating value which exceeding the single data type value
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
Arithmetical Operators
+ Add 5+5 10
- Substract 10-5 5
/ Divide 25/5 5
* Multiply 5*4 20
Remainder of
Mod 20 Mod 6 2
division
"George"&"
& String concatenation "George Bush"
"&"Bush"
Relational Operators
If <condition> Then
statement
End If
Method 1
Method 2
e.g.: Assume you have to find the grade using nested if and display in a text box
If average > 75 Then
txtGrade.Text = "A"
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S"
Else
txtGrade.Text = "F"
End If
e.g.: Assume you have to find the grade using select...case and display in the text box
average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text ="A"
Case 74 To 65
txtGrade.Text ="B"
Case 64 To 55
txtGrade.Text ="C"
Case 54 To 45
txtGrade.Text ="S"
Case 44 To 0
txtGrade.Text ="F"
Case Else
MsgBox "Invalid average marks"
End Select
Loops (Repetition Structures) In Visual Basic 6
A repetition structure allows the programmer to that an action is to be repeated until given
condition is true.
The Do While...Loop is used to execute statements until a certain condition is met. The
following Do Loop counts from 1 to 100.
A variable number is initialized to 1 and then the Do While Loop starts. First, the condition is
tested; if condition is True, then the statements are executed. When it gets to the Loop it goes
back to the Do and tests condition again. If condition is False on the first pass, the statements
are never executed.
number = 1
While number <=100
number = number + 1
Wend
The Do...Loop While statement first executes the statements and then test the condition after
each execution. The following program block illustrates the structure:
The programs executes the statements between Do and Loop While structure in any case.
Then it determines whether the counter is less than 501. If so, the program again executes the
statements between Do and Loop While else exits the Loop.
Do Until...Loop Statement
An example for Do Until...Loop statement. The coding is typed inside the click event of the
command button
Numbers between 1 to 1000 will be displayed on the form as soon as you click on the
command button.
The For...Next Loop is another way to make loops in Visual Basic. For...Next repetition
structure handles all the details of counter-controlled repetition. The following loop counts
the numbers from 1 to 100:
Dim x As Integer
For x = 1 To 50
Print x
Next
In order to count the numbers from 1 yo 50 in steps of 2, the following loop can be used
For x = 1 To 50 Step 2
Print x
Next
The above coding will display numbers vertically on the form. In order to display numbers
horizontally the following method can be used.
For x = 1 To 50
Print x & Space$ (2);
Next
To increase the space between the numbers increase the value inside the brackets after the &
Space$.
Following example is a For...Next repetition structure which is with the If condition used.
In the output instead of number 4 you will get the "This is number 4".