You are on page 1of 7

VISUAL BASIC BASICS

VISUAL BASIC:

VISUAL BASIC is a programming environment from Microsoft in which a programmer uses a graphical user
interface (GUI) to choose and modify pre-selected sections of code written in BASIC programming Language.

IDE:

 IDE-Integrated Development Environment.


 Like any other Windows application, VB consists of multiple windows which appear at startup.
 The windows that are displayed when we start VB are collectively known as the Visual Basic
 An integrated development environment (IDE) is a software suite that consolidates the basic tools
developers need to write and test software.
 IDE contains a code editor, a compiler or interpreter and a debugger that the developer accesses
through a single graphical user interface (GUI).
 An IDE may be a standalone application, or it may be included as part of one or more existing and
compatible applications.
SDI:-
1. Single document Interface
2. 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

MDI:-

1. Multiple document Interface


2. Visual Basic 6.0, the IDE is in a Multiple Document Interface (MDI) format.
3. In this format, the windows associated with the project will stay within a single container known as
the parent.
4. Code and form-based windows will stay within the main container form.

COMPONENTS OF VISUAL BASIC IDE:


1. Menu Bar
2. Tool Bar
3. Project Explorer window
4. Properties Window
5. Form Layout Window
6. Immediate window.
7. Form
8. Code Window

ADVANTAGES OF VISUAL BASIC:

1. Visual Basic applications are Event driven.


2. Visual Basic supports the principles of Object-Oriented design.
3. It's a complete Windows application development system.
4. We can create ActiveX controls,DLLs and add-ins with Visual Basic

DATA TYPES IN VISUAL BASIC:

1. Byte
2. Integer
3. Long
4. Single
5. Double
6. Currency
7. Boolean
8. Date
9. Object
10. String
11. Variant

VARIABLE:
1. Variables are areas allocated by the computer memory to hold data.
2. The content of a variable changes now and then in a program.

Eg .simpleinterest, basic_pay, si12

Components of Visual Basic IDE

The Visual Basic IDE is made up of a number of components


1. Menu Bar
2. Tool Bar
3. Project Explorer
4. Properties window
5. Form Layout Window
6. Toolbox
7. Form Designer
8. Object Browser

Menu Bar:
1. This Menu Bar displays the commands that are required to build an application.
2. The main menu items have sub menu items that can be chosen when needed.
3. The toolbar 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.

Tool Bar:
1. The Toolbox contains a set of controls that are used to place on a Form at design time thereby
creating the user interface area.
2. Additional controls can be included in the toolbox by using the Components menu item on the
Project menu.

CONTROL DESCRIPTION
POINTER Provides a way to move and resize the
controls form
PICTURE BOX Displays icons/bitmaps and meta files. It
displays text or acts as a visual container
for other controls.
TEXTBOX Used to display message and enter text.
FRAME Serves as a visual and functional container
for controls
COMMAND BUTTON Used to carry out the specified action when
the user chooses it.
CHECKBOX Displays a True/False or Yes/No option.
OPTION BUTTION Option Button control which is a part of an
option group allows the user to select only
one option even it displays multiple choices.
LIST BOX Displays a list of items from which a user
can select one.
COMBO BOX Contains a Text-box and a List Box. This
allows the user to select an item from the
drop-down List Box, or to type in a
selection in the Text-box.
HSCROLLBAR AND VSCROLLBAR These controls allow the user to select a
value within the specified range of values
TIMER Executes the timer events at specified
intervals of time
DRIVELISTBOX Displays the valid disk drives and allows the
user to select one of them.
DIRLISTBOX Allows the user to select the directories and
paths, which are displayed.
FILELISTBOX Displays a set of files from which a user can
select the desired one.
SHAPE Used to add shape (rectangle, square or
circle) to a Form
LINE Used to draw straight line to the Form
IMAGE used to display images such as icons,
bitmaps and meta files. But less capability
than the Picture Box
DATA Enables the user to connect to an existing
database and display information from it.
OLE Used to link or embed an object, display and
manipulate data from other windows based
applications.
LABEL Displays a text that the user cannot modify or interact
with.

Project Explorer:-

1. Docked on the right side of the screen, just under the toolbar, is the Project Explorer window.
2. The Project Explorer as shown in in figure servers as a quick reference to the various elements of a
project namely form, classes and modules.
3. All of the object that makes up the application is packed in a project.
4. A simple project will typically contain one form, which is a window that is designed as part of a
program's interface.
5. It is possible to develop any number of forms for use in a program, although a program may consist
of a single form.
6. In addition to forms, the Project Explorer window also lists code modules and classes.

Properties Window:-
1. The Properties Window is docked under the Project Explorer window.
2. The Properties Window exposes the various characteristics of selected objects.
3. Each and every form in an application is considered an object.
4. Now, each object in Visual Basic has characteristics such as color and size.
5. Other characteristics not only affect the appearance of the object but the way it behaves too.
6. All these characteristics of an object are called its properties.
7. Thus, a form has properties and any controls placed on it will have properties too.
8. All of these properties are displayed in the Properties Window.

Object Browser:-
1. The Object Browser allows us to browse through the various properties, events and methods that are
made available to us.
2. It is accessed by selecting Object Browser from the View menu or pressing the key F2.
3. 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.
4. It is possible for us to scroll through the list and select the object or class that we wish to inspect.
5. After an object is picked up from the Classes list, we can see its members (properties, methods and
events) in the right column.
6. A property is represented by a small icon that has a hand holding a piece of paper.
7. Methods are denoted by little green blocks, while events are denoted by yellow lightning bolt icon.

Object naming conversions of controls (prefix)


 Form -frm
 Label-lbl
 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

VARIABLES IN VISUAL BASIC 2005


Definition:

A variable has a name and a value and stores values during program execution.

Declaring Variables :

1. To declare a variable use the Dim statement followed by the variables name, As
keyword, and its type.
2. In a program, variables must be declared in advance.
3. If the compiler is informed of all the variables and their types, it can produce the
most compact and efficient code.
4. When we declare a variable we are actually telling the compiler the type of data we
intend to store in each variable.

Variable initialization :

We can also initialize variables in the same line that declares them.

Example:
The following line declares an integer variable and initialize it to 35.
Dim minMarks As Integer 35
This statement is equal to the following two statements.
Dim minMarks As Integer
minMarks = 35

 Variables are initialized when they are declared, according to their type.
 Numeric variables are initialized to zero, string variables are initialized to a blank
string, and Object variables are initialized to nothing.
 If the variable is declared with an initializer (as in Dim i As Integer 50) it is initialized to
the specified value.

Types of variables:
Visual Basic recognizes the following five categories of variables.
1. Numeric
2. String
3. Boolean
4. Date
5. Object

 The two major variable categories are numeric and string.


 Numeric variables store numbers, string variables stores text and Object variables
store any type of data.

Numeric variables:
All programming languages provide a variety of numeric data types, including the
following:

 Integers
 Decimals
 Single
 Double

1. Decimal, Single and Double are the three basic data types for storing floating point
numbers.
2. The Short data type is the same as the integer data type .
3. The new Integer data type is the same as the long data type.
4. Long data type is new and can represent extremely large integer values.

Naming of variables:

 Variables in VB 2005 are case insensitive.


 For example the variable names strName, strname, STRNAME all refer to the same
variable in the code.

Rules to be followed while naming a variable:

1. Must begin with a letter


2. Cannot have a period
3. Can have up to 255 characters.
4. Must be unique inside of the procedure or the module it is used in ,keywords are not
allowed.
Scope of the Variable:
 Scope is the section of the software program in which the variable is visible and can
be manipulated.
 Along with a type, a variable also has a scope.
 When a variable is declared within a procedure, only that procedure has access to
that variable.
 This is called a local variable because it the variable´s scope is limited to the
particular procedure.

Private Sub Buttonl_Click(ByVal sender As Object, -


ByVal e As System.EventArgs) Handles Buttonl.Click
Dim i As Integer
Dim Total As Integer
For i = 0 to 50 Step 1
Total = Total + i
Next
MsgBox "The Total is" & Total
End Sub

The variables 'i' and 'Total are local variables.


If we are accessing the value of the 'Total' variable from another procedure, Visual Basic
will return an error message that the variable is not declared.
The 'Total' variable is said to have procedure-level scope.
It's visible within the procedure and invisible outside the procedure.

Block-Level scope:
 It is used in a If statement or while loop or for loop.
 A block level scope is visible only in a block of vb code.

If total < 100 Then


Dim i As Integer
i = sum + i
End If

The i variable is not visible outside the block of the if..End If loop.
If we attempt to use it before the If statement, or after the End If statement, VB will throw
an exception.
Module-level scope:

 Variables declared outside any procedure in a module are visible from within all
procedures in the same module, but they're invisible outside the module.
 Setting variables from within many procedures can complicate the debugging of the
software program.

The Lifetime of a Variable:

 The period which a variable holds a value is called the lifetime of a variable.
 Variables declared as Public exist for the lifetime of the software program.
 Local variables, declared within procedures with the Dim or Private statement, are
alive as long as the procedure.
 When the procedure ends, the local variables cease to exist and the allocated memory
is returned to the system.
 When the same procedure is called again the local variables are recreated and
initialized.
 If a procedure calls another procedure, its local variables retain their values while the
called procedure is running.
 With the Static keyword we can force a local variable to preserve its value between
procedures.

Function RunAvg(ByVal dblValue As Double) As Double


RunTotal =RunTotal + dblValue
NumofItems = NumofItems + 1
RunAvg= RunTotal / NumofItems
End Function

We must declare the variables RunTotal and NumofItems outside the function so that their
values are preserved between calls.
We can also declare them in the function with the Static keyword,

Function RunAvg(ByVal dblValue As Double) As Double


Static RunTotal As Double
Static NumofItems As Integer
RunTotal =RunTotal + dblValue
NumofItems = NumofItems + 1
RunAvg= RunTotal / NumofItems
End Function

You might also like