a63296.files.wordpress.com · Web viewSEC: A. Introduction To VB. ... It functions almost...
Transcript of a63296.files.wordpress.com · Web viewSEC: A. Introduction To VB. ... It functions almost...
DELHI PUBLIC SCHOOL TAPILEARNING PARTNERSHIP 2017-18
Visual Basic Study Material CLASS: IX SEC: A
Introduction To VB
VISUAL BASIC is a high level programming language evolved from the earlier DOS
version called BASIC. BASIC stands for Beginners' All-purpose Symbolic
Instruction Code. The program codes in Visual Basic resemble the English language.
It is created by Microsoft. The basic aim in creating this system was to give a user
friendly environment that supports Graphical User Interface.
VISUAL BASIC is a VISUAL and events driven Programming Language. These are
the main divergences from th1e old BASIC. In BASIC, programming is done in a text-
based environment and the program is executed sequentially. In VISUAL BASIC,
programming is done in a graphical environment. In old BASIC, you have to write a
text-based procedure to design the interface, but Visual Basic enables you to design the
interface by dragging and resizing the objects as well as changing their colors, just like
any windows-based programs.
Visual Basic is event-driven because users may click on a certain object randomly, so
each object has to be programmed independently to be able to response to those actions
(events). Examples of events are clicking a command button, entering text into a text
box, selecting an item in a list box etc. Therefore, a VISUAL BASIC Program is made
up of many subprograms; each with its own program code which can be executed
independently and at the same time can be linked together in one way or another.
Visual Basic Integrated Development Environment(IDE): IDE is used in programming
world to describe the interface and the environment that you use to create your own
applications. It is called integrated because it has many features like designing, editing,
1
compiling and debugging within the common environment.
The Visual Basic IDE is made up of following components:
Menu Bar
Tool Bar
Tool Box
Project Explorer
Property Window
Form Design Window
Form Layout Window
Code Window
Uses Of Visual Basic:
As an education program for teaching
For businessmen, to create programs such as inventory management system,
payroll system, financial program as well as accounting programs
Programs to create games
Starting Visual Basic
1. Steps to open Visual Basic: Click START Menu------>PROGRAMS option -------
MICROSOFT VISUAL BASIC 6.0 -------
2. The visual Basic window appears (Fig Below)
2
3. It shows a startup window known as NEW PROJECT WINDOW.
Note: A project in VB is a collection of different objects like forms. A project is
saved with an extension of .vbp.
The New Project window has three tabs:
a. NEW: It is the default tab with standard EXE option to create a new project.
b. EXISTING: This tab displays the list of the projects created earlier and stored in the
system.
c. RECENT: This tab displays the list of the most recently created projects.
4. Select the NEW option and click on OPEN button.
3
Components of Visual Basic Window
Title Bar: It displays the name of the project along with the mode you are currently
viewing.
a. Menu Bar: It is present below Title Bar. It contains different menus like file, edit,
View, project etc.
b. Toolbox: It contains a set of controls that are used to place on a Form while
designing it, thereby creating the user interface area.
c. Form Design window: It is a window for each form to customize the designed
interface of the application. Using the form designer, the user can add controls,
graphics, and text to create the desired form appearance.
d. Properties Window: It is a List of properties settings for a selected form or a
control. These properties are characteristics (such as size, visible, or color) of the4
selected object it provides an easy way to set properties.
e. Project Explorer Window: It is a list of the forms and modules for the current
projects. lt is a hierarchical tree- branch structure, where the project at top of tree and
other parts like forms ,modules) descend from this tree.
f. Form Layout Window: The Form Layout window is a small screen. Which is used to
reposition the form of the application so that it appears in proper place when project is
run.
g. Code Editor Window: Code Editor Window is used to write a VB code for an
application. For each form there is a separate code editor window. It is displayed when
user clicks on form or object in form.
TOOLBOX AND ITS CONTROLS
POINTER TOOL: It is used to select an object. Whenever any tool is selected then a
mouse pointer changes to a ‘+’ sign. It is also the default tool of the toolbox.
LABEL TOOL: It is used to display text on the form.
TEXT BOX: It draws a rectangular box on the form where the cursor blinks to accept
input from the user.
COMMAND BUTTON: It works when the user clicks on it. It supports the vent
driven programming in VB. The most commonly used event is Command_click().
CHECKBOX TOOL: It is used when the user has got the choice to select from one
or more options.
OPTION BUTTON: They are also called as Radio button. It is used to display
options from which the user is allowed to select only one.
5
FRAME TOOL: It is used to group the controls together
LIST BOX TOOL: It permits the programmer to load it up with a number of items,
allows the user to make a selection, and then proceed selection.
COMBO BOX TOOL: It is also used to present a list of items where the user can
select items from the list.
TIMER TOOL: It is used to fire an event at specific intervals.
SHAPE TOOL: It is used to draw different shapes on the form.
PICTURE TOOL: It is used to handle graphics. You can load a picture at design
phase by clicking on the picture item in the Properties window and select the picture
from desired window.
IMAGE TOOL: It handles images and pictures. It functions almost identically to the
picture box. However, there is one major difference, the image in image box is
stretchable while this feature is not available in picture box.
Operators In VB
VB allows us to make various types of operations on numbers and text.
Variable: Variable is an area reserved in the memory to store values of specific data type.
The value can be words, numbers, dates and many more.
When a value is stored in a memory location, is required to be used, modified or removed
then it is referred by a name assigned to it.
A VB variable must follow the given rules for naming:
The first character must be a letter A-Z(uppercase or lowercase). Succeding
characters can be letters, digits, or underscore (-).
It should not contain any space between any two characters.
It should not exceed more than 255 characters.
6
Name should not be a reserved word (VB keyword)
It should not contain any special character like %,&,!,# etc.
NOTE: A variable can store only a specific type of value depending on the data type
used at the time of variable.
VB is not case sensitive. For ex: Num, NUM and num all refer to same variable.
DATA TYPES IN VB
Data Type Function
Integer -2,147,483,648 through 2,147,483,647 (signed); 4 bytes
Long -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807(signed); 8 bytes
Single -3.4028235E+38 through -1.401298E-45 for negative values;4 bytes
Double -1.79769313486231570E+308 through -4.94065645841246544E-324, for negative values; 8 bytes
String 0 to approximately 2 billion (alphanumeric) characters
Date 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999
Boolean True or False
Declaring a variable:
Declaring a variable tells Visual Basic to reserve space in memory. There are two ways
of declaring a variable:
Implicit Declaration: It is not always necessary to declare a variable before it is used.
Automatically whenever VB encounters a new variable, it is assigned a default variable
type and value.
7
Explicit Declaration: When the declaration of the variable is required before it can be
used in a program then it is known as an explicit declaration. It is advisable to declare the
variable explicitly. The variable is declared by DIM statement.
The syntax is:
DIM variable name as Variable Type
ex: DIM rate as Integer
OPERATORS
There are basically three operators used in VB. They are as follows
1. Arithmetical Operators
2. Relational Operators
3. Logical Operators
ARITHMETICAL OPERATORS:
These operators are used for arithmetic operations.
Operator Description Example Result
+ Add: Add two values. 19 + 3 22
_ Subtract: Subtract one number from another. 19 – 3 16
/ Divide: Divide one number by another. 19 / 3 6.33
* Multiply: Multiply two number 19 * 3 57
\ Integer Division : Displays only the integer (without decimal places) after division 19 \ 3 6
Mod Modulus: Returns the remainder after 19 Mod3 1
8
RELATIONAL OPERATORS:
These operators are used to compare two values. These operators compare the values and return the true or false result.
Operator Description Example Result
>Greater Than: Checks whether a number is greater than other or not. 15 > 9 True
< Less Than: Checks whether a number is less than other or not. 4 < 7 True
=Equal To: Checks whether a number is equal to other or not. 14 = 18 False
>=Greater than or equal to: Checks whether a number is greater than or equal to other or not.
18 > =18 True
<=Less than or equal to : Checks whether a number is less than or equal to other or not.
7< = 7 True
< >Not equal to: This operator returns true if the numbers are not equal. 4 < > 6 True
LOGICAL OPERATORS:
These operators are used to combine two or more conditions.
Operator Description
OR It checks whether any of the given statement is true or not.
AND It checks whether both the statements are true or not
9
CONTROL STRUCTURES:
The statement in a program is executed in a sequence. But sometimes, we wish to change the flow of control.
Control statement is useful in these situations.
They are used to control the execution of a program, either by repeating, or
by skipping the execution of a statement.
There are mainly two types control structures.
1. If – Then – Else
2. Select Case
1) If – Then - Else
The selection control structure allows one set of statements to be executed if a
condition is true and another set of actions to be executed if a condition is false.
A selection structure, also called an "If-Then-Else" structure, is flowcharted as
follows:
10
SYNTAX:
If <conditional expression> Then
<one or more statements to be executed if condition is true>
Else
<one or more statements to be executed if condition is false>
End If
If the conditional expression is true, the statements between the keywords Then will
be executed.
If the conditional expression is false, the statements between the keywords Else will
be executed
In any case, program control will resume with the statement following End If.
Example:-
If AvgGrade >= 60 Then
Message = "You passed!"
Else
Message = "You failed!"
End If
2) Select Case
The Case Structure When the situation arises where you need to choose between more than two alternatives, an extended form of the selection structure, called the case structure, must be used.
A flowcharted example follows:
11
Syntax:
Select Case Expression
Case value1
Vb statements…
Case value1
Vb statements…
Case Else
Vb statements…
End Select
Example of case structure:
Dim n as Integer
N= val(text1.text)
Select case n
Case Is = 1
Msgbox (“ Sunday”)
Case Is = 2
12
Msgbox (“ Monday”)
Case Else
Msgbox (“ Wrong input”)
End select
End Sub
Loops (Repetition Structures) In Visual Basic Structures )
Basic
A repetition structure allows the programmer to that an action is to be repeated until given condition is true.
Do While... Loop Statement
The Do While...Loop is used to execute statements until a certain condition is met. The following Do Loop counts from 1 to 100.
Dim n As Integer
n = 1
Do While n <= 100
n = n + 1
Loop
A variable number is initialized to 1 and then the Do While Loop starts.
First, the condition is tested; if condition is True, then the statements are executed.
When it gets to the Loop it goes back to the Do and tests condition again.
If condition is False on the first pass, the statements are never executed.
13
The For...Next Loop
The For...Next Loop is another way to make loops in Visual Basic.
For...Next repetition structure handles all the details of counter-controlled repetition.
The following loop counts the numbers from 1 to 100:
********** CODING ***********
Dim x As Integer
For x = 1 To 50
Print x
Next
*********************************
In order to count the numbers from 1 to 50 in steps of 2 the following loop can be used:
********** CODING ***********
Dim x As Integer
For x = 1 To 50 Step 2
Print x
Next
********************************
The following loop counts numbers as 1, 3, 5, 7..Etc
The above coding will display numbers vertically on the form.
In order to display numbers horizontally the following method can be used.
14
For x = 1 To 50
Print x & Space$ (2);
Next
To increase the space between the numbers increase the value inside the brackets after the & Space$.
Following example is a For...Next repetition structure which is with the If condition used.
Dim n As Integer
For n = 1 To 10
If n = 4 Then
Print "This is number 4"
Else
Print number
End If
Next
In the output instead of number 4 you will get the "This is number 4".
String Functions and commands in VB
15
UCASE( )
Convert given string into upper
case.
UCASE(“dpst”) DPST
LCASE( )
Convert given string into lower
case.
LCASE(“DPST”) Dpst
TRIM( )Removes the leading and
trailing space from
string
TRIM(” DPST “) “DPST”
16
FUNCTION NAME USE EXAMPLE OUTPUT
Len( )
Return the number of character in string.
Print len(text1.text)Print len(“Hello”)
Length of textbox
data5
LEFT( )
Return the N number of characters from the left side of string
Print left(text1.text,2)
Print left(“Hello”,3)Hel
RIGHT( )
Return the N number of characters from the right side of string
Print right(text1.text,2)
Print right(“Hello”,3)llo
MID( )
Return N Number Of Character From The Middle Of The String
PRINT MID(“DPSTAPI”,3,2) ST
LTRIM( )Removes the
leading space from
string.
LTRIM(” DPST “) “DPST “
RTRIM( )Removes the
trailing space from
string.
RTRIM(” DPST “) ” DPST”
17
INSTR( )Search String Within String
PRINT INSTR(“DPSTAPI”,”TAPI”) 4
Difference between For Next and Do While Loop:
For...Next repetition structure handles all the details of counter-controlled repetition.
The Do While...Loop is used to execute statements until a certain condition is met.
18
VAL( ) Convert Text Into Number Format.
DATE( ) Current System Date
TIME( ) Current System Time
NOW ( ) System Date An Time
END COMMAND
To Exit From Vb
SINGLE QUOTE
CHARACTER
To Give Remark In Program. This Will Not Execute While Running Program.
‘PROGRAM FOR SIMPLE INTEREST.