KEMBAR78
CSC 1201 Lecture Note B - Visual Basic Programming | PDF | Basic | Computing
0% found this document useful (0 votes)
7 views34 pages

CSC 1201 Lecture Note B - Visual Basic Programming

Uploaded by

nasibah23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views34 pages

CSC 1201 Lecture Note B - Visual Basic Programming

Uploaded by

nasibah23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

INTRO.TO COMPUTER (SEC.

B)
VISUAL BASIC

Engr. Abdulrahman Muhammad Bello(COREN)


SCIENCE AND TECNOLOGY EDUCATION,BUK
A brief description of Visual Basic.
 VISUAL BASIC is a high level programming
language evolved from the earlier DOS version
called BASIC.
 BASIC, means Beginners' All purpose Symbolic
Instruction Code. It is a fairly easy
programming language to
 learn. The codes look a bit like English
Language. Different software companies
produced different version of
BASIC, such as Microsoft QBASIC,
QUICKBASIC, GWBASIC ,IBM BASIC and so
on.
VISUAL BASIC is a VISUAL and events driven
Programming Language.
These are the main divergence from the old BASIC.
In BASIC, programming is done in a text-only
environment and the program is executed
sequentially.
In VISUAL BASIC, programming is done in a graphical
environment. Because users may click on
a certain object randomly, so each object has to be
programmed independently to be able to response to
those actions(events). Therefore, a VISUAL BASIC
Program is made up of many subprograms, each has its
own program codes, and each can be executed
independently and at the same time each can be linked
together in one
way or another.
On start up, Visual Basic 6.0 will display the
following dialog box as shown in figure 1.1.
You can choose to start a new project, open
an existing project or select a list of
recently opened programs. A
project is a collection of files that make up
your application.
The Visual Basic Start-up Dialog
Box
In figure 1.2, the Visual Basic Environment
consists of
*The Blank Form window which you can design
your application's interface on.
*The Project window displays the files that are
created in your application.
*The Properties window, which displays the
properties of various controls and objects that
are created.
It also includes a Toolbox that consists of all
the controls essential for developing a VB
Application.
Controls are tools such as boxes, buttons, labels
and other objects drawn on a form to get input
or display output. They also add visual appeal.
 Step 1 Draw the interface
 Step 2 Set Properties
 Step 3 Write the events code
Example 2.1
This program is a simple program that
calculate the volume of a cylinder. Let design
the interface
Example 2.1
This program is a simple program that calculate the
volume of a cylinder. Let design the interface
First of all, go to the properties window and
change the form caption to Volume Of Cylinder.
Then draw
three label boxes and change their captions to
Base Radius, height and volume respectively.
After that, draw three Text Boxes and clear its
text contents so that you get three empty boxes.
Named the text boxes as radius ,hght(we cannot
use height as it is the built-in control name of
VB)and volume
respectively. Lastly, insert a command button
and change its caption to O.K. and its name to
OK. Now save the project as cylinder.vbp and
the form as cylinder.vbp as well. We shall leave
out the codes at the moment
Now we shall attempt to write the codes for
the cylinder program.
Now, double click on the O.K button and enter
the codes between Private Sub OK_Click( ) and
End Sub
Private Sub OK_Click( )
r = Val(radius.Text)
h = Val(hght.Text)
pi = 22 / 7
v = pi * (r ^ 2) * h
volume.Text= Str$(v)
End Sub
 Underweight = <18.5
 Normal weight = 18.5-24.9
 Overweight = 25-29.9
 Obesity = BMI of 30 or greater
Body Mass Index(BMI) Of a Human Body can by
calculated using the equation below:
BMI =weight/( height)2
where weight is measured in kg and height in
meter.
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArsgs)
Handles Button1.Click
 Dim height, weight, bmi As Single
 height = TextBox1.Text
 weight = TextBox2.Text
 bmi = (weight) / (height ^ 2)
 Label4.Text = bmi
 End Sub
In addition to conditional operators, there are
a few logical operators which offer added
power to the VB programs.
Operator: Meaning:
And Both sides must be true
Or One side or other must be true
Xor One side or other must be true but
not both
Not Negates truth
To effectively control the VB program flow, we
shall use If...Then...Else statement together with
the
conditional operators and logical operators.
The general format for the if...then...else
statement is
If conditions Then
VB expressions
Else
VB expressions
End If
* any If.. Then.. Else statement must end with End
If. Sometime it is not necessary to use Else.
This is a Program That will evaluate age according to
entry.
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim myAge As Integer
myAge = TextBox1.Text
If myAge > 100 Then
Label2.Text = " Congratulation! You win a lucky prize"
Else Label2.Text = " Sorry, You did not win any prize"
End If
End Sub
Example:
Private Sub OK_Click()
firstnum = Val(usernum1.Text)
secondnum = Val(usernum2.Text)
total = Val(sum.Text)
If total = firstnum + secondnum And Val(sum.Text)
<> 0 Then
correct.Visible = True
wrong.Visible = False
Else
correct.Visible = False
wrong.Visible = True
End If
End Sub
when you run the program , you should be able
to see the interface as shown above. if you
enter a value
each in the radius box and the height box,
then click OK, the value of of the Volume will
be displayed in
the volume box.
Procedure for clicking the OK button to
calculate the volume of cylinder
get the value of r from the radius text box
get the value of h from the height text box
assign a constant value 22/7 to pi
calculate the volume using formula
output the results to the Volume text box
End of Procedure
Back End Code.
 Private Sub btngenerate_Click(sender As System.Object, e As System.EventArgs) Handles
btngenerate.Click
 Dim Sunandalibi As String
 Dim Introtocomputer As Integer
 Dim Maths1302 As Integer
 Dim Physics1210 As Integer
 Dim Average As Integer
 Dim Sakamako As String
 Dim total As Integer

 Sunandalibi = txtsunandalibi.Text
 Introtocomputer = txtintrotocomp.Text
 Maths1302 = txtmaths1302.Text
 Physics1210 = txtphysics1210.Text
 total = Int(Introtocomputer) + Int(Maths1302) + Int(Physics1210)
 Average = total / 3

 lblAverage.Text = Average
 If Average >= 90 And Average <= 100 Then
 lblSakamako.Text = "A+ -- Sai Kwaro!"
 ElseIf Average >= 80 And Average < 90 Then
 lblSakamako.Text = "A ---Bravo"

 End If
 End Sub
 End Class
Course Evaluation App.
Develop the front end of the above code and
run it on the Visual Basic Program application.
There are many types of data we come across
in our daily life. For example, we need to
handle data such
as names, addresses, money, date, stock
quotes, statistics and e.t.c everyday. Similarly
In Visual Basic, we are also going to deal with
these kinds of data. However, to be more
systematic, VB divides data into
different types.
Numeric Data
Numeric data are data that consists of
numbers, which can be computed
mathematically with various
standard operators such as add, minus,
multiply, divide and so on
Variable Names
The following are the rules when naming the
variables in Visual Basic
It must be less than 255 characters No spacing
is allowed, It must not begin with a number,
Period is not permitted.
Examples of valid and invalid variable names
are displayed in Table 5.4

Valid Name Invalid Name


My_Car My.Car
this year 1NewBoy
He&HisFather *& is not
acceptable
In Visual Basic, one needs to declare the
variables before using them by assigning names
and data types.
They are normally declared in the general
section of the codes' windows using the Dim
statement.
The format is as follows:
Dim variable Name as Data Type
Example 5.1
Dim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim doDate As Date
You may also combine them in one line ,
separating each variable with a comma, as
follows:
Dim password As String, yourName As String,
firstnum As Integer
After declaring various variables using the Dim
statements, we can assign values to those
variables.
The general format of an assignment is
variable can be a declared variable or a
control property value. The expression could
be a
mathematical expression, a number, a string, a
boolean value(true or false) and etc.
The following are some examples.
firstNumber=100
secondNumber=firstNumber-99
userName="John Lyan"
userpass.Text = password
Label1.Visible = True
Command1.Visible = false
Label4.Caption = textbox1.Text
ThirdNumber = Val(usernum1.Text)
total = firstNumber +
secondNumber+ThirdNumber
Programming languages consist of a set of rules
that allows string values to be converted into
various ways of generating machine code.
TYPES.
1. Procedural programming languages
C and C++
Java
Pascal
BASIC
2. Functional programming languages
Scala
Erlang
Haskell
Elixir
F#
3. Object-oriented programming languages
Java
Python
PHP
C++
Ruby
4. Scripting languages
E.g
PHP
Ruby
Python
bash
Perl
Node.js
5. Logic programming languages
Prolog
Absys
Datalog
Alma-0

You might also like