Professional Documents
Culture Documents
UNIT-I
Introducing Visual Basic: Event and Event Procedures – Object-related concepts –VB program
Development Process – Logical Program Organization -VB Program Components – VB
environment – Opening, Saving, Running a VB Project.
Visual Basic Fundamentals: Constants – Variables– Data Types and Declarations – Operators
and Expressions –Program Comments.
VB is based up on an event driven paradigm, in which each feature included within the
program is activated only when the user responds to a corresponding object within the user
interface.
Event:
The programs responds to an action taken by the user is referred to as an event.
Event Procedure:
The group of basic commands that brings out about this response is called an event
procedure.
Objects: Forms and controls are collectively referred to as objects. Objects are also
associated with their own properties and method.
Forms: In VB a window is called a form. Each form includes a title bar at the top. A
form may also include a menu bar, a status bar, one or more tools bars, slide bars,
etc. A user area occupies the remaining space with in the form.
Controls: The icons with which the user interacts are called controls. The user will
typically activate a control to produce an event.
Properties: Objects includes properties that generally define the appearances and behaviors.
For eg. Name, caption, height, back ground color.
Methods: Some objects also include special program statements called methods. A method
brings about some user predefined action affecting the associated objects.
1.4 VB program development process:
The process of writing a VB program consists of several steps. They are
1. Decide what the Program is supposed to do.
2. Create a user interface using VB’s program development tools. This generally involves
two related activities.
a. Draw the control within their respective forms.
b. Define the properties of each control.
3. Write the VB instructions to carry out the action relating from the various program
events. This generally involves writing a group of commands, called event procedure, for
each control.
4. Run the program to verify that it executes correctly.
5. Repeat one or more steps if the results are incorrect, or if the program does not responds
as the user has intended.
MENU BAR
The second line is called a Menu Bar, selecting one of the choices(File, Edit, view,
Project, Format,… Help) causes one of Visual Basic’s drop-down menus to appear.
The menus present logical groupings of Visual Basic’s individual features.
For example, the File menu includes selections for opening new or existing VB projects,
saving the project, saving the currently active window, printing the currently active object,
running a VB project, opening recently accessed objects, and so on.
Visual Basic Environment
TOOL BOX
The Toolbox contains icons that represent commonly used controls, such as label, text
box, command button, picture box, frame, picture box, option button, file list box, and so on. The
user can select a control from a toolbox and place it in the current form design window by double
clicking on the control icon, or by clicking once the control icon, then clicking on the desired
location within the Form design Window and dragging the mouse so that the control has the
desired size.Once a control has been placed in the Form design Window, the associated code can
be viewed or edited by double-clicking on the control.
PROPERTIES WINDOW
Every object has properties associated with it. Each object has its unique list of
Properties. The properties window allows the user to assign or change the properties associated
with a particular object.
Active the object by clicking on it; then choose from the corresponding list of properties
shown in the left column of the properties window. Once a property is selected, the adjoining
box in the right column may change its appearance, showing a drop-down menu so the user can
choose from a list of permissible values.
PROJECT WINDOW
The project window displays a hierarchical list of the files associated with a given
project. These files represent individual forms and modules.
The user can display a form or module within the Project Container Window by double-
clicking on the corresponding icon within the Project Window.
The user can select either the Object View or the Code view by clicking on one of the
two leftmost icons within the toolbar at the top of the Project Window.
We then place the necessary controls in the form design window. In particular, we place
two labels, two text boxes, two command buttons, and a geometric shape on the form. Each
control is selected from the Toolbox, which is positioned to the left of the form design window.
The controls are automatically called Label1, Label2, Text1, Text2, Command1, and
Command2, and the form design window is called Form1. The geometric shape has no default
name.
Each control can be selected two different ways. we can click on the desired control icon
within the toolbox, and then Click on the controls location within the Form design window and
then Drag the mouse until the control has the desired size and shape.
Double click on the desired control icon within the toolbox, placing the control at the
centre of the form design window. we can then drag the control to its desired location an Resize
the control by dragging one of it edges and corners.
Border width 2
Fig 1.14
In this current example, we add the following commands to the First event procedure:
Dim R As Single, A As single
R=Val(Text1.Text)
A=3.14593*R^2
Text2.Text=Str(A)
The First line is a variable declaration, starting that R and A are single precision, real
variables. The remaining three lines are assignment statements; i.e., the information represented
by the item on the right hand side of equal sign is assigned to the item on the left hand side. Thus
the first assignment statement assigns the current value of the radius, entered from Textbox1
Text1 to the variable R. the second assignment statements computes the area and assigns this
value to the variable A. The third assignments statement converts the value of area to a String
and the assigns this String to text property of Text2.
Similarly, we add the command
End
to the second event procedure. This command simply terminates the computation.
Here is a more detail explanation of First event procedure. Text1 is the name of an
object (In this case, a textbox) and Text is the name of the objects associated property. Thus,
Text1.text refers to the Text associated with textbox Text. Similarly, Text2.Text refers to Text
associated with Textbox Text2. Val and Str are library functions. Val returns a numerical value
from a string argument. Str does just the opposite- it returns a string from the numeric argument.
When the program is executed the user must enter a value of radius in textbox Text1 and
click on “Go” button. The radius (Text1.Text) is then converted to a numerical value,
represented by the variable R. The Area (represented by the variable A) is then calculated,
Converted to a String, and assigned to Text2.Text. This String is then displayed in Textbox
Text2.
the completed event procedures for this project. Note that the added commands are
intended, relative to the First and last lines of each event procedure. This is not essential, but it is
considered good programming practice. The indentation allows each event procedure to be
quickly identify and easily read.
Project Execution
When the project is executed (by clicking the Start button in the Menu bar), the window
appears. Entering the value for the radius and clicking on the “Go” button results in a display of
the corresponding area,
The computation is ended by clicking on the end Button.
VISUAL BASIC FUNDAMENTALS
Constants: There are two types of constants:
1. Numeric constant
2. String constant
1. Numeric constant: Numbers are referred to as numeric constants. Most numeric constants are
expressed as
Integers – Whole numbers that do not contain a decimal point or an exponent.
long integers – similar to integers with an extended range
single precision real quantities – numbers that include a decimal point, an exponent or
both.
double precision real quantities – similar to single precision real quantities with an
extended range and greater precision.
The following rules apply to numeric constants:
Commas cannot appear anywhere in numeric constants.
A numeric constant may be preceded by a + or – sign.
An integer constant occupied 2 bytes. It must fall within the range -32,768 to 32,767. It
cannot contain either a decimal point or an exponent.
A single-precision real constant occupies 4 bytes. It must fall within the range
-2,147,483,648 to 2,147,483,647. It cannot contain either a decimal point with 7 significant
figures or an exponent.
A double precision real constant occupies 8 bytes. It can include a decimal point
With 15 significant figures.
Example:
0 1492 0.3D7
1 3D+6 300000
-5280 -613E-7 0.1E+1
2. String constant: A string constant is a sequence of characters i.e., letters, numbers and certain
special characters such as +,-,/,*,=,$ etc.,) enclosed in quotation marks. Blank spaces can be
included within a string. A quotation mark can also be placed within a string, but it must be
written as two adjacent quotation marks. String constants are used to represent nonnumeric
information, such as names, addresses, etc. There is no practical restriction on the maximum
number of characters that can be included within a string constant. Thus, the maximum length of
a string constant can be considered infinite.
Example:
Examples:
Area xmax
Radius account_number
CustomerName c3
Data types and data declarations: Visual basic supports all common data types
including Integer,long integer,single,double and string. The language also supports other data
types such as Boolean,byte, currency and date data, as well as variant-type data and user-defined
data types.
The Dim statement is used to associate variables with specific data types. This process,
which is common to all modern programming languages, is known as data declaration. The dim
statement is written as
Dim variable name 1 As data type 1, variable name 2 As data type 2, etc.
Example:
Variants: the data type of the variable is determined implicity by the value that is assigned to the
variable.
Named Constants: Named constants are similar to variants. However, variables can be
reassigned different values within a program, whereas named constants remain unchanged
through out the program.
Suffixes: Rather than declaring a data type explicitly, a variable or named constant can be
associated with a data type by adding a single-character suffix to the end of the variable/constant
name. Several of the more commonly used suffixes are:
User-Defined Data types: With the help of standard data items such as integers, single-precision
reals, strings etc., the user can define a multicomponent data type. The data type definition is:
Type Customer
CustomerName As String
AcctNo As Integer
Balance As Single
End Type
Once the data type has been defined, the user can declare one or more variables of this data type,
as follows:
OldCustomer.CustomerName NewCustomer.CustomerName
OldCustomer. AcctNo NewCustomer. AcctNo
OldCustomer. Balance NewCustomer. Balance
Operators and Expressions: Special symbols called arithmetic operators, are used to
indicate arithmetic operations such as addition, subtraction, multiplication, division and
exponentiation. These operators are used to connect numeric constants and numeric variables,
thus forming arithmetic expressions.
Arithmetical Operators
+ Add 5+5 10
- Subtract 10-5 5
/ Divide 25/5 5
* Multiply 5*4 20
1. Exponentiation
2. Multiplication and Division
3. Integer division
4. Integer remainder
5. Addition and subtraction
To evaluate the algebraic term, [ 2(a+b)2 + (3c)2 ] m/(n+1). The visual basic expression
corresponding to this algebraic term is: ( 2 * ( a + b) ^ 2 + ( 3 * c) ^ 2) ^ (m / (n + 1)).
Example:
Then the string expression Str1 & “ “ & Str2 & “ DOLLARS” will cause three individual
strings to be concatenated, resulting in single string TEN THOUSAND DOLLARS.
Example: