Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Unit 1

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.
Features of VB
Starting and Exiting Visual Basic Project Explorer
Before you can write programs in VB 6, you need to install Visual Basic 6
compiler on your computer. 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 startup, Visual Basic 6.0 will display the following dialog
box as shown in Figure below.
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). 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.

The Form is the primary building block of a Visual Basic 6 application. A


Visual Basic 6 application can actually comprise many forms. 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.

Create and Save a New Program


1. Start VB.

Find Visual Basic on the Windows Start menu and start it.

The large VB window appears with a New Project dialog box (if there is
no dialog box, bring it up by File > New Project).

Select Standard Exe. The VB window shows Project1 with an


empty Form1.

2. Name the project.

Select Project > Project1 Properties. In the Project Properties dialog box,
change Project1 to your project name, say counter.

The title bar on the VB window changes to match.

3. Name the form and set the form caption.

The Properties window at the right of the VB window shows the


properties for the form. The first property at the top of the list is Name.
Change the name to, say, frmCounter.

The title bar in the Form window changes to match.

Still in the Properties window, scroll down to Caption. Change the


caption to the same name as the project, say counter.

The title bar on the form itself changes to match.

4. Save the form (you should always name and save the form before you
save the project).

Select File > Save frmCounter As.... The Save Form as dialog box
appears. The Save in: textbox shows the name of the folder where the
form will be saved. You should create a new folder for this project.
Navigate to the folder where you want to keep all your VB projects and
click on the new folder icon (a picture of a folder with a little highlight).
After you create the new folder, select that folder. Its name should
appear in the Save in: box. The form file name frmCounter.frm should
appear in the File name: box (if not, type in the correct name). Click
on Save.

The form is saved.

5. Save the project (be sure to name and save the form before you save the
project).

Select File > Save Project As.... The Save Project as dialog box appears.
The Save in: textbox should show the correct folder (the one you just
created for the form). The File name: box should show the correct name
(Counter.vbp in this example). Click on Save.

The project is saved.

Run the program

There are several ways to run your program:

 Press the F5 key.


 On the VB menu bar, Run > Start
 On the VB toolbar, click the VB Run icon (the arrow)

The program window appears, looking much like the form you designed. The
controls on the window are active. While the program is running, it behaves
like any other window on the desktop: you can move it, minimize it, etc.

The indicator on the VB window title bar changes from design mode to run
mode. Many items in the VB window disappear during run mode and many
menu and toolbar operations not enabled.

Stop the program

There are several ways to stop your program:

 In your program's window, click the Exit button or menu (if you provided
one)
 In your program window's title bar, click the Close button (VB always
provides one)
 On the VB menu bar, Run > Stop
 On the VB toolbar, click the VB Stop icon (the box)

The indicator on the VB window title bar changes from run mode to design
mode. Items in the VB window reappear, and menu and toolbar operations are
enabled again.
Forms
Visual Basic Form is the container for all the controls that make up the user
interface. Every window you see in a running visual basic application is a form,
thus the terms form and window describe the same entity.

The main characteristic of a Form is the title bar on which the Form's caption
is displayed. On the left end of the title bar is the Control Menu icon. Clicking
this icon opens the Control Menu. Maximize, Minimize and Close buttons can
be found on the right side of the Form. Clicking on these buttons performs the
associated function.

The following figure illustrates the appearance of a Form

The control menu contains the following commands:

 Restore: Restores a maximized Form to the size it was before it was


maximized; available only if the Form has been maximized.
 Move : Lets the user moves the Form around with the mouse
 Size : Lets the user resizes the control with the mouse
 Minimize: Minimizes the Form
 Maximize : Maximizes the Form
 Close : Closes the Form
Form Properties

S.N Properties Description

1 AcceptButton The button that's automatically activated when


you press Enter, no matter which control has the
focus at the time. Usually the OK button on a form
is set as AcceptButton for a form.

2 CancelButton The button that's automatically activated when


you hit the Esc key.
Usually, the Cancel button on a form is set as
CancelButton for a form.

3 BackColor Sets the form background color.

4 Enabled If True, allows the form to respond to mouse and


keyboard events; if False, disables form.

5 Font This property specify font type, style, size

6 Name This is the actual name of the form.

7 Text The text, which will appear at the title bar of the
form.

Form Events

S.N Event Description

2 Click Occurs when the form is clicked.

3 Closed Occurs before the form is closed.

13 Load Occurs before a form is displayed for the first


time.

15 MouseDown Occurs when the mouse pointer is over the form


and a mouse button is pressed.

16 MouseEnter Occurs when the mouse pointer enters the form.


17 MouseHover Occurs when the mouse pointer rests on the
form.

18 MouseLeave Occurs when the mouse pointer leaves the form.

19 MouseMove Occurs when the mouse pointer is moved over


the form.

20 MouseUp Occurs when the mouse pointer is over the form


and a mouse button is released.

21 MouseWheel Occurs when the mouse wheel moves while the


control has focus.

22 Move Occurs when the form is moved.

23 Resize Occurs when the control is resized.

26 VisibleChanged Occurs when the Visible property value changes.

Loading and Unloading Forms

In order to load and unload the forms, Load and Unload statements are used.
The Load statement has the following syntax:

Load FormName

And the Unload statement has the following syntax:

Unload FormName

The FormName variable is the name of the Form to be loaded or unloaded.


Showing and Hiding Forms
Show method is used to Show a Form. If the Form is loaded but invisible, the
Show method is used to bring the Form on Top every other window. If the
Form is not loaded, the Show method loads it and then displays it.
Formname.show()

The Hide method is used to hide a Form. The following is the syntax of the
Hide Method.

FormName.Hide

To hide a Form from within its own code, the following code can be used.

Me.Hide

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 properties
too. All of these properties are displayed in the Properties Window.

All the controls in the ToolBox except the Pointer are objects in Visual Basic.
These objects have associated properties, methods and events.

Real world objects are loaded with properties. For example, a flower is loaded
certain color, shape and fragrance. Similarly programming objects are loaded
with properties. 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 etc. 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.
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

Displays icons/bitmaps and metafiles. It displays text or


PictureBox
acts as a visual container for other controls.

TextBox Used to display message and enter text.

CheckBox Displays a True/False or Yes/No option.

OptionButton control which is a part of an option group


OptionButton 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.
Contains a TextBox and a ListBox. This allows the user to
ComboBox select an ietm from the dropdown ListBox, or to type in a
selection in the TextBox.

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


Image
metafiles. But less capability than the PictureBox

Events

Programs need to do something in response to user actions and actions


initiated by the operating system. Such actions, which are external to the
program itself (although they may be triggered by the program) are
called events.

Traditional procedural programming languages create programs in which the


execution path is almost entirely dictated by the program's code. A Windows
application, however, is event-driven, in that the user can interact with
different elements within the user interface, in any order they choose. The
events do not occur in a pre-determined order, and the code that is executed
will be the code that is associated with a specific event (referred to in Visual
Basic as an event procedure), regardless of the order in which events occur.

There are different types of events and different ways to trigger them. An event
can be triggered in the following ways:

 User interaction - clicking a button, for example.

 Object event - a timer control can trigger a timer event.

 System event - the operating system can trigger events.

 Code - the program itself may cause an event to occur.


Events are most often triggered by some form of user interaction. Forms and
their controls all have their own unique set of events that are specific to a
particular type of object. A Button control, for example, has a number of
events, including the Click event which is triggered when the user clicks on it,
causing the code within the Click event to be executed.

Event Procedures: Code related to some object. This is the code that is
executed when a certain event occurs.

Unit 2
Loops
Loops are control structures used to repeat a given section of code a certain number of times or until a particular
condition is met.

We can write a Visual Basic procedure that allows the program to run repeatedly until a condition or a
set of conditions is met. This is procedure is known as looping. Looping is a very useful feature of
Visual Basic because it makes repetitive works easier.

Unit 4
Procedures
Visual Basic offers different types of procedures to execute small sections of
coding in applications. Visual Basic programs can be broken into smaller
logical components called Procedures. Procedures are useful for condensing
repeated operations such as the frequently used calculations, text and control
manipulation etc. The benefits of using procedures in programming are:
 It is easier to debug a program a program with procedures, which breaks
a program into discrete logical limits.
 Procedures used in one program can act as building blocks for other
programs with slight modifications.
 A Procedure can be Subroutines, Function or Property Procedure.
Subroutines
A subroutine is a block of statements that carries out a well-defined task. The
block of statements is placed with a pair of sub/end sub statements and can
be invoked by name.
Example:
Sub sayhello()
MsgBox “Hello User”
End Sub
The statements in a subroutine are executed and when the End Sub statement
is reached, control returns to the calling program.
Functions
A function is similar to a subroutine, but a function returns a result.
Subroutines perform a task and don’t report anything to the calling program;
functions commonly carry out calculations and report the result. The
statements that make up a function are placed in a pair of Function/End
Function statements. Moreover, because a function reports a result, it must
have a type, for example:
Function ShowMonth() As String
ShowMonth = Month (date)
End Function

You might also like