Welcome to New Semester

Post on 24-Jan-2016

23 views 0 download

description

Welcome to New Semester. CS-65. Session-1. Visual Basic. About Visual Basic - PowerPoint PPT Presentation

Transcript of Welcome to New Semester

An ISO 9001:2000 Certified Organization

Welcome to New Semester

CS-65

© Copyright PCTI Group 2009 | | <document classification>

Session-1

© Copyright PCTI Group 2009 | | <document classification>

Visual Basic

© Copyright PCTI Group 2009 | | <document classification>

About Visual Basic

Visual Basic is a Windows based programming Language for developing professional applications. Visual Basic is actually BASIC language, which is visual in its nature.Visual is a method used to create GUI applications.The GUI(Graphical User Interface) which enables users to interact with an application. Microsoft developed this language in 1991.

© Copyright PCTI Group 2009 | | <document classification>

Successor of BASIC language

GUI(Graphic User Interface) Support.

Very Easy and Understandable

IDE(Integrated Development Environment)

Access to Databases(Relational and non

Relational)

Supports OOPS

Event Driven Language

Features of Visual basic

© Copyright PCTI Group 2009 | | <document classification>

Quick Development

Access to Windows API

ActiveX and OLE

Internet Support

Quick error Detection/Correction

© Copyright PCTI Group 2009 | | <document classification>

Visual basic Environment

The working environment in VB integrates many different functions such as design, editing, compiling and debugging with a common environment. The working environment of VB is also referred to as the Integrated Development Environment

© Copyright PCTI Group 2009 | | <document classification>

Visual Basic Integrated Development Environment

When you start new project, VB shows this screen. This is visual Basic Integrated Development Environment. IDE means it integrates many different functions such as design, editing,

compiling, debugging within a common environment.

© Copyright PCTI Group 2009 | | <document classification>

What is Project

Visual Basic Programs are organized into Projects. A Project is a collection of various components. Projects contains different Forms.

•The extension of Project files are (.VBP)

•The Extension of Form modules files are (.FRM)

•The extension of Class modules files are (.CLS)

•The extension of Standard modules files are (.BAS)

•The extension of Custom control files are (.VBX or .OCX).

© Copyright PCTI Group 2009 | | <document classification>

Creating Project in Visual Basic

There are three main steps to creating an applications in Visual Basic

1. Create the Interface

2. Set the Properties

3. Write the code.

© Copyright PCTI Group 2009 | | <document classification>

ToolboxThe set of controls available in the Toolbox.Toolbox contains controls used to customize forms.

© Copyright PCTI Group 2009 | | <document classification>

Form Properties

Properties means the characteristics that describe the behavior of an object or control.

Control

An Object or a Control has some properties such as its name, color, appearance, caption etc. known as its properties.

Name

Caption

Enabled

Height and Width

© Copyright PCTI Group 2009 | | <document classification>

Project Explorer Window

This window contains three buttons

1. View code

2. View Object

3. Toggle folder

© Copyright PCTI Group 2009 | | <document classification>

Form Events

Events are the activities that happen when an object does something.

An Event is any user action directed at the application.

Different Event Procedures are

click, DbClick, Load, UnLoad etc.

.

© Copyright PCTI Group 2009 | | <document classification>

An Event Handlers consists of :-

three parts

Private Sub Form_Click()

Statements of code

End Sub

•The First Line ‘ Private Sub Form_Click()’ is the the event definition

•The Second Line writes the code of statements

•The Third Line ‘End Sub’ the end of subroutine

© Copyright PCTI Group 2009 | | <document classification>

Print CommandPrint command used to print the message

or result on the screen.

print “welcome”

print 10+20

© Copyright PCTI Group 2009 | | <document classification>

Basic Controls1. Label Control:- This control

displays text on the Form.

Label PropertiesLabel Properties

Name Caption Forecolor

Alignment Backcolor

Appearance Font

© Copyright PCTI Group 2009 | | <document classification>

2. Text Box Control:This Control used to get the data from user.

PropertiesName

MultiLine

Max Value

Scrollbar

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

3. Command Button

The command button Control allows users to click it to perform actions.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

4. Shape and Line Control: This controls are useful for drawing graphical elements on the surface of a form.Different shapes are available, it can be displayed as rectangle,Circle or square. The Border Color and Fill Color properties can be set to change the color, the border style, border width.

Rectangle 0

Square 1

Oval 2

Circle 3

© Copyright PCTI Group 2009 | | <document classification>

Example of

Shape Control

© Copyright PCTI Group 2009 | | <document classification>

4. Picture Box Control:

Picture Box is an all purpose graphics container. We can use it to displays the contents of .BMP, .JPG, .GIF graphic files.

5. Image Control: Image Control is similar to the Picture Box Control but is used only for displaying Picture

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

6. Check Box

A check Box provides the user with a toggle choice. It indicates whether a condition is on or off.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Properties

ListIndex

Selecteditem

Methods

Additem

Clear

Listcount

Style

Removeitem

7. Combo Box

It present a list of choices to the user that only the selected item is displayed. It Combine the features of Text Box and a List Box.

© Copyright PCTI Group 2009 | | <document classification>

Example of Combo Box

© Copyright PCTI Group 2009 | | <document classification>

8. List Box Control:List Box Control long lists of options from which

users can choose one. By default, the choices are displayed vertically in a single column.

Properties

Listindex

Selecteditem

Methods

Additem

Clear

Listcount

Style

Removeitem

© Copyright PCTI Group 2009 | | <document classification>

Example of List

© Copyright PCTI Group 2009 | | <document classification>

9. TIMER Control

The timer Control can be used to create an event in your application at a regular interval.

Property:

Interval

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

10. HSrcollBar and VScrollBar Controls

A Scrollbar is a indicator that lets the user select a value between the two ends of a control.

Property

Min

Max

Value

Large change

Small change

© Copyright PCTI Group 2009 | | <document classification>

Example of Scrollbars

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

File System Controls:DriveListBox, DirListBox, FileListBoxDriver

DrivelistBox:- Displays the names of the drives connected to th PC.

DirListBox: Displays the folders of current drive

FileListBox: Displays the files of the current folder.

© Copyright PCTI Group 2009 | | <document classification>

Example of

Drive, Dir & File Control

© Copyright PCTI Group 2009 | | <document classification>

OLE Container

The OLE (Object Linking Embedding) Control is an easy way to add capabilities such as Microsoft Excel, word, Paint brush and many others

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Dialog Boxes

Dialog Boxes is a window used to display and accept information

Dialog boxes can either be Modal or Modeless

1. Modal Dialog Box

2. ModeLess Dialog Box

© Copyright PCTI Group 2009 | | <document classification>

Modal Dialog Box does not let the user continue working with rest of the application until it is closed ex- Font, Paragraph dialog box.

Modeless Dialog Box lets the shift focus between the dialog box and another form without having to close the dialog box. Ex. Find dialog box

© Copyright PCTI Group 2009 | | <document classification>

Predefined Dialog Boxes

Visual Basic provides two predefined dialog boxes that you can use in your applications:

1. Inputbox Function

2. Msgbox Function

© Copyright PCTI Group 2009 | | <document classification>

Inputbox

•The InputBox function is used to input the data from user. This function displays a modal dialog box that asks the user to enter some data.

syntax:inputbox(prompt,[title],[default],[xpos],[ypos],[helpfile],[context] as string

© Copyright PCTI Group 2009 | | <document classification>

Example

I=inputbox(“enter no”,”hello”,0)

output:

© Copyright PCTI Group 2009 | | <document classification>

Msgbox

The Msgbox function is used to get yes or no responses from users and displays the message on the screen. It displays a message in a dialog box.

Syntax for displaying

Msgbox(prompt,[button],[title],[helpfile],[context])

© Copyright PCTI Group 2009 | | <document classification>

example

msgbox (“hello”, ,”message”)

Output:

© Copyright PCTI Group 2009 | | <document classification>

Dim ans As Integerans = MsgBox("Do you want to continue", vbYesNo, "Information")If ans = vbNo ThenUnload MeElseText1.Text = ""End If

© Copyright PCTI Group 2009 | | <document classification>

DescriptionButton Constant

Display Information Message iconvbInformation

Display an Exclamation markvbExclamation

Display a Question markvbQuestion

Display Stop signvbCritical

Display Yes and No buttonsvbYesNo

Display Yes, No, and Cancel buttonsvbYesNoCancel

Display OK and Cancel buttonsvbOKCancel

Display OK button onlyvbOKOnly

Choices for MsgBoxes

© Copyright PCTI Group 2009 | | <document classification>

NoVBNo7

YesVBYes6

IgnoreVBIgnore5

RetryVBRetry4

AbortVBAbort3

CancelVBCancel2

OKVBOk1

Button ClickedVisual Basic Constant

ValueReturned

Values Returned by MsgBox Function

© Copyright PCTI Group 2009 | | <document classification>

Operators in Visual BasicOperators are the systems or words that trigger an action on some data.. The data on which operators operate, are called Operators operate, called Operands.

1. Arithmetic Operators:-

VB supports a number of different math operators that can be used in program statements.

1. Addition 2. Subtraction 3 Division

4. Multiplication 5. Mod

2. Comparison Operators:- These operators are used to compare the values of two or more variables.

= <> > >= <<=

© Copyright PCTI Group 2009 | | <document classification>

3. Logical Operators:- The logical operators are used to combine

two or more checking condition.

And Both conditions should be true

Or Either one of the condition should be true

Not Negates the result of a condition

© Copyright PCTI Group 2009 | | <document classification>

Symbolic Constants

We can assign a name to a constant with the Const statement: const constant_name as variable type = valueExampleConst IntRate as single = 0.07

© Copyright PCTI Group 2009 | | <document classification>

Exercise

• Create a Calculator (Add, Sub, Multi, Divide) Buttons

• Input the Marks in Two Lists. Print the sum in Third List.

• Create Three Combo [combo1(Size), combo2(Font), combo3(Font style) ]

• Create Payslip of Employees

© Copyright PCTI Group 2009 | | <document classification>

Session-2

© Copyright PCTI Group 2009 | | <document classification>

Variables are used to store the data in memory. (memory cells) Stored data that may change at run timeVariable names in VB:

Must begin with letterCan contain letters, numbers and the underscore _ charactercan’t include a periodcan’t be over 255 charactersare not case-sensitive

Variables In Visual Basic

© Copyright PCTI Group 2009 | | <document classification>

Data types In Visual Basic

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

2 options to declare a variable:

Inside a procedureIn the General Declaration Section

Declaring Variables

© Copyright PCTI Group 2009 | | <document classification>

Inside a procedure:syntax:Dim/static <varname> as <datatype>example:Dim I as integerDefault datatype is variant.Dim provides a local access and the variable is destroyed once the procedure’s execution is over.Static also provides a local access but the lifetime of the variable is till the execution of the program.

© Copyright PCTI Group 2009 | | <document classification>

In the general Declaration Section:

syntax :

Dim/Private/Public <varname> as <data type>

Variable declared either through dim or private statement are static variables and can be accessed by all procedures within the same form module.

Variables declared with public statement can be accessed by all modules within the project

© Copyright PCTI Group 2009 | | <document classification>

Control Structures

Decision Looping

If- Then-Else

Select Case

Do-Loop-While

Do While Loop

Do Until loop

For..Next

While..Wend

Control structure are the flow of control of a program.

© Copyright PCTI Group 2009 | | <document classification>

Syntax:

If condition is true Thenstatements for true alternative

[Elseif condition] then statements for true alternative [ Else]

statements for false alternativeEnd if

If Then Else

© Copyright PCTI Group 2009 | | <document classification>

Example:Letter Grade DeterminationDim Average as Integer, LetterGrade as stringAverage = CInt(txtAverage.text)If Average >= 90 then

LetterGrade = “A”ElseIf Average >= 80 then

LetterGrade = “B”ElseIf Average >= 70 then

LetterGrade = “C” ElseIf Average >= 60 then

LetterGrade = “D”Else

LetterGrade = “F”End IftxtLetter.Text = LetterGrade

© Copyright PCTI Group 2009 | | <document classification>

Select Case

Syntax:Select Case expressionCase Condition1 is true

First set of statementsCase Condition2 is true

Second set of statementsCase Condition3 is true

Third set of statementsCase Else

Last set of statementsEnd Select

© Copyright PCTI Group 2009 | | <document classification>

Input a Char Print it is Vowel Or Consonant

© Copyright PCTI Group 2009 | | <document classification>

Looping Structures:-

Looping structures are the statements that execute instructions repeatedly.Looping is also known as iteration or repetition.

1. Do While …………Loop

1. It repeats a set of statements until our condition is false. It

checks the condition at the top. Example.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

2. Do-Loop-whileThis loop also repeats a set of statements acc. To

condition. Do-Loop-While executes the loop at least once.

Example

© Copyright PCTI Group 2009 | | <document classification>

3. Do…Until Loop:- This Loop is same like Do-While Loop. But difference is that it repeats a set of statements until our condition is true. Syntax

Do until condition

statements

Loop

© Copyright PCTI Group 2009 | | <document classification>

4. Do… Loop Until:-This loop check the condition at the end of program. SyntaxDo

statementsLoop until (condition)

© Copyright PCTI Group 2009 | | <document classification>

5. While ……..Wend

This Loop is functionally equivalent to the Do While…Loop. Syntax

While Condition

statements

Wend

© Copyright PCTI Group 2009 | | <document classification>

Syntax:

For variable = start value to end value Step change valuestatements that compose body of loopNext variablewhere variable = the counter variable in loop

start value=Beginning value of the counter variable.end value= the ending value of the counter variablechange value = the amount the counter variable

changes each time through the loop.

For..Next

© Copyright PCTI Group 2009 | | <document classification>

Example of For-Next Loop

For Counter = 1 to 10 Step 1

Counter Variable

Beginning Value

Ending Change Value Value

Sum = Sum + Counter Body of Loop

Next Counter

For Statement

Next Statement

© Copyright PCTI Group 2009 | | <document classification>

6. For..Next

SyntaxFor <counter_var>=start_val to <end_val> step increment_valStatementsNext <counter_var>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Exercise

• Input any Ten Numbers, Print all the numbers in a List Box, print the sum in Text Box.

• Input any number, print their factorial value.

• Input any number, print their table.

© Copyright PCTI Group 2009 | | <document classification>

Numeric Functions

Val(string) or cint:- To convert character string to number

Int(number):-This Function rounds the number down to the integer less than or equal to its arguments.

CLng:-To convert number to long.

CDbl: To convert number to double.

CSgn: To convert number to single.

© Copyright PCTI Group 2009 | | <document classification>

String FunctionsLen: This function gives you the length of

the string.

Len(string)

Lcase: This function converts a string into lower case.

Lcase(string)

Ucase: This function converts a string into upper case. Ucase(string)

© Copyright PCTI Group 2009 | | <document classification>

Ltrim(string),Rtrim(string),Trim(string)

These functions remove the leading or trailing spaces from a string.

Str:-To converts numeric value to string values.

Left, Right:-These two functions are used to extract a certain number of characters from the leftmost or rightmost portion of a string.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Array

Array is collection of data elements of similar data type. Each element is called as sub-script.

Declaring Array

Dim arr(10) as integer

Dim 1 to 10 as integer

© Copyright PCTI Group 2009 | | <document classification>

Example of One Dimensional Array

© Copyright PCTI Group 2009 | | <document classification>

Example of Two Dimensional Array

© Copyright PCTI Group 2009 | | <document classification>

Dynamic Array

A dynamic array can be resized at any time. These are the most flexible and convenient features of VB. For example, you can use a large array for a short time and then free up memory to the system when you are no longer using the array.

© Copyright PCTI Group 2009 | | <document classification>

Dim book() as string

Redim books(99)

Contents of array is erased when redimensioning, you need to use Preserve keyword

Redim Preserve book(99)

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Control ArrayA control Array is a group of controls that share name and type. They also share the same event procedures. A control array has at least one element.

© Copyright PCTI Group 2009 | | <document classification>

Control ArraysA control array is a group of controls that share the same name and type. They also share the same event procedures. The Maximum index you can use a control array is 32767.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Procedure and FunctionA Procedure is a named unit of a group of program

statements that performs a well defined task.

Types of Procedures

1. Sub-Procedure

1. A Sub-procedure or sub-routine or sub is a procedure that performs a task but does not return a value.

1. General Procedure

2. Event Procedure

© Copyright PCTI Group 2009 | | <document classification>

Sub Procedure name

Statements

End Sub

You may call a Sub procedure in one of two ways:-

1. With a Call statement

2. Without a Call statement

© Copyright PCTI Group 2009 | | <document classification>

Private and Public Subs

Event procedure always start from Private Sub. Private means that code in other modules can’t call the procedure.A Public Sub can be called from anywhere in the project.

Calling the Procedure

© Copyright PCTI Group 2009 | | <document classification>

Procedure with parameters

© Copyright PCTI Group 2009 | | <document classification>

Function Procedure:-A function is a procedure that performs a specific

task and returns a value.

Function Procedure name(arguments)

Statements

End Function

© Copyright PCTI Group 2009 | | <document classification>

Function with Parameters

© Copyright PCTI Group 2009 | | <document classification>

Function with Parameters and with return value

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Creating Menu in VBMenus are a convenient and consistent way of grouping commands so that they become readily and easily accessible to users.

Menu Bar – This is the horizontal bar containing different menu options. Each menu option may have another submenu attached to it.

© Copyright PCTI Group 2009 | | <document classification>

Designing Menus

Click on Tools- Menu Editor command or

Right click on form choose Menu Editor or

press Ctrl + E Shortcut Key.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Assigning Access keys and Shortcut keys

Access Keys- allows the user to open a menu by pressing the ALT key and typing a designated letter.

For ex- Menu is File, We define the caption property of File is

&File F is Access Key

© Copyright PCTI Group 2009 | | <document classification>

Shortcut Keys-

directly activate a Menu item such as Ctrl + A, Ctrl + C

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Common Dialog Control

The common dialog control is a very powerful tool that provides you with a set of standard dialog boxes for opening, saving and printing files and for selecting colors and fonts. To include the common dialog control, Select Project > Components from the Menu Bar.

Select Microsoft Common Dialog Control 6.0

© Copyright PCTI Group 2009 | | <document classification>

Coding of Menu options

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Creating Popup

A Popup menu is a floating menu that is displayed over a form, independent of the menu bar.

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Accessing DatabasesA database is a collection of information related to a particular topic.The information is stored in tabular form.All the data organized in a database call table. The columns of table are referred as fields and the rows are referred to as records.

A database management is a system that stores and retrieves information from a database.Creating, modifying, deleting, adding data in files.

Visual basic allows you to create your own database in Microsoft Access. This facility is invoked by selecting Data Manager.

© Copyright PCTI Group 2009 | | <document classification>

Using Data Manager

The visual Basic can easily access the database creating using Microsoft Access.Databases created with Visual Basic can be manipulated with Microsoft Access.

To open a Data Manager to create Database

To open Data Manager window, select Data Manager from the Add-Ins menu

Select Visual Data Manager

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

Give the name of database. After that Right click on properties

Select New Table. A table structure displays on the screen

© Copyright PCTI Group 2009 | | <document classification>

Give the name of table file.

Click on Add field. Add the fields one by one.

Build the table

Double click on table to add the records into the table

© Copyright PCTI Group 2009 | | <document classification>

Data Access Mechanisms

In Visual Basic we can access databases through three different data access mechanisms or interfaces.

These are:

1. DAO- Data Access Objects

2. ADO- ActiveX Data Objects

3. RDO- Remote Data Objects

© Copyright PCTI Group 2009 | | <document classification>

Data Control

Data Control along with other controls are used to display, add, modify and delete data from a database.To make the connection between a from and a database, one places a data control in the from and set the properties to identify the name of the database of Access.

Data used to provide access to data in databases through bound controls on the form.

© Copyright PCTI Group 2009 | | <document classification>

Some important properties of Data control are:Database Name:-determine the path and filename of the data base file

Record Source. A set of records retrieved from table is determines by the Record Source property. This set of records is placed in an object called the Record set.

© Copyright PCTI Group 2009 | | <document classification>

Manipulating DataTo Move between records

MoveFirst:- Data1.Recordset.MoveFirst

MoveLast:-Data1.Recordset.MoveLast

MoveNext:-Data1.Recordset.MoveNext

MovePrevious:-Data1.Recordset.MovePrevious

To Add a record

Data1.Recordset.Addnew

To Update a record

Data1.Recordset.update

To delete a record

Data1.Recordset.delete

© Copyright PCTI Group 2009 | | <document classification>

ADO Data Control

This is latest version of data control, implemented as an ActiveX control. We can access the data through ADO Data Control.

© Copyright PCTI Group 2009 | | <document classification>

Module in VB

There are Three types of Modules

1. Form Module

2. Standard Module

3. Class Module

© Copyright PCTI Group 2009 | | <document classification>

1. Form Module- is a module that stores all the procedures and declarations of a single form The Form Modules are saved with extension .FRM

2. Standard Module- are module that stores general purpose code of the application. The standard module stores the procedures and declarations accessible from anywhere in the program. With extension .BAS

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>

© Copyright PCTI Group 2009 | | <document classification>