Anda di halaman 1dari 95

Meaning

of
Visual Basic
1

VB as Programming Language
Visual Basic is a Programming tool that allows user to develop Windows or
GUI ( Graphical User Interface ) applications. It means that rather than writing numerous line for code to describe the appearance and location for interface elements like C , C + + or Basic. User can simply drag and drop pre-built objects into the form window on computer screen. VB is a much enhanced version of BASIC programming language ( BASIC is Predecessor of VB ). The main difference is that the VB can create windows programs by just Drag & Drop where as BASIC could only create DOS programs or work only in CUI (Command User Interface ).

Features of Visual Basic ( VB )


VB introduces the concept of Event Driven programming Model.

It has built in Database Handling Features of VB.


It incorporated the concepts of objects. It has useful Error Handling Features.

It has Front End Application Developer.


It also known as RAD ( Rapid Application Development )

Event & Event Procedure

An event is an action recognized by an object, such as clicking a mouse or pressing a key. User can write code to respond to that event. Each object has different events that it recognizes.

An event procedure is a section of code that is executed when an event (like clicking a command button) takes place. Event procedures can be added to your project in the code editor window.

Program Development Process


It is a process in which an ordered sequence of steps is defined to

develop an application. It includes : How the Program should Work Plan about User Interface

Implement it
Test it

Starting Visual Basic


6

STARTING VB APPLICATION
Opening a Project : -

To open a VB project , invoke VB6 shortcut on the Desktop or going through the pull-up menu select VB program .
Start Program Microsoft Visual Basic 6.0 .

Visual Basic I D E

When user choose any application platform, like any other window application, VB consists of multiple window, which appear at start up. The windows that are display when user start VB are collectively Known as

Visual basic Integrated Development Environment ( IDE ).

Visual Basic I D E
Title bar Toolbar Menu bar

Project Window

Toolbox

Form Window

Properties Window

Form Layout Window Visual Basic Integrated Development Environment


9

Properties Window
The Properties window let user to change the characteristics, or property settings, of the form itself and of visual interface elements on the form. The Properties window consists of two columns: the first is the name of the property, which user cannot change; the second column, which is the property setting that user can change. Any Change in the property of the object will affect the object on the form.

10

Tool Box
The toolbox consists of the various objects that user will use to design the application. These objects, called controls, are objects that user see in all standard Win applications, such as textboxes, command buttons, option (radio) buttons, checkboxes, etc.

11

Form Window
The Form window is the central area where user can draw application. The forms are windows which holds control like Button , Checkbox etc which make user application. The large area of form is called client area for working with different controls.

12

Source Code Window


The Source Code window is where user type the code which VB execute. It has two parts Control Box , Event Box . The Heading of window indicate which event the code is associated . It is a window where user write most of the code .

13

Source Code Window

It shows the control Box which contains two controls ( Command 1 & Form ).

14

Source Code Window

It shows the Event Box that Selected Control can perform these event.

15

Saving a VB program
Saving a Project : - After creating a Project , user need to save it . For this click File menu and select save project. Then system asks : 1. Provide a name for Form File ( . frm ) 2. After that Provide a name for Project file ( .vbp )

16

Running a program
Running a Project : - After saving the project , the next step is to

execute the program. Following are different ways to execute a


project. 1. Press F5 Key

2. On Menu Bar Click Run - Start 3. On VB Toolbar , Click Run Icon.

17

Opening a vb program
After you created and saved a file , you can open it at any time. Click file openselect your vb program from the open dialog ok Then you can get your saved vb applivation.

18

Steps in Developing Application


1. Draw the user interface 2. Assign properties to controls 3. Attach code to controls

Draw the User Interface


Visual Basic operates in three modes. Design mode - used to build application Run mode - used to run the application Break mode - application halted and debugger is available

Getting Started

7 Windows
1.

2.
3. 4. 5. 6. 7.

Main Window Form Window Toolbox Properties Window Form Layout Window Project Window Code Editor Window

Main Window

consists of the title bar, menu bar, and toolbar.

Form Window

is central to developing Visual Basic applications. It is where you draw your application.

Toolbox

is the selection menu for controls used in your application.

Properties Window

is used to establish initial property values for objects.

Form Layout Window

shows where (upon program execution) your form will be displayed relative to your monitors screen.

Project Window

displays a list of all forms and modules making up your application.

Code Editor Window

Contains the actual Basic coding.

Assign Properties to Controls

Naming Convention of Controls


Object Form Command Button Label TextBox Menu Prefix frm cmd, btn lbl txt mnu Example frmHello cmdExit, btnStart lblStart, lblEnd txtTime, txtName mnuExit, mnuSave

Check box

chk

chkChoice

Attach Code to Controls


Code in a Visual Basic application is divided into smaller blocks called procedures. Event procedure syntax:

object name_event () Ex. Command1_Click ()

Private Sub object name_event ()

End Sub ___________________________________ ____ Private Sub Command1_Click ()


End Sub

Private Sub Command1_Click ()

object name.property = value End Sub ___________________________________ ____ Private Sub Command1_Click ()
Text1.Text = "Hello, world!" End Sub

Variables
Variable is a data name that is used to strore a value. Syntax : Dim variablename as datatype Example : Dim a as Integer Dim b as Integer,c as Double,d as Date
34

Rules used in naming variables

Variable name cannot exceed 255 characters. They may include letters, numbers, and underscore (_). The first character must be a letter. You cannot use a reserved word like Dim,If,Else etc.

Vb is not case sensitive.

Data Types
VB supports all common data types such as Bytes,Boolean,Date,Integer,String, Double and variant etc. Integer To store a integer value String To strore a String (group of characters). Double To store decimal value.

Visual Basic Controls

Controls are the elements of the User Interface with which the user can interact and control the application. Form is the first and main element of the user interface which acts as container for other elements. The basic controls are displayed as icons on the toolbox which is a part of the interface during the design time.
37

TOOL BOX

38

PICTURE BOX & IMAGE BOX


Displays a graphic image. It can also serve as a container for other controls

Displays a graphic image. Similar in appearance to the picture control but with different functionality. Main property: Strutch =true

39

LABEL ,TEXT BOX & BUTTON


To pass information to the user through text.

Displays text that the user can edit. Main Properties : Multiline to enter multiple lines in text box. Password char to enter password format. Scollbar To make vertical or horisontal Scrollbars. Enables the user to initiate a program action.

40

FRAME & TIMER


Serves as a container for other controls. Provides a method for grouping controls. (To group controls in a frame, select the frame with a single-click first, then draw a control in it.)

Provides a means for an action to be taken after passage of a certain amount of time. Main Property : Interval property integer value ranges from 0 to 65535.A zero value disables the timer.Positive value represents the number of milliseconds between timed events.
41

CHECK BOX & OPTION BUTTON


Displays or enables input of a two-part choice, such as Yes/No or True/False.At a time more than one check box can be selected. Value 1=checked and 0=unchecked. Displays or enables a choice among multiple items. (Also known as a radio button.). Only one option can be select at a time. True=option selected and False=Option deselected.

42

OLE & DATA CONTROL


OLE=Object linking and embedding which is used to place other files such as word,poerpoint,excel etc with the vb form.

Provides a link to database files.It act as a mediator between front end (vb) and back end (database=access). That means it connects vb and access.

43

COMBO BOX & LIST BOX


Enables the user to select an entry from a list or enter a new value. It is a combination of text box and list box. Value can to added to it through coding or by using list property in pro.pert box

Displays a list of items from which the user can select one or more entries. Value can to added to it through coding or by using list property in pro.pert box

44

HscrollBar & VscrollBar


Produces a numerical value based on the scroll bar's horisontal position.

Produces a numerical value based on the scroll bar's vertical position.

45

DriveListBox & Dir ListBox & FileListBox


Displays and enables a user to choose from available disk drives on the computer.

Displays and enables a user to choose from available subdirectories on a drive. Displays and enables a user to choose from available files in a directory.

46

LINES & SHAPES


Displays lines on the form

Displays geometric shapes on the form such as circle,rectangle,oval etc.

47

OPERATORS
VB contains the following operators. Arithmetic operator Assignment operator Relational operator Logical Operator

48

ARITHMETIC OPERATOR
SYMBOL + MEANING

Addition Subtration

*
/

^ Mod

Multiplication Common division operator that gives result in real number (decimal number). Used for integer division that gives result in integer. Exponential (To get the power value.). To get the remainder.
49

ASSIGNMENT OPERATOR
The = sign is used to assign a value to a variable. Example : Dim a as Integer a=10

50

RELATIONAL OPERATOR

SUMBOL = <> < > < >

MEANING Equal Not Equal Less than Greater than Less than or equal to Greater than or equal to

51

Logical Operator

The logical operators are

SYAMBOL AND OR NOT

MEANING Both conditions must be true. Either one condition is true. True changes to false case and vice versa

52

VB Control Structures
CONTROL FLOW

In a program, statements may be executed sequentially, selectively or iteratively. Every programming language provides constructs to support sequence, selection or iteration. So there are three types of programming constructs : Sequential Constructs Selection Constructs Iterative Constructs
53

Sequential Construct

The sequential construct means the statements are being executed sequentially. This represents the default flow of statements.
Stament 1

Stament 2
Stament 3

54

Selection Construct
The selection construct means the execution of statement(s) depending upon the condition-test. If a condition evaluates to true, a course-of-action (a set of statements) is followed otherwise another course-of-action is followed. This construct is also called decision construct as it helps in decision making.
55

Selection Construct
Condition ?

true

One course-ofaction Statement 1 Statement 2

false Statement 1 Another courseofaction

Statement 2

56

Iterative Constructs
The iterative or repetitive constructs means repetition of a set-of-statements depending upon a condition-test. A setof-statements are repeated again and again till the condition or Boolean Expression evaluates to true. The iteration constructs are also called as looping constructs.
57

Iterative Construct
false The exit condition
Condition ?

True Statement 1 The loop body

Statement 2
58

Selection Constructs

VB provides two types of selection construct : 1) If statement 2) Select Case statement

The If Statement : If statement of VB comes in various forms & are given below: 1) If..Then Statement 2) If..Then..Else Statement 3) If..Then..ElseIf Statement 4) Nested Ifs

If..Then Statement
Def. : An If..Then statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed otherwise it is ignored. Syntax : If (condition) Then statements End If

If..Then Statement
Example 1. : If (Num>0) Then Print It is a positive number End if

Example 2 : If txtAge.Text>=18 Then Print You are eligible to vote End if

If..Then..Else Statement

If..Then..Else statement provides an alternate choice to the user i.e. if the condition is true then a set of statements are executed otherwise another set of statements are executed. Syntax : If (condition) Then VB Statement(s) Else VB Statement(s) End If

Examples of If..Then..Else

Example 1 : If txtAge.Text>=18 Then Print You are eligible to vote Else Print Sorry, You are not eligible to vote End If

Example 2 : If Num Mod 2=0 Then Print It is an Even Number Else Print It is an Odd Number End If

If..Then..ElseIf Statement
If..Then..ElseIf statement is used to test a number of mutually exclusive cases and only executes one set of statements for the case that is true first. Syntax : If (condition 1) Then Statement(s) ElseIf (condition 2) Then Statement(s) ElseIf (condition 2) Then Statement(s) : Else Statement(s) End If

Example of If..Then..ElseIf
If (Age<=4) Then Print Your rate is free. ElseIf (Age<=12) Then Print You qualify for the childrens rate. ElseIf (Age<65) Then Print You must pay full rate Else Print You qualify for the seniors rate. End If

Nested Ifs
A nested If is an if that has another If in its ifs body or in its elses body. The nested if can have one of the following three forms : 1. If (expresssion 1) Then If (expression 2 ) Then Statement 1 [Else Statement 2 End If Else body-of-else] End If

Nested Ifs
2. If (expression 1) Then body-of-if Else : If (expression 2) Then Statement-1 [Else Statement-2] End If

Nested Ifs
3) If (expression 1) Then : If (expression 2) Then Statement-1 [Else Statement-2] End If Else If (expression 3) Then Statement-3 [Else Statement-4] : End If End If

Example of Nested Ifs


If Num>0 Then Print It is a positive number Else If Num<0 Then Print It is a negative number Else Print The number is equal to zero End If End If

Select-Case Statement

Select-Case is a multiple branching statement and is used to executed a set of statements depending upon the value of the expression. It is better to use SelectCase statement in comparison to If..Then..ElseIf Statement when the number of checks are more. There are 3 different forms of using Select-Case statements and are given below :

Different forms of Select-Case


1. Select Case : Simplest Form [Exact match] Select Case Expression Case Value one or more visual basic statements Case Value one or more visual basic statements Case Else : one or more visual basic statements End Select

Example of Form 1
Select Case byMonth Case 1,3,5,7,8,10,12 number_of_days=31 Case 2 number_of_days=28 Case 4,6,9,11 number_of_days=30 End Select

Syntax of Form 2
Select Case : Second Form [Relational Test] Select Case Expression Case is relation : one or more visual basic statements Case is relation : one or more visual basic statements [Case Else : one or more visual basic statements End Select

Example of Form 2
Select Case marks Case Is < 50 Result = Fail Case Is < 60 Result = Grade B Case Is < 75 Result = Grade A Case Else Result = Grade A+ End Select

Third Form of Select Case


Select Case : Third Format [Range Check] Select Case Expression Case exp1 To exp2: one or more visual basic statements Case exp1 To exp2: one or more visual basic statements [Case Else: one or more visual basic statements End Select

Example of Form 3
Select Case Age Case 2 to 4 : Print PreNursery Case 4 to 6 : Print Kindergarden Case 6 to 10 : Print Primary Case Else : Print Others End Select

Iterative Constructs (Looping Structures)

A loop is said to be the set of instructions which are repeated again and again in a program.

TYPES OF LOOP
1)

2)

Sentinel-controlled Loop Structures : repeat statements until a special value called sentinel value (or the terminating value) is reached. Counter-controlled Loop Structures : repeat the set of statements until the value specified by the counter variable is reached.

78

Looping Structures
1. 2.

3.

VB offers broadly following three types of looping structures : For..Next Do Loop a) Do While..Loop b) Do..Loop While c) Do Until..Loop d) Do..Loop Until While..Wend

For..Next Statement

This type of statement is used when the user knows in advance how many times the loop is going to be executed.

Syntax :

For <counter Variable>=<start_val> To <end_val> Step <increment/Decrement Value> One or more VB Statements Next <counter Variable>

Examples
Example

1 : Generate natural nos from 1

to 100 For I = 1 To 100 Print I Next I Example 2 : Generate first 20 even nos. For E = 2 to 40 Step 2 Print E Next E

More Examples
Example

3 : Generate odd nos from 100 to 30 in a list box. For O = 99 to 31 Step -2 ListO.AddItem(O) Next O Example 4 : Generate table of any number N. For T = 1 To N Print N; *; T; =; N*T Next T

More Examples

Example 5 : Find factorial of a given number N.

: Fact=1 For I= 1 to N Fact = Fact * I Next I Print Factorial of ; N; =; Fact :

Do..Loop Structures
Do While..Loop : Do While loop is an entry controlled loop in which the condition is placed at the entry point. This statement executes the statements specified in the body of the loop till the condition evaluates to true. The loop may not be executed at all the if the condition is initially false. Syntax : Do While <condition or boolean expression> One or more VB Statements Loop

Examples of Do While..Loop

Example 1 : Never executes loop Dim A as Byte A=10 Do While A>10 A=A-1 Loop Example 2 : Executes loop Dim P as Byte P=20 Do While P>5 P=P-2 Loop

Do..Loop While
Do Loop While is an exit controlled loop as the condition is placed at exit point. The body of the loop is going to be executed at least once whether the condition evaluates to true or false. Loop is executed as long as the result of the condition remains true. Syntax : Do One or more VB Statements Loop While <condition or Boolean Expression>

Examples
Example 1 : Do num = InputBox (Enter a number) sum = sum + num Loop While num < > 0 Here the statements inside the loop will be executed once no matter what the comparison test evaluates to.

Do..Until Loop

Do Until loop is an entry controlled loop in which the condition is placed at the entry point. This statement executes the statements specified in the body of the loop till the condition evaluates to false. The loop may not be executed at all the if the condition is initially true. Syntax : Do Until <condition or boolean expression> One or more VB Statements Loop

Examples of Do Until..Loop

Example 1 : Never executes loop Dim A as Byte A=10 Do Until A<10 A=A-1 Loop Example 2 : Executes loop Dim P as Byte P=20 Do Until P<5 P=P-2 Loop

Do..Loop Until
Do Loop Until is an exit controlled loop as the condition is placed at exit point. The body of the loop is going to be executed at least once whether the condition evaluates to true or false. Loop is executed as long as the result of the condition remains false. Syntax : Do One or more VB Statements Loop Until <condition or Boolean

Examples
Example 1 : Do num = InputBox (Enter a number) sum = sum + num Loop Until num = 0 Here the statements inside the loop will be executed once no matter what the comparison test evaluates to.

While..Wend

While..Wend loop is functionally equivalent to the Do While..Loop. It executes a set of VB statements till the condition evaluates to true.

Syntax : While <Condition> one or more vb statements Wend

Examples
Example 1 : Generate the sum of first 10 natural nos I=1 While I<=10 Sum = Sum + I I=I+1 Wend Print Sum of 10 natural nos = ; Sum

Nested Loops
A loop within another loop is called as Nested Loop. Example : For I = 1 to 5 For J = 1 To 3 Inner Loop Print J Outer Loop Next J Print Next I

Working of Nested Loops


In nested loops the inner loop is executed completely for one time execution of the outer loop. In the Previous example the Inner Loop will be executed three times for every execution of the outer loop. Nested loops are very useful when there is a requirement to generate different kind of patterns as output.

Anda mungkin juga menyukai