Unit1 VB
Unit1 VB
VB
Visual Basic ?
• Visual Basic is a third-generation event-driven
programming language first released by Microsoft in
1991.
• It evolved from the earlier DOS version called
BASIC. BASIC means Beginners' All-
purpose Symbolic Instruction Code.
• Visual Basic is a user-friendly programming language
designed for beginners, and it enables anyone to
develop GUI window applications easily.
• Microsoft has released many versions of Visual
Basic, from Visual Basic 1.0 to the version Visual
Basic 6.0.
Visual Basic Applications
• Geometric Progression, Quadratic Equation
Solver, Simultaneous Equation Solver ,Prime Number
Tester, Factors Finder, Quadratic Function Graph
Plotter.
• inventory management system , investments
calculator, point-of-sale system, payroll system,
accounting program and more to help manage your
business and increase productivity.
• Games : slot machine, reversi, tic tac toe .
• multimedia programs : Smart Audio
Player, Multimedia Player
Object-Oriented Concepts (VB2008 vs VB6)
• Visual Basic 2008 is very much similar to
Visual Basic 6 in terms of syntaxes and
program structure, but their underlying
concepts are very different.
• The main difference is that VB2008 is an
object-oriented Programming Language under
the .NET framework.
• Although VB6 may have some OOP
capabilities, Visual Basic 6 is not an object-
oriented programming language because it
does not have inheritance capabilities.
• On the other hand, VB2008 is an Object Oriented
Programming Language, just like other OOP such as
C++ and Java.
• VB6 is known as a procedural or functional
programming language. Some other procedural
programming languages are C, Pascal, and
FORTRAN.
• An object-oriented programming language has few
core features
– Encapsulation
– Inheritance
– Polymorphism
– Class
Difference
• Application Development : Offline (VB)
• Web Development : Online (HTML )
Starting With VB?
• Start -> Programs -> Microsoft Visual Basic 6.0.
Components of VB
Visual Basic IDE is made up of a number of
components
1. Menu Bar & Tool Bar
2. Project Explorer
3. Properties window
4. Form Layout Window
5. Toolbox
6. Form Designer
7. Object Browser
Menu Bar
• This Menu Bar displays the commands that are
required to build an application.
• The main menu items have sub menu items that can
be chosen when needed.
• The toolbars 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.
Toolbox
• The Toolbox contains a set
of controls that are used to
place on a Form at design
time.
• Additional controls can be
included in the toolbox by
using the Components
menu item on the Project
menu.
• Pointer : Provides a way to move and resize the controls form
• PictureBox: Displays icons/bitmaps and metafiles. It displays text
or acts as a visual container for other controls.
• TextBox: Used to display message and enter text.
• CommandButton: Used to carry out the specified action when the
user chooses it.
• CheckBox: Multiple selection.
• OptionButton: Option Button control which is a part of an option
group allows the user to select only one option even it displays
multiple choices.
• ListBox: Displays a list of items from which a user can select one.
• ComboBox: Contains a TextBox and a ListBox. This allows the
user to select an item from the dropdown ListBox, or to type in a
selection in the TextBox.
• HScrollBar and VScrollBar: These controls allow the user to
select a value within the specified range of values.
• Shape: Used to add shape (rectangle, square or circle) to a Form.
• Line: Used to draw straight line to the Form
Properties Window
• The Properties Window is docked
under the Project Explorer window.
• The Properties Window exposes the
various characteristics of selected
objects.
• Each and every form in an
application is considered an object.
• Each object in Visual Basic has
characteristics such as color and
size.
• All these characteristics of an object
are called its properties.
• Thus, a form has properties and any
controls placed on it will have
properties too.
• All of these properties are displayed
in the Properties Window.
Form Designer Window
Form Layout Window
• Form Layout window is a simple
visual basic design tool displays
thumbnail view of the current
form.
• This helps in controlling the form
position in the Windows
environment. When more number
of forms are there in a particular
program.
• This “Form Layout” window is
helpful to arrange all the forms
onscreen, in exactly the way in
which the user needs them.
• By simply dragging the miniature
form to a particular location, the
position of a form can be set in
the “Form Layout” window.
Project Explorer
• Docked on the right side of the screen, just under the toolbar, is the Project
Explorer window.
• The Project Explorer serves as a quick reference to the various elements of
a project namely form, classes and modules.
• All of the object that make up the application are packed in a project.
• A simple project will typically contain one form .It is possible to develop
any number of forms for use in a program, although a program may consist
of a single form
Object Browser
• The Object Browser allows us to browse
through the various properties, events and
methods.
• It is accessed by selecting Object Browser
from the View menu or pressing the key F2.
• View→ Object Browser (F2)
IDE
• All these components of the initial VB screen
makes the IDE (Integrated Development
Environment)
• Initially it was SDI (Single Document
Interface) later it was designed as MDI
(multiple Document Interface)
• SDI- One application/window opened
• MDI –Many application/Window opened.
Properties,Methods and Events
• All the controls except pointer are objects in
VB.
• Real-world objects –Properties (Ex: Flower-
color,Shape,fragrance)
• Properties –named attribute of a programming
object and it defines the characteristics of an
object.
• Textbox : enabled/disabled , Font, multiline,
text,visible,width etc..
Methods
• An action that can be performed on objects.
• In OOP, a method that is connected or built-in
procedure to invoke any action on object.
• Ex: text1.setfocus
text1.refresh
Events ?
• An event is a signal that informs an application
that something important has occurred.
• when a user clicks on a form, the form can
raise a Click event and call a procedure that
handles the event.
Event Handlers
• Event handlers are procedures that are called
when a corresponding event occurs.
• We can use any valid subroutine as an event
handler.
Ex :Mouse Events
• Visual Basic responds to various mouse events,
which are recognized by most of the controls.
• The main events are MouseDown,
MouseUp and MouseMove.
• MouseDown occurs when the user presses any
mouse button
• MouseUp occurs when the user releases any
mouse button.
• These events use the arguments button, Shift, X,
Y and they contain information about the mouse's
condition when the button is clicked.
• Textbox
– change Event
– Mouse Move Event
– Click Event
Working With forms
• Form –Basic object.
• Form window- code and objects that creates
user interface.
• Covers Entire screen or many forms contained
in a single screen.
• Default –Form1
• Change the form name using caption property.
• Every form has its own properties,methods and
events.
Methods
• Show
• Load ,unload
• Hide ,unhide
Forms Runtime and design time
properties
Syntax : object .property Name = Value
• Explicit Declaration
• Implicit Declaration
The variables are declared with a Dim statement with variable name and its
/* coding here*/
End Sub
SCOPE of a Variable
Global Level
Dim X as Integer
(visible to the whole form )
Procedure level
Dim y as integer
(Visible only within Procedure
Ex :
If (age>18)then
Msgbox “ eligible to vote
Else
Msgbox” Not Eligible to vote”
Endif
Flow Chart & Syntax for IF then elseif ….(Many)
Syntax & Example : IF elseif..Endif
• Syntax : • Example
If user= “Prabhu” Then
If condition Then msgbox “hi, Prabhu”
statement ElseIf user= “Priya” Then
ElseIf condition Then msgbox “hi, Priya”
other statement ElseIf user= “Ram” Then
ElseIf condition Then msgbox “hi, Ram”
other statement Else
Else msgbox “hi Gita”
default statement End If
End If
Nested If-Else Statement
• In nested If-Else statement, the defined
If condition returns true then it will enter into
the body of condition and perform
another if…else condition checking based on
our requirements.
NESTED IF –FLOW CHART
Syntax & Example
If x > y Then
If condition Then
If x >= 10 Then
If nested_condition_1 Then
msgbox("x value greater than or equal to 10")
// Statements to Execute
Else
Else
msgbox ("x value less than 10")
// Statements to Execute
End IF
End If
Else
Else
If y <= 20 Then
If nested_condition_2 Then
msgbox ("y value less than or equal to 20")
// Statements to Execute
Else
Else
msgbox("y value greater than 20")
// Statements to Execute
End If
End If
End If
End If
msgbox("Press Enter Key to Exit..")
Select Case Statement
• Another way to control the program flow, that
is, the Select Case control structure.
• The Select Case control structure is slightly
different from the If....ElseIf control structure.
• The difference is that the Select Case control
structure can handle conditions in an easier
manner than the If...Then...ElseIf control
structure.
Example for Select Case
Syntax : Select Case
Select Case expression
Case value1
statements-1
Case value2
statements-2
Case Else
Default Statement
End Select
Do While
Do While condition
' Statements till true
Loop
For ..Next Loops
• SYNTAX :
For counterVariable = fromValue To toValue
VB Statements...
Next counterVariable
Example :
Dim i as integer
For i = 1 To 10
Print “Hello”
Next i
Examples : For …. Next
Dim x As Integer Dim x As Integer
For x = 1 To 50 For x = 1 To 50 Step 2
Print x Print x
Next Next
Output : ? Output: ?
x Print X Next x Print X step 2
Do Loops
• Some Do...Loops first executes the statements and then
test the condition after each execution.
• Repeats a block of statements while
a Boolean condition is True or until the condition
becomes False.
• Can test condition only one time, at either the start or
the end of the loop.
• If you test condition at the start of the loop (in
the Do statement), the loop might not run even one
time.
• If you test at the end of the loop (in
the Loop statement), the loop always runs at least one
time.
Forms of Do loop
Do
Do While <condition >
statement-block
statement-block
Loop
Loop
While <condition >
Do
statement-block Do Until <condition>
Loop statement-block
Until <condition> Loop
Do Loop
N=10 n=n+10 Loop
Example : for Do Until Loops
Dim num As integer
num=0
Do Until num > 10
number = number + 1
Print number
Loop
Num Number= number +1 Print number
Example for While … Wend Loops..
With Text1
.Font.Size = 14
.Font.Bold = True
.ForeColor = vbRed
.Height = 230
.Text = "Hello World"
End With
Arrays
✓ An array is a consecutive group of memory locations
that all have the same name and the same type.
✓ To refer to a particular location or element in the array,
we specify the array name and the array element
position number.
✓ Individual elements of an array are identified using an
index.
✓ Arrays have upper and lower bounds and the elements
have to lie within those bounds.
✓ Each index number in an array is allocated individual
memory space and therefore users must avoid
declaring arrays of larger size than required.
Declaring arrays
➢ Arrays occupy space in memory.
➢ The programmer specifies the array type and the number of
elements required by the array so that the compiler may
reserve the appropriate amount of memory.
➢ There are two types of arrays in Visual Basic namely:
➢Fixed-size array : The size of array always remains the
same-size doesn't change during the program execution.
➢Dynamic array : The size of the array can be changed
at the run time- size changes during the program
execution
Fixed-sized Arrays
➢ When an upper bound is specified in the
declaration, a Fixed-array is created.
➢ The upper limit should always be within the
range of long data type.
Declaring a fixed-array
below.
Boolean Cbool Integer Cint
•
Byte Cbyte Long CLng
Currency Ccur Single CSng
Date Cdate String CStr
Decimals Cdec Variant Cvar
Double CDbl Error CVErr
Visual Basic Operators
• Arithmetic Operators
• Assignment Operators
• Logical/Bitwise Operators
• Comparison Operators
• Concatenation Operators
Arithmetic Operators
Operator Description Example (a = 6, b = 3)
+ It will add two operands. a+b=9
- It will subtract two operands. a-b=3
* It will multiply two operands. a * b = 18
/ It divides two numbers and a/b=2
returns a floating-point result.
\ It divides two numbers and a\b=2
returns an integer result.
Mod It divides two numbers and a Mod b = 0
returns only the remainder.
^ It raises a number to the power of a ^ b = 216
another number.
Assignment Operators
Operator Description Example (a=20)
= It will assign a value to a a=3
variable or property.
+= It will perform the addition of a += 10 equals to a =
left and right operands and a + 10
assign a result to the left
operand.
-= It will perform a subtraction of a - = 10 equals to a =
left and right operands and a - 10
assign a result to the left
operand.
*= It will perform a multiplication a *= 10 equals to a =
of left and right operands and a * 10
assign a result to the left
operand.
Assignment Operators
Operator Description Example (a=20)
/= It will perform a division of a /= 10 equals to a =
left and right operands and a / 10
assign the floating-point result
to the left operand.
\= It will perform a division of a \= 10 equals to a =
left and right operands and a \ 10
assign the integer result to the
left operand.
^= It will raise the value of a a ^= 10 equals to a =
variable to the power of a ^ 10
expression and assigns the
result back to the variable.
Logical / Bitwise Operators
<> It will return true if both operands are not a < > b = True
equal.
Concatenation Operators
Right (“string”, n) Returns number of characters from the Right(“Visual Basic”, 4) = asic
right side of a string.
trim(“string”) Removes both leading and trailing Trim$(" Visual Basic ")=
blank spaces from a string. Visual Basic
Mid(string, position, n) Mid function extracts a Mid(“Visual Basic”, 4, 6) =
substring from the original ual Bas
phrase or string.
Instr(n, original, embedded) Specifies location of substring Instr(1, “Visual Basic”,”
from the original string . Basic”)=8
• Another Example:
Value Named Constant
16 vbCritical
3 vbQuestion
48 vbExclamation
64 vbInformation
Example for Inputbox()
• userMsg = InputBox(“Enter Student’s Date of Birth”)