Introduction To Visual Basic
Introduction To Visual Basic
VISUAL BASIC
Aim
To equip the learners with appropriate knowledge and skills on developing running programs using the
visual basic.
Learning Outcomes
2. Understand the various data types, control structures and data structures used in object
oriented programming
Methodology
Learning Resources
https://microsoft-visual-basic-2010-express.software.informer.com/download/#downloading
Course Evaluation
Total 100%
WEEK 1
1. Visual programming
WEEK 2
VISUAL ENVIRONMENT
3. Visual objects
WEEK 3
PROGRAM STRUCTURE
2. Data types
3. Operators
4. Variables
WEEK 4
CONTROL STRUCTURES
PROGRAM WRITING
1. Creating an application
WEEK 7
2. Compilation
3. Debugging
WEEK 8
4. Testing
5. Execution
WEEK 9
CONTROL STRUCTURES
WEEK 10 CAT 2
WEEK 11
ERROR HANDLING
1. Types of errors
WEEK 12
SUB-PROGRAMS
1. Meaning of subprograms
2. Types of subprograms
3. Scope of variables
WEEK13
LINKING TO DATABASES
1. Database controls
2. Reports
1. You can develop Windows-based applications and the games with Visual Basic, and you can find
the answers to your programming problems much more easily than other programming languages.
2. It is easy to use and easy to learn, and it is so popular.
3. Visual basic provides a comprehensive interactive and context-sensitive online help system, and the
structure of the basic programming language is very simple, it is not only a language but primarily
an integrated, interactive development environment (IDE).
4. Visual Basic IDE has been highly optimized to support rapid application development (RAD), it is
particularly easy to develop graphical user interfaces and to connect them to handler functions
provided by the application.
5. The graphical user interface of the VB-IDE provides intuitively appealing views for the
management of the program structure in the large and the various types of entities (classes,
modules, procedures, forms,).
6. Visual Basic is a programming language offering general ease of use combined with ease of
implementing a graphical user interface, it is relatively simplistic and therefore limited in function
compared to more advance.
The dialog box offers you five types of projects that you can create. As we are going to learn to
create Windows Applications, we will select Windows Forms Application.
At the bottom of this dialog box, you can change the default project name WindowsApplication1 to
some other name you like, for example, myFirstProgram. After you have renamed the project, click
OK to continue.
THE VB INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)
One of the most significant changes in Visual Basic is 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.
Menu Bar
Tool Bar
Project Explorer
Properties window
Form Layout 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 the latest versions of Visual Basic 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.
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.
Project Explorer
Docked on the right side of the screen, just under the toolbar, is the Project Explorer window. 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.
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.
Form -form
Label-lb.
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
FORM 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:
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
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.
SCOPE OF VARIABLES
The scope of a variable determines the accessible range of a defined variable at the time of
declaration in any block, module, and class for example, when you declare a variable within a
procedure, only that code within that procedure can access or change the variables value. The scope
is limited to the procedure containing the variable. There are three variable scopes in Visual Basic
1. Local variable
2. Module variable
3. Public variable
1. local variable
A local variable is a type of variable defined within a procedure scope, block, or function. It is
available with a code inside the procedure, and it can only be visible to that specific procedure.local
variable be declared using the Dim or static statement. Its syntax is:
These variables are not accessible from outside of the local method. However, the local variable can
be easily accessed by the nested programming function in the same method.
2. Module/Form variable
All existing procedures can easily identify a variable that is declared inside a module sheet is called
a module-level variable. The defined module variable is visible to all procedures within that
module or form only, but it is not available for other module's procedures. The Dim or private
statement at the top of the first procedure declaration can be declared the module-level variables. It
means that these variables cannot be declared inside any procedure block. Further, these variables
are useful to share information between the procedures in the same module. And one more thing
about the module-level variable is that these variables can remains existence as long as the module
is executed.it can be declared using either Dim or Private keywords.
As the name defines, a public variable is a variable that is used to access the variables globally in a
program. It means these variables can be accessed by all the procedures or modules available in a
program. To access the variables globally in a program, you need to use the friend or public
keyword with a variable in a module or class at the top of the first procedure function. Global scope
is also known as the Namespace scope.it is declared using the Public Keyword.Its syntax is
1. Objects
An object in a VB program is a component of the user interface, such as a form (which represents
the window in which the program executes) or a control (a button, list, checkbox, textbox, or some
other component that is contained in the form). Every object has properties which define the
characteristics of the object. One property that every object has is (Name) -- the name of the object.
Every object is given a default name when you create it, but we always change the names, using a
style of naming that helps us remember the type of the object. We start the name with an
abbreviation of the object type:
2. Events
While a program is running, the user can do many things, such as move the mouse, click the mouse
button, enter text, or move to a different window. Each of these actions is called an event. Each
event has two components: what the user did (click, type, etc.) and on which object did they
perform the event (which button did they click on, which textbox did they type into). In Visual
Basic you can write code that will perform actions in response to events. Some examples of events
are:
Event Procedures procedures. Each event procedure contains the statements that execute when a
particular event occurs on a particular object. Event procedure names have the
form objectName_event, where objectName is the name of the object on which the event occurred
and event is the type of the event. For example, an event procedure named cmdClear_Click will be
executed when the user clicks on the button named cmdClear.
The event procedures are written in the code window. To open the code window, double click on
the control for which you want to write the event procedure. The code window will open and it will
contain the shell of the event procedure for that object:
End Sub
Methods
A method causes an object to perform an action or task. Methods are actions that you want to
perform. For example, Add is a method of the Combo Box object, because it adds a new entry to a
combo box.
Its Syntax is:
Object. Method (arg1, arg 2,)
Example
Sub Add Entry (new Entry as String)
Combo.Add new Entry
End Sub
NB: The above procedure uses the Add method to add a new item to a Combo Box.