How Visual Basic Works
Visual Basic is essentially a Windows programming language.
• Windows: Any rectangular box that displays information or runs an application is called a
window. The browser that you’re reading this in? That’s a window. The folder that pops up
when you fire up Windows Explorer? That’s a window as well. So is the dialog box reminding
you to change your firewall settings, rename a folder, or download a file. Basically, any user-
facing GUI element can be called a window.
• Events: Any sort of input or action within Windows is called an ‘event’. Clicking an icon,
pressing a key, moving a window around – these are all examples of events. An event basically
creates a message telling Windows to perform some action.
• Messages: Every time an event occurs, Windows creates a message which is then directed to
the appropriate application to perform an action. Think of messages as the mediators between
events and applications.
The Integrated Development Environment (IDE)
IDE is a term commonly used in the programming world to describe the interface and environment
that we use to create our applications. 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. Tha Visual Basic IDE is made up of a number
of components
• Menu Bar
• Tool Bar
• Project Explorer
• Properties Windows
• Form Layout Window
• Toolbox
• Form Designer
• Object Browser
On start up, Visual Basic 6.0 will display the following dialog box as shown in Figure below.
1
We 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). Next, click on the Standard EXE icon to go into the actual Visual
Basic 6 programming environment.
When we start a new Visual Basic 6 Standard EXE project, we will be presented with the Visual
Basic 6 Integrated Development Environment (IDE). The Visual Basic 6 Integrated Programming
Environment is shown in Figure below. It consists of the toolbox, the form, the project explorer and
the properties window.
Menu Bar
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 shown below.
2
Control Description
Pointer Provides a way to move and resize the controls form
Displays icons/bitmaps and metafiles. It displays text or acts as
PictureBox
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 control which is a part of an option group allows
OptionButton 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.
Contains a TextBox and a ListBox. This allows the user to
ComboBox select an ietm from the dropdown ListBox, or to type in a se-
lection in the TextBox.
HScrollBar and VScroll- These controls allow the user to select a value within the spec-
Bar ified range of values
Timer Executes the timer events at specified intervals of time
Displays the valid disk drives and allows the user to select one
DriveListBox
of them.
Allows the user to select the directories and paths, which are
DirListBox
displayed.
Displays a set of files from which a user can select the desired
FileListBox
one.
Shape Used to add shape (rectangle, square or circle) to a Form
Line Used to draw straight line to the Form
used to display images such as icons, bitmaps and metafiles.
Image
But less capability than the PictureBox
Enables the use to connect to an existing database and display
Data
information from it.
Used to link or embed an object, display and manipulate data
OLE
from other windows based applications.
Label Displays a text that the user cannot modify or interact with.
Project Explorer
The Project Explorer as shown in in figure serves 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.
3
Properties Window
The Properties Window is docked under the Project Explorer window. The Properties Window ex-
poses the various characteristics of selected objects. Each and every form in an application is con-
sidered 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 char-
acteristics 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. The
Properties window lets you change the characteristics, or property settings, of the form itself and of
visual interface elements on the form. The Properties window consists of two columns: the first is the
name of the property, which you cannot change; the second column, which is the property setting that
you can change.
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.
4
Properties, Methods And Events
All the controls in the ToolBox except the Pointer are objects in Visual Basic. These objects have
associated properties, methods and events. A property is a named attribute of a programming object.
Properties define the characteristics of an object such as Size, Color etc. or sometimes the way in
which it behaves. For example, a TextBox accepts properties such as Enabled, Font, MultiLine, Text,
Visible, Width, etc. Some of the properties are listed below
• Enables property allows the TextBox to be enabled or disabled at run time depending on the
condition set to True or False.
• Font property sets a particular font in the TextBox.
• MultiLine property allows the TextBox to accept and display multiple lines at run time.
• Text property of the TextBox control sets a particular text in the control.
• Visible property is used to hide the object at run time.
• Width property sets the TextBox to the desired width at design time.
The properties that are discussed above are design-time properties that can be set at the design tme by
selecting the Properties Window. But certain properties cannot be set at desgn time. For example, the
CurrentX and CurrentY properties of a Form cannot be set at the design time.
A method is an action that can be performed on objects. For example, a cat is an object. Its
properties might include long white hair, blue eyes, 3 pounds weight etc. A complete definition of
cat must only encompass on its looks, but should also include a complete itemization of its activities.
Therefore, a cat’s methods might be move, jump, play, breath etc.
Similarly in object-oriented programming, a method is a connected or built-in procedure, a block
of code that can be invoked to impart some action on a particular object. A method requires an object
to provide them with a context. For example, the word Move has no meaning in Visual Basic, but the
statement,
Text1.Move 700, 400
performs a very precise action. The TextBox control has other associated methods such as Refresh,
SetFocus, etc.
• The Refresh method enforces a complete repaint of the control or a Form. For example,
Text1.Refresh refreshes the TextBox.
• The Setfocus method moves the focus on the control. For Example Text1.SetFocus sets the
focus to TextBox control Text1.
Event Driven Programming
When you create a program in Visual Basic, you’ll generally be doing event-driven programming.
Event-driven programming means that most of the code you write will be run as users do things
within your program or even when certain things happen in Windows–when events occur. Of course,
programming this way means that you have to know when events occur and have to write code that
will make your program do something in response to the event. Whenever an event takes place,
Windows sends out a message to your program. Your program reads this message, and then runs the
code you’ve attached to the event. If you don’t specify code for an event, your program will simply
ignore the event. Generally, this code is known as a procedure, defined as any block of code that can
be called from within your application. This code might be used to move objects around on a form,
calculate a value from a formula, or write data to a database. No matter the purpose, a procedure
always uses this format:
5
[ P u b l i c | P r i v a t e ] [ S t a t i c ] Sub | F u n c t i o n | P r o p e r t y
f u n c t i o n n a m e ( a r g u m e n t s ) [ As Type ]
{ . . . Your p r o c e d u r e c o d e . . . }
End Sub | F u n c t i o n | P r o p e r t y
An event procedure is the place in your project where you put the code that will run when an event
occurs. To write an event procedure, you must access the Code window for your object by doing one
of the following:
• Double-clicking the object
• Selecting the object with the mouse and pressing F7.
• Selecting the object and choosing Code from the View menu.
• Selecting the object’s form in the Project Explorer, clicking the View Code button, and choosing
the object from the Code window
Visual Basic automatically generates an event procedure when you select an event in the Code Win-
dow Let us consider a TextBox control and a few of its associated events to understand the concept
of event driven programming. The TextBox control supports various events such as Change, Click,
MouseMove and many more that will be listed in the Properties dropdown list in the code window for
the TextBox control. We will look into a few of them as given below.
• The code entered in the Change event fires when there is a change in the contents of the
TextBox.
• The Click event fires when the TextBox control is clicked.
• The MouseMove event fires when the mouse is moved over the TextBox
Common Events of Visual Basic Controls
Event Occurrence
Change The user modifies text in a combo box or text box.
Click The user clicks the primary mouse button on an object.
DblClick The user double-clicks the primary mouse button on an object.
DragDrop The user drags an object to another location.
DragOver The user drags an object over another control.
GotFocus An object receives focus.
KeyDown The user presses a keyboard key while an object has focus.
The user presses and releases a keyboard key while an object
KeyPress
has focus.
KeyUp The user releases a keyboard key while an object has focus.
LostFocus An object loses focus.
The user presses any mouse button while the mouse pointer is
MouseDown
over an object.
Displays the valid disk drives and allows the user to select one
DriveListBox
of them.
MouseMove The user moves the mouse pointer over an object.
The user releases any mouse button while the mouse pointer is
MouseUp
over an object.
6
The Relationship Between Properties, Methods, and Events
Although properties, methods, and events do different things, it’s important to realize that they’re of-
ten interrelated. For example, if you move a control with the Move method (most likely in response to
an event), one or more of the control’s position properties (Top, Height, Left, and Width) will change
as a result. Because the control’s size has changed, the Resize event occurs. This interdependence
means that you can sometimes accomplish the same task multiple ways in your code by manipulating
object properties or methods.
Steps in Building a Visual Basic Application
1. Design the interface by adding controls to the form and set their properties.
2. Write code for the event procedures.
Simple application Changing Background and Foreground Color at Random
In this example, it show how to write code to change the background and the foreground color ran-
domly. 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 write 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.
Now, rename the controls as follows:
• Form1-MyForm
• Label1-LblMessage
• Command1-cmd bgColor
• 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
P r i v a t e Sub c m d b g C o l o r C l i c k ( )
Dim r , g , b As I n t e g e r
r = I n t ( Rnd ( ) ∗ 2 5 6 )
g = I n t ( Rnd ( ) ∗ 2 5 6 )
b = I n t ( Rnd ( ) ∗ 2 5 6 )
MyForm . B a c k C o l o r = RGB( r , g , b )
End Sub
P r i v a t e Sub C m d f g C o l o r C l i c k ( )
Dim r , g , b As I n t e g e r
7
r = I n t ( Rnd ( ) ∗ 2 5 6 )
g = I n t ( Rnd ( ) ∗ 2 5 6 )
b = I n t ( Rnd ( ) ∗ 2 5 6 )
Lbl Msg . F o r e C o l o r = 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.