CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Introduction to Visual Basic
1.1 The concept of computer programming
Programming involves the creation of a series of instructions aimed at directing a computer to perform tasks
far more efficiently than humans. The initial programming language, known as machine language, relied on
binary code (consisting of 0s and 1s) to establish communication with the computer. Nonetheless, machine
language posed significant challenges due to its complexity. Thankfully, scientists have since developed high-
level programming languages that are considerably more user-friendly and accessible for mastering. Among
the high-level programming languages are Java, Javascript, C, C++, c# and Visual Basic.
1.2 What is Visual Basic?
Visual Basic is a third-generation event-driven programming language first released by Microsoft in 1991. It
evolved from the earlier DOS version called BASIC. BASIC means Beginners' All-
purpose Symbolic Instruction Code. Since then Microsoft has released many versions of Visual Basic, from
Visual Basic 1.0 to the final version Visual Basic 6.0. Visual Basic is a user-friendly programming language
designed for beginners, and it enables anyone to develop GUI window applications easily.
In 2002, Microsoft released Visual Basic.NET(VB.NET) to replace Visual Basic 6. Thereafter, Microsoft
declared VB6 a legacy programming language in 2008. Fortunately, Microsoft still provides some form of
support for VB6. VB.NET is a fully object-oriented programming language implemented in the .NET
Framework. It was created to cater for the development of the web as well as mobile applications. However,
many developers still favor Visual Basic 6.0 over its successor Visual Basic.NET.
1.3 What kind of programs can you develop using Visual Basic 6?
In VB 6, the possibilities for program creation are virtually limitless. Whether you're a math teacher
interested in coding programs like geometric progression, quadratic equation solver, simultaneous equation
solver, prime number tester, factors finder, or a quadratic function graph Plotter—the scope is expansive.
For science educators, the potential extends to simulation programs covering projectile, simple harmonic
motion, star war, and beyond. Businesses can leverage VB 6 to develop applications like inventory
management systems, Amortization Calculators, investments calculators, point-of-sale systems, payroll
systems, accounting programs, and more, streamlining operations and boosting productivity. Gaming
enthusiasts can delve into the world of VB 6 to create engaging programs such as slot machines, reversi, tic-
tac-toe, and others. Moreover, the realm of multimedia is at your fingertips with the ability to craft Smart
Audio Players, Multimedia Players, and a variety of other applications
1.4 The Visual Basic 6 Integrated Development Environment
Before you can write programs in VB 6, you need to install Visual Basic 6 compiler on your computer. You can
purchase a copy of Microsoft Visual Basic 6.0 Learning Edition or Microsoft Visual Basic Professional
Edition from Amazon.com, both are vb6 compilers. Besides, you can also buy it from eBay at Microsoft
Visual Basic 6.0 6 Professional PRO MSDN Library Manual Service Pack. If you have already installed
Microsoft Office in your PC or laptop, you can also use the built-in Visual Basic Application in Excel to start
creating Visual Basic programs without having to spend extra cash to buy the VB6 compiler.
You can also install VB6 on Windows 10 but you need to follow certain steps otherwise the installation will
fail. First, you need to run setup as administrator. Next, you need to use custom installation. Clear the
checkbox for Data Access. If you don't, set up will hang at the end of the installation. Finally, click next and
wait for the installation to complete. For complete instructions, please follow this link Install VB6 on
Windows 10
Copyright @ Eugene Tebo November 2024
1
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
After installing the vb6 compiler, the icon will appear on your desktop or in your programs menu. Click on
the icon to launch the VB6 compiler. On start up, Visual Basic 6.0 will display the following dialog box as
shown in Figure 1.1.
Figure 1.1: New Project Dialog
You can choose to either start a new project, open an existing project or select a list of recently opened
programs. A project is a collection of files that make up your application. There are various types of
applications that we could create, however, we shall concentrate on creating Standard EXE programs (EXE
means executable). Before you begin, you must think of an application that preferably have commercial
,educational or recreational value. Next, click on the Standard EXE icon to go into the actual Visual Basic 6
programming environment.
When you start a new Visual Basic 6 Standard EXE project, you will be presented with the Visual Basic
6 Integrated Development Environment (IDE). The Visual Basic 6 Integrated Programming Environment is
shown in Figure 1.2. It consists of the toolbox, the form, the project explorer and the properties window.
Figure 1.2: VB6 Programming Environment
Copyright @ Eugene Tebo November 2024
2
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
The Form is the primary building block of a Visual Basic 6 application. A Visual Basic 6 application can actually
comprise many forms, but we shall focus on developing an application with one form first. We will learn how
to develop applications with multiple forms later. Before you proceed to build the application, it is a good
practice to save the project first. You can save the project by selecting Save Project from the File menu,
assign a name to your project and save it in a certain folder. You shall now proceed to learn Visual Basic
programming from the next lesson onwards.
Building VB Applications
2.1 Creating Your First Application
First of all, launch Microsoft Visual Basic 6 compiler. In the New Project Dialog , choose Standard EXE to enter
Visual Basic 6 integrated development environment. In the VB6 IDE, a default form with the name Form1 will
appear. Next, double click on Form1 to bring up the source code window for Form1, as shown in Figure 2.1.
The top of the source code window consists of a list of objects and their associated events or procedures. In
the source code window, the object displayed is Form1 and the associated procedure is Load.
Figure 2.1 The VB6 Source Code Window
When you click on the object box, the drop-down list will display a list of objects you have inserted into your
form, as shown in figure 2.2. Here, you can see a form with the name Form1, a command button with the
name Command1, a Label with the name Label1 and a Picture Box with the name Picture1.
Copyright @ Eugene Tebo November 2024
3
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 2.2: List of Objects
Similarly, when you click on the procedure box, a list of procedures associated with the object will be displayed , as
shown in Figure 2.3. Some of the procedures associated with the object Form1 are Activate, Click, DblClick (which
means Double-Click) , DragDrop, keyPress and more. Each object has its own set of procedures. You can always select
an object and Writing the Codes for any of its procedure in order to perform certain tasks.
Figure 2.3 List of Procedures
You do not have to worry about the beginning and the end statements (i.e. Private Sub Form_Load.......End
Sub.); Just key in the lines in between the above two statements exactly as are shown here. When you
press F5 to run the program, you will be surprised that nothing showed up .In order to display the output of
the program, you have to add the Form1.show statement like in Example 2.1.1 or you can just
use Form_Activate ( ) event procedure as shown in example 2.1.2. The command Print does not mean
printing using a printer but it means displaying the output on the computer screen. Now, press F5 or click on
the run button to run the program and you will get the output as shown in Figure 2.4.
You can also perform arithmetic calculations as shown in Example 2.1.2. VB uses * to denote the
multiplication operator and / to denote the division operator. The output is shown in Figure 2.5, where the
results are arranged vertically.
Example 2.1.1
Private Sub Form_Load ( )
Form1.show
Print "Welcome to Visual Basic tutorial"
End Sub
Copyright @ Eugene Tebo November 2024
4
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Example 2.1.2
Private Sub Form_Activate ( )
Print 20 + 10
Print 20 - 10
Print 20 * 10
Print 20 / 10
End Sub
Figure 2.4 : The output of Example 2.1.1
Figure 2.5 : The output of Example 2.1.2
You can also use the + or the & operator to join two or more texts (string) together like in example 2.1.4 (a)
and (b)
Example 2.1.4(a)
Private Sub Form_Load ( )
A = "Tom"
B = "likes"
C = "to"
D = "eat"
E = "burger"
Print A + B + C + D + E
End Sub
Copyright @ Eugene Tebo November 2024
5
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Example 2.1.4(b)
Private SubForm_Load ( )
A = "Tom"
B = "likes"
C = "to"
D = "eat"
E = "burger"
Print A & B & C & D & E
End Sub
The Output of Example 2.1.4(a) &(b) is as shown in Figure 2.6.
Figure 2.6
2.2 Steps in Building a Visual Basic Application
Step 1: Design the interface by adding controls to the form and set their properties
Step 2:Writing the Code for the event procedures
Example 2.2 Changing Background and Foreground Color at Random
In this example, we want to show you how to Writing the Code to change the background and the
foreground color randomly. We will place two command buttons and a label on the form. One of the
command buttons will be used to change the background color while the other one will be used to change
the foreground color. The Label is for displaying the foreground color. There are two events here, change
background color and change foreground color. Therefore, we need to Writing the Code for the two event
procedures.
To make the program more interesting, we will use the Rnd() function, the Int() function and the RGB codes
to change the color randomly. The Rnd() function creates a random number between 0 and 1 and the RGB
code uses a combination of three integers to form a certain color. The Int() is a function that converts a
number into an integer by truncating its decimal part and the resulting integer is the largest integer that is
smaller than the number. For example, Int(0.2)=0, Int(2.4)=2, Int(4.8)=4. Therefore, Int(Rnd()*256) returns
the smallest integer 0 and the biggest integer 255. The format of RGB code is RGB(a,b,c), where a, b, c range
from 0 to 255. For example, RGB(255,0,0) is red, RGB(255,255,255) is white and (0,0,0) is black. Do not worry
about the jargons, you will learn them in later lesson.
Now, rename the controls as follows:
• Form1-MyForm
• Label1-LblMessage
• Command1-cmd_bgColor
Copyright @ Eugene Tebo November 2024
6
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
• Command2-cmd_fgColor
Next, change the caption of the Label to "Please Change My Color". In addition, change the caption of
Command1 button to "Change Background Color" and change the caption of Command2 button to "Change
Foreground Color"
Now, enter the following code
Private Sub cmd_bgColor_Click()
Dim r, g, b As Integer
r = Int(Rnd() * 256)
g = Int(Rnd() * 256)
b = Int(Rnd() * 256)
MyForm.BackColor = RGB(r, g, b)
End Sub
Private Sub Cmd_fgColor_Click()
Dim r, g, b As Integer
r = Int(Rnd() * 256)
g = Int(Rnd() * 256)
b = Int(Rnd() * 256)
Lbl_Msg.ForeColor = RGB(r, g, b)
End Sub
When you run the program, each time you press on the 'Change Background Color' button, you will see
different background color. Similarly, each time you press on the 'Change Foreground Color', you will see the
message on the Label changes color. The output is shown in Figure 2.7.
Figure 2.7
Working With Controls
3.1 The Control Properties
To effectively write an event procedure for a control to respond to an event, it is necessary to configure
specific properties that dictate its appearance and functionality. These properties can be set either through
the properties window or dynamically during runtime.
In Figure 3.1, you'll find a standard properties window for a form. At the top, you'll see the currently selected
object. The bottom part is divided into columns—the left lists various properties linked to the selected
object, and the right displays their current states. To modify these properties, simply highlight and adjust the
items in the right column by typing or selecting from the available options..
Copyright @ Eugene Tebo November 2024
7
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 3. Properties Window
To modify the caption, simply highlight 'Form1' in the Caption section and replace it with your desired text.
Additionally, you can customize the form's appearance by selecting either a 3D or flat style, adjusting
foreground and background colors, changing the font type and size, toggling buttons for enable/disable, and
managing minimize and maximize options, among other possibilities.
You may also change the properties at runtime to produce special effects such as change of color, shape,
animation and more. Example 3.1 shows the code that will change the form color to red whenever the form
is loaded. VB uses the hexadecimal system to specify the color. You can check the color codes in the
properties windows corresponding to ForeColor and BackColor .
Example 3.1: Program to change background color
This example changes the background colour of the form using the BackColor property.
Private Sub Form_Load()
Form1.Show
Form1.BackColor = &H000000FF&
End Sub
Example 3.2: Program to change shape
This example is to change the control's Shape using the Shape property. This code will change the shape to a
circle at runtime.
Copyright @ Eugene Tebo November 2024
8
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Private Sub Form_Load()
Shape1.Shape = 3
End Sub
We won't describe the details on how to set the properties just yet. However, please follow a few key points
while configuring the properties.
• You should set the Caption property of a control clearly so that users understand the function of the control.
• Use a meaningful name for the Name property because it is easier to write and read the event procedure and
easier for debugging.
• One more important property is to enable or disable a control.
• Finally, you must also considering making the control visible or invisible at runtime, or when should it become
visible or invisible.
3.2 Handling Common Controls
Figure 3.2 is the toolbox that shows the basic controls.
Figure 3.2: Toolbox
3.2.1 The TextBox
The text box is the standard control for accepting an input from the user and 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 using the function Val(text). The following example illustrates a simple program
that processes the input from the user.
Example 3.1: Summation of two numbers
In this program, two text boxes and a few labels and a command button were inserted onto the form
together with . The two text boxes are to accept inputs from the user, one of the labels will display the sum
of two numbers that are entered into the two text boxes. The command button is programmed to calculate
the sum of the two numbers using the plus operator. The program uses a variable sum to accept the
summation of values from text box 1 and text box 2.The procedure to compute the summation of two
numbers is as follows.
Private Sub Command1_Click()
'To add the values in TextBox1 and TextBox2
Sum = Val(Text1.Text) +Val(Text2.Text)
'To display the answer on label 1
Label1.Caption = Sum
End Sub
The output is shown in Figure 3.3
Copyright @ Eugene Tebo November 2024
9
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 3.3
3.2.2 The Label
The label is for providing instructions and guides to the users, as well as for displaying 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. Please refer to Example 3.1 and
Figure 3.1 for the usage of the label.
3.2.3 The Command Button
The command button is for executing a procedure triggered by an event initiated by the users. The most
common event associated with the command button is the Click event, and the syntax for the procedure is
Private Sub Command1_Click ()
Statements
End Sub
Example 3.2 A Simple Password Cracker
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.
Private Sub cmd_ShowPass_Click()
Dim yourpassword As String
yourpassword = Txt_Password.Text
MsgBox ("Your password is: " & yourpassword)
End Sub
Run the program and enter a password, then click on the Show Password button to reveal the password, as
shown in Figure 3.4.
Copyright @ Eugene Tebo November 2024
10
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 3.4 The Password Cracker
You can also reveal the password by setting the PasswordChr property back to normal mode, as follows:
Private Sub cmd_ShowPass_Click()
Dim yourpassword As String
Txt_Password.PasswordChar = ""
End Sub
*Check out our more advanced password cracker program
3.2.4 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.
Picture1.Picture=LoadPicture ("C:\VBprogram\Images\grape.gif")
Example 3.4 Loading Picture
In this program, insert a command button and a picture box. Enter the following code:
Private Sub cmd_LoadPic_Click()
MyPicture.Picture = LoadPicture("C:\Users\admin.DESKTOP-
G1G4HEK\Documents\My Websites\vbtutor\vb6\images\uranus.jpg")
End Sub
* You must ensure the path to access the picture is correct. Besides that, the image in the picture box is not
resizable. The output is shown in Figure 3.5
Figure 3.5 The Picture Viewer
3.2.5 The Image Control
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.
Image1.Picture=LoadPicture ("C:\VBprogram\Images\grape.gif")
Example 3.5 Loading Image
In this program, we insert a command button and an image control into the form. Besides that, we set the
image Strech property to true. Next, enter te following code:
Private Sub cmd_LoadImg_Click()
MyImage.Picture = LoadPicture("C:\Users\admin.DESKTOP-
G1G4HEK\Documents\My Websites\vbtutor\vb6\images\uranus.jpg")
Copyright @ Eugene Tebo November 2024
11
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
End Sub
Figure 3.6 The Image Viewer
* Note the the difference between the image in Figure 3.5 and Figure 3.6.
3.2.6 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
Example 3.2
Private Sub Form_Load ( )
List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”
End Sub
The Output
Figure 3.7 The ListBox
The items in the list box can be identified by the ListIndex property, the value of the ListIndex for the first
item is 0, the second item has a ListIndex 1, and the third item has a ListIndex 2 and more.
3.2.7 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 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
Copyright @ Eugene Tebo November 2024
12
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
method. For example, if you wish to add a number of items to Combo box 1, you can key in the following
statements
Example 3.3
Private Sub Form_Load ( )
Combo1.AddItem "Item1"
Combo1.AddItem "Item2"
Combo1.AddItem "Item3"
Combo1.AddItem "Item4"
End Sub
The Output
Figure 3.8 The ListBox
3.2.8 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.
Example 3.4
Private Sub Cmd_OK_Click()
If Check1.Value = 1 And Check2.Value = 0 And Check3.Value = 0 Then
MsgBox "Apple is selected"
ElseIf Check2.Value = 1 And Check1.Value = 0 And Check3.Value = 0 Then
MsgBox "Orange is selected"
ElseIf Check3.Value = 1 And Check1.Value = 0 And Check2.Value = 0
Then
MsgBox "Orange is selected"
ElseIf Check2.Value = 1 And Check1.Value = 1 And Check3.Value = 0 Then
MsgBox "Apple and Orange are selected"
ElseIf Check3.Value = 1 And Check1.Value = 1 And Check2.Value = 0 Then
MsgBox "Apple and Pear are selected"
ElseIf Check2.Value = 1 And Check3.Value = 1 And Check1.Value = 0 Then
MsgBox "Orange and Pear are selected"
Else
MsgBox "All are selected"
End If
End Sub
The Output
Copyright @ Eugene Tebo November 2024
13
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 3.9 The ListBox
3.2.9 The OptionButton
The OptionButton control also lets the user selects one of the choices. However, two or more Option buttons
must work together because when 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”.
Example 3.4
In this example, we want to change the background color of the form according to the selected option. We
insert three option buttons and change their captions to "Red Background","Blue Background" and "Green
Background" respectively. Next, insert a command button and change its name to cmd_SetColor and its
caption to "Set Background Color". Now, click on the command button and enter the following code in the
code window:
Private Sub cmd_SetColor_Click()
If Option1.Value = True Then
Form1.BackColor = vbRed
ElseIf Option2.Value = True Then
Form1.BackColor = vbBlue
Else
Form1.BackColor = vbGreen
End If
End Sub
Run the program, select an option and click the "Set Background Color" produces the output, as shown in
Figure 3.10.
Figure 3.10
Copyright @ Eugene Tebo November 2024
14
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
3.2.10 The Shape Control
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
corresponding to a rectangle, a square, an oval shape, a circle, a rounded rectangle and a rounded square respectively.
Example 3.5
In this example, we inserted six option buttons. Turning the option buttons into a control array is a more
effective approach, especially since they execute similar actions., i.e to change shape. In order to create a
control array, click on the first option button, rename it as MyOption. Next, click on the option button and
select copy then paste. After clicking the paste button, a popup dialog (Figure 3.11)will ask you whether you
wish to create a control array, select yes. The control array can be accessed via its index value,
MyOtion(Index. In addition, we also insert a shape control.
Figure 3.11
Now, enter the code in the code window. We use the If..Then..Else program structure to determine which
option button is selected by the user. You can learn about If..Then..Else in Lesson 7.
Private Sub MyOption_Click(Index As Integer)
If Index = 0 Then
MyShape.Shape = 0
ElseIf Index = 1 Then
MyShape.Shape = 1
ElseIf Index = 2 Then
MyShape.Shape = 2
ElseIf Index = 3 Then
MyShape.Shape = 3
ElseIf Index = 4 Then
MyShape.Shape = 4
ElseIf Index = 5 Then
MyShape.Shape = 5
End If
End Sub
Run the program and you can change the shape of the shape control by clicking one of the option buttons.
The output is shown in Figure 3.12.
Copyright @ Eugene Tebo November 2024
15
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 3.12
3.2.10 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 as shown in
Figure 3.13
Figure 3.13 The Drive List Box
3.2.11 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 as shown in Figure 3.14
Copyright @ Eugene Tebo November 2024
16
CWD232 PROGRAMMING AND PROJECT LECTURE NOTES 02
Figure 3.14 The DirListBox
FIG 3.15 The FileListBox
You can coordinate the Drive List Box, the Directory List Box and the File List Box to search for the files you
want. The procedure will be discussed in later lessons.
Copyright @ Eugene Tebo November 2024
17