Visual Basic Notes-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Visual Basic

Visual Basic is a programming language and development environment created by


Microsoft in 1991. It is an extension of the BASIC programming language that
combines BASIC functions and commands with visual controls. Visual Basic provides
a graphical user interface that allows the developer to drag and drop objects into the
program as well as manually write program code. Visual Basics is an event driven
and object oriented programming language which helps us to create window
applications in easy way. Visual Basic also referred to as VB is designed to make
software development easy and efficient, while still being powerful enough to create
advanced programs. It is a third generation language.

Control Structures
Control Structures allow us to regulate the flow of our program’s execution. Using
control Structures we can write Visual Basic code that makes decisions or that
repeats actions. Other Control Structures let us guarantee disposal of a resource or
run a series of statements on the same object reference.

Decision Controls

If…….THEN: The IF ……THEN construct is probably the most common decision


making tool used by Visual Basic programmers. The basic syntax of this decision
structure is as follows: START

IF (Condition) THEN
Condition
Statement False

ENDIF True
Statement to be
executed

STOP

Essentially if the expression evaluates to true then the code in the body of the
statement is executed. If, on the other hand, the expression evaluates to false the
code is skipped.

Visual Basic
Page 1
IF…..THEN…….ELSE: In IF….THEN…..ELSE if the expression evaluates to true then
Statement associated with IF is executed. IF the expression evaluates to false then
the statement associated with ELSE part gets executed. So in this type of structure if
the condition is true then a specific task is performed and if the condition is false
then other task is performed.
START
The syntax is as:

IF (condition) THEN

Statement
Condition
ELSE

Statement Statement to be
Statement to be
executed
ENDIF executed

Stop

Select Case: The Select case structure is an alternative to IF…THEN…..ELSE. It is


used when there are several choices and we have to choose only one. The select case
works with a single test expression at the top of the structure. The result of the
expression is compared with the value of the case. If the result matches to a block,
the statement(s) associated with that case is executed. The syntax is as:
Select case (test expression)
Case 1
Statement 1
Case 2
Statement 2
Case 3
Statement 3
Case n
Statement n
End Select

Visual Basic
Page 2
Looping Structure: A loop is a special type of statement that is used to execute
a statement repeatedly. For example if we have to print Hello 10 times , the first way
is to write hello statement 10 times. Another way is to use a loop and to write print
statement only once. The loop will run 10 times and will print the message 10 times.

DO-While Loop: Do-While loop is used to execute a block of statements till a


condition is true. As soon as the condition becomes false, the statements written in
loop are not executed and the loop exits. In this loop first the condition is tested
then the statement gets executed. If the condition is initially false the statements are
not executed and the loop will not run even once. The syntax is as:
Do While (condition)
Statement(s)
Loop
e.g.
Dim a as integer
a=1
Do While(a<10)
Print a
a=a+1
Loop
This will print numbers from 1 to 10.
While Wend: WHILE WEND loop is used to execute a series of statements as long
as a given condition is true. In this type of loop if condition is true all statements are
executed until the WEND statement is encountered. Control then returns to the
While statement and condition is again checked. If condition is still true the process
is repeated, if it is not true execution resumes with the statement following the
WEND statement. The syntax is as:
While (condition)
Statement(s)
Wend
FOR….NEXT Loop: For Next loop is a special type of loop which is used when the
number of executions of a statements are known in advance. This loop uses a
variable called counter that increases or decreases the value each time the loop is
executed.

Visual Basic
Page 3
The syntax is as:
FOR Counter= Start To End { increment / Decrement}
Statement(s)
Next
e.g. For a=1 To 10
Print a
Next

Function: A function is a special type of procedure that returns a value every time
it is called. Visual Basic provides a rich collection of inbuilt functions. A user can also
define a function according to need those functions are called userdefined functions.
The main advantages of a function are reusability and modularity. Duplication of
code is reduced by using functions. We can call the function in two ways- call by
value and call by reference. In Call by value method a copy of the variable is passed
and original values are not modified. In call by reference a variable a variable itself
is passed and the original value is modified.

User Defined Functions: A function that does not exist but is created by a user to
perform some task is called user defined functions. A function must return a value. A
user can define a function either parameterized or without parameter.
Library Functions: Library functions are also called system functions or inbuilt
functions. These are the functions that have been defined by a programming
language. We can only use them but can’t change their definition. A library function
can be used many times in a program. Some functions are:

String: A data type consisting of a sequence of contiguous characters that represent


the characters themselves rather than their numeric values.

String Functions
Space(number) : it is a library function related to string which is used to put ‘n’
number of spaces where ‘n’ is a value of argument being passed. The number is an
argument to be passed to this function. It should be a positive number.
Chr ( ): The chr function returns the string that corresponds to an ASCII.
e.g. chr (65) will return capital A because ASCII value of A is 65

Visual Basic
Page 4
Str(number): This string related function is used to convert a numeric value into
string value. This function is important when we need to perform mathematical
operations.

Right(String, length): This string function is used to extract the right portion of a
phrase. The numbers of characters to be returned are mentioned as well.
Syntax: Right(Phrase,n)
Where n is the starting position from the right of the phrase where the portion of
the phrase is going to be extracted. E,g
Right(Visualbasic,4) = asic

Left (string, Length): This string related function is used to extract the leftmost
characters from a string. The numbers of characters to be returned are mentioned
as well. Syntax:
Left(phrase,n)
Where n is the starting position from the left of the phrase where the portion of the
phrase is going to be extracted. E,g
Left(Visualbasic,4) = Visu

Mid (string, Start, length): The Mid function extracts a substring from the
original phrase or string. It takes the following format:
Mid(Phrase,position,n)
Where position is the starting position of the phrase from which the extraction
process will start and n is the number of characters to be extracted. Example
Mid(“VisualBasic”,3,6) = ualBas

Instr(start, string1, srting2): This string returns an integer specifying the


starting position of the first occurrence of one string within another. This function is
used for searching purposes. This function returns the position of the string being
searched. Syntax
Instr(n,originalphrase,embedded phrase)
Example:
Instr(1,”VisualBasic”,”Basic”)= 7

Visual Basic
Page 5
Len(string): The length function returns an integer value which is the length of a
phrase or a sentence including the empty spaces. The syntax is :
Len (phrase)
Example:
Len(visualbasic)
It will return 11.

Ltrim(string): The Ltrim function trims the empty spaces of the left portion of the
phrase. Syntax
Ltrim(Phrase)
Example
Ltrim (“ VisualBasic”,4)=VisualBasic

Rtrim(string): The Rtrim function trims the empty spaces of the right portion of
the phrase. Syntax:
Rtrim (Phrase)
Example:
Rtrim (“ VisualBasic “ )= VisualBasic
Trim(string): This function is used to remove the spaces on both sides of the
string.
Ucase(string): This string function is used to convert a given string from
lowercase to uppercase.
e.g. Dim a as string
a=Ucase(apple)
now a = APPLE

Lcase(string): This string function is used to convert a given string from


uppercase to lowercase.
Lcase(APPLE)= apple
String(number, Character): This string function returns repeating character
string of the length specified.
e.g. Dim a as string
a=string(5,r)
now a= rrrrr.

Visual Basic
Page 6
Time Functions
Now( ): this is a time related function which is used to return the current systems
date and time . syntax:
Now( )

Time( ):this is a time related function which is used to return only current systems
time. Syntax: Time( )

Minute ( ):This is a time related function which is used to return current systems
minutes. Thus the value returned is always between 1 and 60.

Month ( ): this function is used to return the current month of the system.

Number Functions
Sgn(number): it is a numeric function which returns sign of a number passed to it.
If the number passed is a positive number the function returns 1, if the number
passed is a negative number the function returns -1. Syntax:

Sgn(number)
Val(string): This is a numeric related function which is used to convert a string
into a number.

Int( ): This is a numerical related function which is used to return the integer part
of a number. If the number is negative the function returns a number less than or
equal to the number.
Example:
Int(2.9) It will return 2
Int(-8.4) it will return -9

Abs( ): This is a numeric function which is used to return the absolute value of
numeric expression .i.e a number without sign(+ or -) e.g Abs(-8) =8

Visual Basic
Page 7
Fix( ):this is a numeric related function which is used to return the integer part of a
number. Even though if the number is negative the function returns a number
greater than or equal to a number. Int(-8.4) . it will return -8

Sqr ( ): This function is used to return the square root of a number passed to it.
Example:
Sqr(9)
It will return 3

Round (n, m ): Round is the function that rounds up a number to a certain number
of decimal places. The format is round(n, m) which means to round a number n to m
decimal places. E.g. round(7.2567,2) = 7.26

Trunc( ): this function returns integer part of a specified decimal or double


number

Pow( ): Return the argument raised to the specified power

MsgBox( )
This function is used to display a message in a dialog box. It waits for a user to click
a button and returns a value indicating which button the user clicked. The returned
value is stored in a variable for further use. Syntax:
Msgbox(prompt[,buttons ][,title][,helpfile, contect])

Prompt: A string expression displayed as the message in the dialog box. The
maximum length is about 1024 characters
Buttons: used to specify the number and type of buttons to display.
Title: the string displayed on the title bar of the message box
Help file: a string expression that identifies the help to be used for the dialog box.
Context: numeric expression that identifies the help file number.

Visual Basic
Page 8
Input box function
This function is is used to collect information from the user and returns a string
containing the contents of the text box which can be collected in a variable or some
other container. Syntax:
Inputbox(prompt[,title[,default[,xpos[,ypos][,help file,context])
Prompt: A string expression displayed as the message in the dialog box.
Title: String expression displayed in the title bar of the dialog box.
Default: a string expression displayed in the in the text box as the default response
if no other input is provided.
Xpos: the distance in pixels of the left edge of the dialog box from the left edgeof the
screen.
Ypos: The distance in pixels of the upper edge of the dialog box from the top of the
screen.
Help file: a string expression that identifies the help to be used for the dialog box.
Context: numeric expression that identifies the help file number.

Event Driven Programming: In computer programming event driven


programming is a programming paradigm in which the flow of the program is
determined by events such as user actions (mouse clicks, key presses) or messages
from other programs. When we click, press a key , move the mouse or fire other
events the particular block of code of the corresponding event is executed and then
the program behaves in a certain way. When we fire an event the code in the event
procedure is executed and the Visual Basic performs its operations as per the
instructions written in the event procedure code.

Form: A form is an object which acts as a container for controls. It can hold one or
more controls like buttons, check boxes etc. A form is the basic work area in Visual
Basics which acts as a background for other objects placed on it to form user
interface. It acts as a visual interface for an application.

Visual Basic
Page 9
Types of Forms:
1. Single Document Interface
2. Multiple Document Interface
Single Document Interface: A single document Interface or SDI is an application
primarily made of a form equipped with a menu. SDI is stand alone ordinary
window or form that exists independently of each other. Notepad is the example of
Single document interface. SDI application allow only one open document frame
window at a time. It is made up of one or more independent windows which
appears separately on the window desktop. Each window of the application holds a
single document. If the user wants to open more documents with that application
then he must open a new window. SDI applications tend to be more robust and bug
free than MDI applications.
Multiple Document Interface: The Multiple Document interface (MDI) was
designed to simplify the exchange of information among documents all under the same
roof. With the main application we can maintain multiple open windows but not the
multiple copies of application. Data exchange is easier when we can view and compare
many documents simultaneously. The main form or MDI form is not duplicated but it
acts as a container for all the windows and it is called parent window. The windows in
which the individual documents are displayed are called child windows. MDI application
must have at least two forms, the parent form and one or more child forms. Each of these
forms has certain properties. There can be many child forms contained within the parent
form but there can be only one parent form. To create an MDI application, we must
follow these steps:

1. Start a new project and then choose Project >>> Add MDI Form to add the parent
Form.

2. Set the Form's caption to MDI Window

3. Choose Project >>> Add Form to add a SDI Form.

4. Make this Form as child of MDI Form by setting the MDI Child property of the SDI
Form to True. Set the caption property to MDI Child window.

Visual Basic automatically associates this new Form with the parent Form. This child
Form can't exist outside the parent Form, it can only be opened within the parent Form.

Visual Basic
Page 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy