The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by sanjeev.khattri.dubai, 2019-05-05 05:55:53

VISUAL BASIC

MIDDLE SCHOOL

Visual Basic is a tool that allows you to develop Windows (Graphic User Interface- GUI) applications. Visual
basic is event driven, meaning code remains idle until called upon to respond to some events (button
pressing menu selection). Visual Basic is governed by event processor. Nothing happens until the event is
detected. Once the event is detected the code corresponding to that event is executed.
Visual Basic 6.0 versus Other Versions of Visual Basic
· The original Visual Basic for DOS and Visual Basic for Windows were introduced in 1991.
· Visual Basic 3.0 (a vast improvement over previous versions) was released in 1993.
· Visual Basic 4.0 released in late 1995 (added 32-bit application support).
· Visual Basic 5.0 released in late 1996. New environment, supported creation of ActiveX controls, deleted
16-bit application support.
· And, now Visual Basic 6.0 - some identified new features of Visual Basic 6.0:
Þ Faster compiler
Þ New ActiveX data control object
Þ Allows database integration with wide variety of applications
Þ New data report designer
Þ New Package & Deployment Wizard
Þ Additional internet capabilities
Steps in Developing Application

• There are three primary steps involved in building a Visual Basic application:

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

We’ll look at each step.

Drawing the User Interface and Setting Properties

• 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

We focus here on the design mode.

• Six windows appear when you start Visual Basic.

⇒ The Main Window consists of the title bar, menu bar, and toolbar. The title bar indicates
the project name, the current Visual Basic operating mode, and the current form. The
menu bar has drop-down menus from which you control the operation of the Visual Basic
environment. The toolbar has buttons that provide shortcuts to some of the menu options.
The main window also shows the location of the current form relative to the upper left
corner of the screen (measured in twips) and the width and length of the current form.

⇒ The Form Window is central to developing Visual Basic applications. It is where you draw
your application.

⇒ The Toolbox is the selection menu for controls used in your application.

Pointer Picture Box Label Text Box
Frame Command Button
Check Box Option Button
Combo Box List Box Vertical Scroll Bar

Horizontal Scroll Bar
Timer Drive List Box
Directory List Box File List Box
Shapes Lines
Image Box Data Tool

Object Linking Embedding

⇒ The Properties Window is used to establish initial property values for objects. The drop-
down box at the top of the window lists all objects in the current form. Two views are
available: Alphabetic and Categorized. Under this box are the available properties for the
currently selected object.

⇒ The Form Layout Window shows where (upon program execution) your form will be
displayed relative to your monitor’s screen:

⇒ The Project Window displays a list of all forms and modules making up your application. You
can also obtain a view of the Form or Code windows (window containing the actual Basic
coding) from the Project window.

• As mentioned, the user interface is ‘drawn’ in the form window. There are two ways to place controls
on a form:

1. Double-click the tool in the toolbox and it is created with a default size on the form. You can
then move it or resize it.

2. Click the tool in the toolbox, then move the mouse pointer to the form window. The cursor
changes to a crosshair. Place the crosshair at the upper left corner of where you want the
control to be, press the left mouse button and hold it down while dragging the cursor toward
the lower right corner. When you release the mouse button, the control is drawn.

• To move a control you have drawn, click the object in the form window and drag it to the new
location. Release the mouse button.

• To resize a control, click the object so that it is select and sizing handles appear. Use these handles
to resize the object.

Click here to Use sizing
move object handles to
resize object

Example:
Design an application in visual basic which will input the name and your age and prints it.
Step 1:

Start Visual Basic.

Select standard exe and click at “open”

2. You will get the following screen

Step 2: Now you have to design the application on this “form”
You need a input Box to take name and age from user and a list box to show the answer.
*textbox is used for input & listbox are used for output.
so here we need two text boxes (one for name and another for age)
so let’s draw it. Click on the text box tool.

Draw two text boxes one for name and one for age.

The text boxes have the word “text1” and “text2” printed inside it… we need to clear it by changing the
text property of textboxes to space.
This can be done by clicking on the textbox. (once we click on text box, the property table for it appears
on right side)

IN THE PEROPERT TABLE YOU WILL SCROLL DOWN TO “TEXT” PROPERTY

AND DELETE THE CONTENT (Text1)

Repeat same for Text box 2
Now you need to put a message along with these text boxes which will tell the user that “what he needs
to enter here” example for Text1 message should be “Enter name “ for text2 “Enter age”
Messages are created using Label tool

So you need to select the Label tool and place them above both boxes

Click on Label1 and from property table change the Caption property to “Enter name” do same for Label
2 and change its caption property to “Enter age “ .

Same way you will change caption of Label2
Now you need Listbox to show answer

We will draw a list box on form for output:
Now you will need a button to execute this application , you can use “command” button
And change the caption of command button to “RUN”

Now we need to make this “Run” Button execute so wee need to code it. For coding the “RUN” button
double click on “RUN” BUTTON
A code window will open.

Now we should learn about coding commands
Visual basic has three main types of data types
Alphabets, symbols are stored in “String variables”
Non decimal numbers are stored in “integer variables”
Decimal numbers are stored in “Double variables”
So as we need “Name” and “age” here so we will declare two variables in the coding windows as
Dim name as String // Dim is command to declare a variable
Dim age as double //

Now the user when runs the program will type the “name” in Text box 1 and “age” in “Text box 2”
So we need to add the following line to get the name from text1
name=text1
As age is a integer variable and textbox contains text, so both of them of are not same types so before
storing it into “age” variable we need to convert it to number by using val() function as given below.
age=val(text2)

so no both of these two lines need to be typed in code window.

Finally we have to show the “name” and “age” on listbox 1 as
name “your are “ age “years old”
now here since we have variables and messaged “ “
so we need to join them using joining operator “&”
so our message becomes
name & “your are “& age & “years old”
and we need to put this sentence in listbox 1 so it should go like
list1.addItem(name & “your are “& age & “years old”)
this is the way a message is shown on listbox
list1.additem(“ any message “)
let’s put this in coding window

Now we should save and run this program by clicking at “save” and “start” buttons

Once we start and fill in the “name” and “age in text boxes we need to click at “run” button to see output.
So good job! You made your first Application in Visual Basic.

GETTING STARTED ON LAPTOP: VIDEO LINK

PART1 https://drive.google.com/open?id=1UUMyIN02_hOchp_scpavvZ0MYkfpGuWB
PART https://drive.google.com/file/d/1_nAyf3QY4Fn5xwY9ptPhu2wNPa2Q30Ii/view?usp=sharing
2
PART3 https://drive.google.com/file/d/1DlLqvjb70N7twieXBFLZDigc8vbsbqT8/view?usp=sharing

WORKSHEET 1.1

#1.1.1 Write a visual basic application which will input name and basic salary
and calculates hra=10% of basic salary, da=15% of basic salary, total
salary= basic+hra+da, It will print the messages as

#1.1.2. Write a visual basic application which will input “name” and “marks” in
5 subjects. The application should print “total”, “percentage” of student.

#1.1.3. Write a visual basic application which will input scores of a batsman in 5
matches and print their “average”

WORKSHEET 1.2

Q1 Write an application in Visual Basic which will input Employee name, basic
salary and calculate
Hra= 30/100*salary [house rent allowance]
Da=40/100*salary [ daily allowance]
Net_salary=basic+hra+da
It should print
“The salary of “ __emplyee name ___ “ is “ ____net salary ___

Q2. Write a Program which will input a number and check if number is divisible
by 21 or number is not divisible by 21.
The program should print message like
Number _ “is divisible by 19”

or
number _ “is not divisible by 19”

# SESSION 2

IF-ELSE & CONDITIONAL OPERATORS

To check condition in visual basic we use if-else statements.

IF...THEN...ELSE STATEMENT

An If statement can be followed by an optional Else statement, which executes when the Boolean expression is false.

Syntax:

The syntax of an If...Then... Else statement in VB.Net is as follows:
If(condition )Then

'statement(s) will execute if the condition is true
Else
'statement(s) will execute if the condition is false
End If

If-else is used to check if a condition is true or false according to result the application moves ahead.
For example, we need to see that the number entered by user is positive or negative so our
If statement should look like this
if(no>0) then

List1.Additem(no& is positive “)
else

List1.Additem(no&” is negative “)
End if

To create a visual basic application for it, we will design a form which will

1. one label box stating the message “Enter a number “

2. one text box to take a number

3. One listbox to show answer
4. One command button to execute the code, we will name it as CHECK

5. We will double click at CHECK button and in the code window we will type all commands
together.
Dim no as Integer
no=val(text1)
if no>0 then
List1.Additem( no & “ is positive number “)
Else
List1.Additem( no & “ is negative number “)
End if

6. We will save and run the application and type the input number as 27.

7. And when we will click at CHECK, Output message will be as shown

8 we will try again for number -10
8. When we press CHECK button output will be

i) RELATIONAL OPERATORS
These operators are used to check relation between two operands. They are of following types
a) > greater than example if(a>b) then
b) < less than example if(a<b) then
c) = equal to example if(a=b) then
d) <> not equal to if(a<>b) then

WORKSHEET 2.1

Question 2.1.1: Write a Program which will input name and age of a person and check if the
person is adult or
Minor.

INPUT: Enter name
Akshat

Enter age
14

OUTPUT : Akshat is Minor

Question 2.1.2: Write a program which will input a number and check if the number is even
or odd.
INPUT : Enter a number

18
OUTPUT : 18 is even number

Question 2.1.3: Write a program which will input a number and check if the number is positive
or negative
INPUT : Enter a number

-5
OUTPUT : -5 is negative

# SESSION 3

LOGICAL OPERATORS

Logical operators allow you to evaluate one or more expressions and return a Boolean value
(True or False).

AND

It returns True if and only if both expression1 and expression2 evaluate to True.
If either expression is False, then the result is False

For example:

If x=5 and x<7 then
List1.Additem(“ The number equals 5 and is less than 7 “)

Else
List1.Additem(“ The number does not equals 5 and is not less than 7 “)

Endif

In this case, the code after the If statement will be executed only if the value of x is five
and the value of y is less than seven
Input x=4
Output :

The number does not equals 5 and is not less than 7
Input x=5

The number equals 5 and is less than 7

OR

It returns True if any of both expression1 and expression2 evaluate to True. If
both expression is False, then the result is False
For example:

If x<5 or x>10 then
List1.Additem(“ The number is less than 5 or more than 7 “)

Else
List1.Additem(“ The number is either not less than 5 or not more than 7“)

Endif

NOT

It returns True if expression is false .
For example:

If Not no > 5 Then
List1.AddItem (" " & no & " is less than 5 ")

Else
List1.AddItem (" " & no & " is more than 5 ")
End If

INPUT : X=10
10 is less than 5

INPUT : X=15
15 is MORE than 5

WORKSHEET 3.1

#3.1.1 Write a visual basic application which will input the electricity “units” consumed by the customer

and calculate and print “bill” on following condition.

UNITS CONSUMED RATES

0 – 300 0

301 – 500 0.25 PER UNIT

501 – 700 0.50 PER UNIT

701 and above 0.75 PER UNIT

#3.1.2 Write a visual basic application which will input the “percentage” marks of the student and

calculates the “grades” based on following conditions.

PERCENTAGE GRADES

81 – 100 A

61 – 80 B

41 – 60 C

0 – 40 D

#3.1.3 Write a visual basic application which will input the “sale” amount and calculates “discount” and

“discounted price” on the following conditions

SALE AMOUNT DISCOUNT RATE

1000 and above 30% of sale amount

501 – 999 25% of sale amount

301 – 500 15% of sale amount

0 – 300 0

WORKSHEET 3.2

WORKSHEET 3.3
Q1: In a Hypermarket one offer says “ No vat charges for shopping above 1000 “. Write a VB app. Which
will ask for customer name, no of items purchased and price of each item and calculates.
Amount= no_of_items x price
Net Amount= Amount -discount Discount is
given as shown below:

Amount Discount

>2000 10% of amount

>=1000 and <2000 5%of amount

>=500 and <1000 3% of amount

<500 0%
The output should be:

“The net amount for “ ___name___ “ is “___ net_amount

Q2. In a car parking place the charges are as per given table

Hours parked Charges

10

>1 and <=5 2.5 per hour

>5 A flat amount of 10 aed
“The charge of parking vehicle no “ __vehicle no ___ “ is “ __charges ___

Q3. Write an application in Visual Basic which will input, Employee name and Monthly salary and
calculate : Yearly_ salary=12*monthly_salary and income tax as :

Yearly salary Income tax

>=10,00000 10% of (yearly salary – 1000000)

>=500000 5% of (yearly salary – 1000000)

<500000 0

It shoud print “ Income tax of “___employee_name___ “ is “__income_tax

WORKSHEET 3.4

WORKSHEET 3.5

Q1 A three digit number is formed from its digit using this formula
Number=digit1 x 100 + digit2 x 10 + digit3 x 1
Input 3 digits of a number and print the number formed by the digits.
Example:
Input:
Enter 1st digit : 1
Enter 2nd digit : 5
Enter 3rd digit : 3
Output: 153
(which is calculated as 1 x 100 + 5 x 10 + 3 x 10
100 + 50 + 3
153)

Q2. A special numbers are two digits numbers when you add sum of
digits to the product of digits it equals the number itself.
Input 2 digits of a number and check if it is a special number
Example:
Input :
Enter 1st digit : 5
Enter 2nd digit: 9
Output: Sum of digits : 14
Product of digits : 45
It is a special number
(sum of digits + product of digits
14 + 45 = 59(which is original number)

Q3 Take two number and check whether the sum is greater
than 5, less than 5 or equal to 5.

Q4 Input three numbers and print in ascending order
Example x=4,y=6,z=1
146

Q5 Input 4 numbers and check whether the sum of 1st two numbers is
greater/less
than the last two numbers.
Example:
Input : Enter four numbers: 3,4,7,2
Output: Sum of 1st two numbers < sum of last two numbers
Input 3 numbers and print them in descending order
Example
Input: 4,2,8
Output” 8,4,2

Q6 Input 4 numbers and check if the sum of squares of 1st two numbers is
equal to sum of squares of last two numbers
Example:
Input: 2,4,6,3
Output: Sum of squares of 2 and 4 is not equal to sum and square of 6
and 3

Q7 Input 5 numbers and check if the difference of 1st two is less then the
middle number
Example:
Input 21,3,14,5,6
Difference of 21 and 3 is > middle number 14

Q8 Input 5 numbers and calculate their average, also check if the average is more
than 100 or not.
Example:

Input: 10,34,65,12,34
Output: Average is 31
Output: Average is <100
Q9 Input 5 numbers and check if the middle number is between the range of 1st
and last number.
Example:
Input 21,3,14,5,6
Output: Middle number 14 is out of range of 21 and 6

Q10 Input three digits of a number and check if it is Armstrong number
Armstrong numbers are those numbers where sum of cube of digits equals
the number itself.
Input: 1,5,3

Output: Armstrong number
As 1*1*1+5*5*5+3*3*3

= 1 +125 +27
=153

TEST YOUR SELF

# SESSION 4

LOOPS :

Loops are used for repeating statements in visual basic. There are 3 types of loops.

(i) FOR-NEXT Loop
(ii) While – Wend Loop

The syntax of the for next loop is:
For i=1 to 5

List1.Additem(“ “+i)
Next i
The output of this loop will be
1
2
3
4
5

In the above example we have printed the loop variable “I” which automatically increases
by 1 starting from initial value of loop “i=1” and goes till the ending value “i=5”

If we need to print the numbers with a difference of say “2” then we can use the for loop
as
The syntax of the for next loop is:
For i=1 to 10 step 2

List1.Additem(“ “+i)
Next i
The output of this loop will be
1
3
5
7
9

Summing series in Visual Basic.

Series is a set of numbers with same increment/decrement up to “n” terms.
Lets take an example
Sum the series:

i) 1,3,5,7,9………………………n
Dim n,t,s as integer
n=val(text1)
t=1 // initialize the 1st term of series to “t”
for i=1 to n // loop will go for number of terms in series.

s=s+t // adding the term to “s” sum variable
t=t+2 // the next term is formed by increasing the current term by 2
next
List1.Additem(“ Sum is “ & s)
The same question can be done by while –wend loop as
Dim n,t,s as integer
n=val(text1)
t=1 // initialize the 1st term of series to “t”
while i<n // loop will go for number of terms in series.
s=s+t // adding the term to “s” sum variable
t=t+2 // the next term is formed by increasing the current term by 2
i=i+1 // loop variable is to be incremented unlike in for loop
wend
List1.Additem(“ Sum is “ & s)

WORKSHEET 4.1

WORKSHEET 4.2

SOLVING FRACTIONAL SERIES

The fractional series can be solved by using two different variables for denominator and numerator.

Example

Sum the series

S=1/2+2/4+3/8………………n

Solution:

Dim numo,deno, s as double

Dim n as integer

n=val(text1)

numo=1

deno=2

for i=1 to n

s=s+numo/deno

numo=numo+1 // numerator increases by 1

deno=deno*2 // denominator gets multiplied by 2

next

List1.Additem(“ Sum of series is “ & s)

Example:

Sum the series:
S= 1/1 + 2/3+3/5………………..n
Dim nemo,deno,s as double
Dim n as integer
N=val(text1)
nemo=1
deno-1
for i=1 to n

s=s+nemo/deno
nemo=nemo+1
deno=deno+2
next i
List1.Additem(“Sum of series is “ & s)

SPECIAL FRACTIONAL SERIES

There are certain cases when sum and cube of natural numbers are involved we need to be careful
with initializing the 1st value of nemo and deno.

The (natural number series) is
1,2,3,4,5……………………………….n
(Square of natural number series) is
1,4,9,16,25………………………………n (12+22+32+42+52……………………n)
(Square of natural number +1 series)
2,5,10,17,26………………………………n ((12+1)+(22+1)+(32+1)+(42+1)+(52+1)……………………n)
(Square of natural number - 1 series)
0,3,8,15,24………………………………n ((12-1)+(22-1)+(32-1)+(42-1)+(52-1)……………………n)

(Cube of natural number series) is
1,8,27,64,125………………………………n (13+23+33+43+53……………………n)
(Cube of natural number +1 series)
2,9,28,65,126……………………………n ((13+1)+(23+1)+(33+1)+(43+1)+(53+1)……………………n)
(Cube of natural number - 1 series)
0,7,26,63,124………………………………n ((13-1)+(23-1)+(33-1)+(43-1)+(53-1)……………………n)
When we see that our numerator or denominator is following one of the above series then
The 1st value will always be 1 example numo=1 or deno=1 although the series shows 1st term as 0 or
2 and INCREMENT WILL ALWAYS BE +1 LIKE NEMO=NEMO+1 ,DENO=DENO+1 as natural number
increases by 1.
Example
S=0/2+7/5+26/10+63/17…………………………………n
Here normally it looks
nemo=0 WRONG
deno=2 WRONG
but this is not correct, we can see that numerator is following (cube-1) series so
nemo=1
and denominator is following (square+1) series
deno=1

let’s complete the code
dim nemo,deno,s as double
din n as integer
n=val(text1)
nemo=1
deno=1
for i= 1 to n

s=s+(nemo*nemo+1)/(deno*deno*deno-1)
nemo=nemo+1
deno=deno+1
next

NESTED LOOPS

When one loop is nested within another, several iterations of the inner loop are performed for every
single iteration of the outer loop.

for i=1 to 3 OUTER LOOP
print(i), INNER LOOP
for j=1 to 2 INNER LOOP ENDS 1ST
print(j);
next j OUTERLOOP ENDS AFTER INNER LOOP

print(“”)
next I

The output of the above code will be :

ij

1 1 2 // inner loop completes its full rotation

2 1 2 // inner loop completes its full rotation

3 1 2 // inner loop completes its full rotation

Now outer loop completes its one rotation

So we can say that for every rotation of OUTER LOOP the INNER LOOP completes its full rotation, the total
number of rotation can be calculated by multiplying the upper limits of both loops like here 2x3 = 6
So this process will go sox times.

NESTED LOOPS FOR FACTORIAL SERIES

The factorial of a number is calculated as
Factorial 5 = 1 x 2 x 3 x 4 x5 =120
Factorial 3 = 1 x 2 x 3 =6
The symbol which represents factorial is !
Factorial of a number ‘n’ is calculated in visual basic as
f=1
for i=1 to n
f=f*i
next i
The above set of code is called factorial loop.
Sum the following series:
S= 1/2! + 3/4! + 5/6! ………………………………n
This series will start like any fractional series only we will add factorial code.
Solution:
Dim nemo,deno,s as Double
Dim n as integer
n=val(text1)
nemo=1
deno=2
for i=1 to n

f=1 // since we need factorial of denominator so we will add factorial code here
for i=1 to n

f=f*i
next i
s=s+ nemo/f
nemo=nemo+2
deno=deno+2
next i
List1.Additem(“Sum is “ & s)

PATTERNS PRINTING USING NESTED LOOPS

columns

*

*** R

***** O

******* W

********* S

Number of rows are vertical (counted downwards)

Number of columns are horizontal (counted right ways)

To code a pattern we count number of rows (5 in above pattern)
and count columns for the 1st row only (1 in above case)

Dim r,c as integer // ‘r’ for rows and ‘c’ for columns

r=5

c=1

for i = 1 to r

for j=1 to c

print ("* "); // ; (semicolon) is used to print the next “*” in same line

next j

print(" ") // is used to start from a new line

c=c+2

next i

INCLINED PATTERNS
The patterns which are inclined from left sides are to be coded in a little
different way. We need to keep indenting (spaces from left side).
The spaces from left side can increase or decrease as per pattern.

*
***
*****
*******

The above pattern is inclined from left side.
To make the above pattern get coded, we need a space loop.
A space loop is nothing more but a simple loop which prints single space
in it. So if the loop goes for 5 times, it prints 5 spaces horizontally.
For s = 1 To 5
Print (" ");
Next s
The above code will repeat 5 times and prints 5 spaces horizontally
*representing single space by ‘_’
_____
If we observe the above pattern we will see that the 1st row (with 1 star)
is farthest from left side and 2nd row is little nearer and so the distance
between the left side of screen and each row decreases every time.

10 spaces *
8 spaces * * *
6 spaces * * * * *
4 spaces * * * * * * *
So we will use a space loop before ‘j’ loop(as j loop is for column
handling). This space loop’s upper value decreases every time like
10,8,6,4.
Let’s code this way …
We know that the above pattern has 5 rows and topmost column has value
1, as we cannot count space so we take a value around 10- 60 let’s take
sp=10
Dim r, c, sp As Integer
sp = 10
r=5
c=1
For i = 1 To r
For s = 1 To sp
Print (" ");
Next s
For j = 1 To c
Print ("* ");
Next j
c=c+2
sp = sp - 2
Print (" ")

Next i

TWO PATTERNES CAN BE COMBINED TOGETHER

*

***

*****

*******

********* Above 1st pattern

******* Below 2nd pattern

*****

***

*

We can code the two patterns together.

Dim r, c as Integer

r=5

c=1

For i = 1 To r

For j = 1 To c

Print ("* ");

Next j

Print (" ")

c=c+2

Next i

r=4
c=7
For i = 1 To r
For j = 1 To c
Print ("* ");
Next j
Print (" ")
c=c-2
Next i

WORKSHEET 4.3

Write java codes to print the following patterns

******
******
**
**
*** *
****
**
**

******
******

**
**
*** ***
*** ***

*
***
*****
***
***
***
***

NUMBER PATTERNS

Number patterns are printed by using the same concept of symbol patterns, but a variable
which is going to handle the ‘numbers’ will be used.

Example
1
12
123
1234
12345

As in this pattern every row is starting with 1 so we have to initialize n=1 before the
horizontal process (j loop starts)

Dim r,c,n as Integer
r=5
c=1
For i = 1 To r

n=1
For j = 1 To c
Print (n);
n=n+1
Next j
Print ("")
c=c+1
Next i


Click to View FlipBook Version