Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

F.Y.J.C Science - Computer Science I: Visual Basic

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

F.Y.J.

C Science Computer Science I


Visual Basic
Introduction
VISUAL BASIC is a visual and events driven programming Language. These are the main
divergences from the old BASIC. In BASIC, programming is done in a text based environment and
the program is executed sequentially. In VISUAL BASIC, programming is done in a graphical
environment. In old BASIC, you have to write a text-based procedure to design the interface, but
Visual Basic enables you to design
the interface by dragging and resizing the objects as well as changing their colors, just like any
windows-based programs.
Visual Basic is event-driven 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).
Examples of events are clicking a command button, entering text into a text box, selecting an
item in a list box etc. Therefore, a VISUAL BASIC Program is made up of many subprograms,
each with its own program code which can be executed independently and at the same time can
be linked together in one way or another.

Three Editions of VB :
1) The VB Learning Edition.
2) The VB Professional Edition.
3) The VB Enterprise Edition.
1) The Visual Basic Learning Edition :
It is introductory edition that lets you easily create windows applications. It covers with all the
tools you need to build mainstream windows applications.
2) The Visual Basic Professional Edition :
It is for computer professionals and includes advanced features such as tools to develop Active X
and Internet Controls.
3) The Visual Basic Enterprise Edition :
It is advanced edition and is assigned at programmers who build distributed applications in a
team environment. It includes all the features of professional Edition plus tools such as Visual
Source Safe and the automation and component manager.

Features of VB
GUI Interface
VB is a Graphical User Interface language. This means that a VB program will always show
something on the screen that the user can interact with to get a job done.
Debugging
The Runtime Debugger helps to find and fix bugs in programs at runtime
Efficiency
Visual Basic is designed to make software development easy and efficient.
Easy to use and understand
VB provides a graphical user interface that allows the developer drag and drop objects into the
programs as well as manually write program code.

What is GUI?
Nirmala Memorial Foundation college of Commerce and Science

Page 1

The graphical user interface describes anything your application displays to the user. It is the
primary way you interact with the user and allow him or her to interact with you. Creating great
GUI's is something Visual Basic is amazing at. You can create professional interfaces with
minimal effort. Learn how with these UI Visual Basic tutorials.
Graphical User Interfaces have been around for many years. As you develop more and more
advanced applications it is worth your time to focus on your VB6 UI design. This includes
understanding how each of the visual basic ui components work (such as buttons, textboxes,
combo boxes, images, menus, etc.). By understanding all of these components individually you
will have a good knowledge of what can and can't be done.

The Visual Basic Environment

1) The Menu Bar :


It contains basic menus File, Edit, View, Project, Format, Debug, Run, Query, Diagram, Tools, AddIns, Window, Help .
2) The Project Explorer :
Displays the component of the project. Simple projects are made of a single form. The project
window is called Project explorer, because it has the look of window explorer.
3)

The Toolbox
It contains the icons of the control you
can place on a form to create the applications
user interface. To place a control on a form, you
first select it with the mouse and then move the
mouse over the form. The cursor turns into a
cross and you can draw the control on the form.

Nirmala Memorial Foundation college of Commerce and Science

Page 2

4) The Properties Window :


It contains the property setting for the selected control, such as size, caption, name and colour.

5) The Form Designer :


It is main window in the middle of the screen and in it you design and edit the applications user
interface. The form designer displays two windows for each form : The form itself and a code
window.
6) The Form Layout
You can use form layout window to determine the initial position of forms in your application.

Controls used in VB

a) The Text Box :

The text box is the standard control that is used to receive input from the user as well as to
display the output. It can handle string (text) and numeric data but not images or pictures.

b) The Label :
The label is a very useful control for Visual Basic, as it is not only used to provide instructions
and guides to the users, it can also be used to display output. One of its most important
properties is Caption. Using the syntax Label.Caption. It can display text and numeric data.
c) The Command Button
The command button is a very important control as it is used to execute commands. It displays
an illusion that the button is pressed when the user clicks on it. The most common event
associated with the command button is the Click event, and the syntax for the procedure is:
Private Sub Command1_Click ()
Statements
End Sub
d) The Picture Box
The Picture Box is one of the controls that used to handle graphics. You can load a picture during
the designing phase by clicking on the picture item in the properties window and selecting the
picture from the selected folder. You can also load the picture at runtime using the LoadPicture
method.
e) The Image Box
The Image Box is another control that handles images and pictures. It functions almost
identically to the picture box.
f) The List Box
The function of the List Box is to present a list of items. The user can click and
Select items from this list. In order to add items to it, use the AddItem method.

Nirmala Memorial Foundation college of Commerce and Science

Page 3

g) The Combo Box


The function of the Combo Box is also to present a list of items. However, the User
needs to click on the small arrowhead on the right of the combo box to see the items which are
presented in a drop-down list. In order to add items to the list, you can also use the AddItem
method.
h) The Check Box
The Check Box control lets the user select or unselect an option. User can select more than
one check box.
i) The Option Button
The Option Button also lets the user select one of the choices, user can not select more than
one option.

Example :
1) To calculate summation of two numbers
entered by user.
Design your form as below

Private Sub Command1_Click()


Dim n1 as Integer
Dim n2 as Integer
Dim Sum as Integer
n1= Val(Text1.Text)
n2= Val(Text2.Text)
Sum = n1+n2

// Sum of two numbers

Label1.Caption = Sum
End Sub
2. If you want to display sum of two numbers in message box, Then use following
coding.
Private Sub Command1_Click()
Dim n1 as Integer
Dim n2 as Integer
Dim Sum as Integer
n1= Val(Text1.Text)
n2= Val(Text2.Text)
Nirmala Memorial Foundation college of Commerce and Science

Page 4

Sum = n1+n2

// Sum of two numbers

Msgbox(Sum of two number is : &sum)


End Sub

Data Types
a) Numeric Data
Numeric data are data that consist of numbers, which can be computed
mathematically with various standard operators such as add, minus, multiply, divide
and so on.
b) Non-numeric Data Types
Non numeric data that consists of data which can not be computed mathematically with various
standard operators.
Type

Stores

Integer

Single

Whole
Number
Whole
Number
Decimal

Double

Decimal

Long

Currenc
y
String
Byte

Date
Object

Variant

Memory(by
te)
2
4
4

Range
-32,768 to
+32,767
+/- 2 billions
+/- 1E45 to
3E-38
+/- 5E324 to
1.8E308
+/- 9E14

8
8

Text

1/char

Whole
Number
Logical

<= 65400
char
0-255

True/False

Date &
Time
Instance
of
Classes
Any of
above

8
4

1/1/100 to
12/31/9999
N/A

16 + 1/char

N/A

Variables
In terms of VISUAL BASIC, variables are areas allocated by the computer memory to hold data.
Each variable must be given a unique name. To name a variable in Visual Basic, you have to
follow a set of rules.

Rules for naming the variable in VB:


The name must be start with a letter not number or other character.
The remainder of name can contain numbers, letters and/or underscore character. Space
,Punctuation are not allowed.
Name should be unique within variable scope.
The name can be no longer than 255 character.
No reserve words. (for example dim, string, caption, text are not allowed as name for
variable)
Valid Name

Invalid Name

Nirmala Memorial Foundation college of Commerce and Science

Page 5

My_Car
thisyear

Car&toy
1newBoy

For example :
Dim num1 as Integer
Dim num2 as Variant
Dim name as String

Decision Making Statement


Conditional Operators
To control the VISUAL BASIC program flow, we can use various conditional
operators. Basically, they resemble mathematical operators. Conditional operators
are very powerful tools which let the VISUAL BASIC program compare data values
and then decide what action to take, whether to execute or terminate the program etc.
Conditional Operators
=
Equal to
> More than
<
Less than
>=
More than and equal to
<=
Less than and equal to
<>
Not equal to
Logical Operators
In addition to conditional operators, there are a few logical operators that offer added power to
the VISUAL BASIC programs.
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

Using If.....Then.....Elseif.Else Statements with Operators


1) Simple IF statement
Syntax:
If condition Then
command
End IF
Example:
If num1 > num2 Then
msgbox(number 1 is Greater)
End If

2) IF---Else--- Statement
Syntax:
IF condition Then

Nirmala Memorial Foundation college of Commerce and Science

Page 6

Else

End IF
3) If- ElseIf Else
Syntax:
If condition Then

ElseIf condition Then

Else
..
End If
Example:
Dim n1 as Integer
Dim n2 as Integer
n1=Text1.text
n2=Text2.text
If n1 > n2 Then
Msgbox(n1 is greater than n2)
Else
Msgbox(n2 is Greater)
End IF

Loop Statements in VB
1) Do While .. Loop
The structure of a Do Loop command can be written in 2 different formats as follows:
a) Do While condition
Block of one or more VISUAL BASIC statements
Loop
b) Do
Block of one or more VISUAL BASIC statements
Loop While condition
Example :
Print 1 to 10 numbers on form using Do-while Loop

Private Sub command1_Click ()


Dim i as Integer
Nirmala Memorial Foundation college of Commerce and Science

Page 7

i=1
Do while i <= 10
Print i
Loop
End sub
2) The While.Wend Loop
The structure of a While.Wend Loop is very similar to the Do Loop. It takes the
following format:
While condition
Statements
Wend
Ex:
To print 1 to 10 numbers using While- wend.
Private Sub command1_Click ()
Dim i as Integer
i=1
while i <= 10
Print i
Wend
End sub

3) The For....Next Loop


The For.Next loop is a very useful loop if we intend to have a fixed number of
repetitions. It also allows the step increment. If you do not add the step increment,
the default increment is 1.
The structure of a For.Next loop is:
For Variable_name =startNumber to endNumber (Step increment or decrement)
One or more VISUAL BASIC statements
Next Variable_name
For example:
Print 1 to 10 numbers on form using For.Next Loop
Private Sub Command1_Click ()
Dim i as Integer
For i = 1 To 10
Print i
Next i
End sub
Print 10 to 1 numbers on form using For.Next Loop
Private Sub Command1_Click ()
Dim i as Integer
For i = 10 To 1 Step -1
Nirmala Memorial Foundation college of Commerce and Science

Page 8

Print i
Next i
End sub

Functions
The main purpose of the functions is to accept certain input and return a value which is passed
on to the main program to finish the execution. There are two types of functions, the built-in
functions (or internal functions) and the functions created by the programmers.

Built In Functions :
Function
Abs
Asc
Chr

Return Value
Absolute value of a number
ASCII code of a character
Character corresponding to

Cos
Date
Now
Sin
Sqr
Time
Val

given ASCII Code


Cosine of an angle
Current date as a text string
Current date and time
Sine of an angle
Square root of a number
Current time as a text string
Numeric value of a given
string

For Example :
1) Private Sub Command1_Click ()
Dim n1 as Integer
Dim n2 as Integer
n1 = val(Text1.text)
//Val function
n2= val(Text2.text)
Print(sqr(n1))
//Sqr function for square root
Print(sin(n1))
//Sin function for sine
Print (Abs(n1))
//Absolute value of given number
Print(Now)
//Now function for current date
End sub

User Defined Functions


We can define our own set of functions called as User defined functions.
The general format of a function is as follows:
Public

Function

functionName (Arg As dataType,..........)

Public indicates that the function is applicable to the whole project.


For ex.
1) Private Sub Command1_Click ()
Nirmala Memorial Foundation college of Commerce and Science

Page 9

Call display
End Sub
Public Function display()
Msgbox(Welcome,, You have successfully run this program)
End Function

Nirmala Memorial Foundation college of Commerce and Science

Page 10

C++
String Functions in C++
Strlen()
Returns the length of string.
Strlen(Mumbai)
Will return 6 as a length of string Mumbai
Strcmp()
It compares two strings.
strcmp(s1, s2)
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
Strcat()
It concatenates two strings
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
Strrev()
It will reverse the string.
Strrev(mumbai)
Output will be iabmum

For examples :
1)
#include <iostream.h>
#include<conio.h>
#include <string.h>
void main( )
{
char S1[ ] = "test";
char S2[80];
strcpy(S2,S1);
cout << "String1: " << S1 << en
dl;
cout << "String2: " << S2 <<
endl;
getch( );
}

Output :
String1: test
String2: test
2)
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s1[80], s2[80];
strcpy(s1, "C++");
strcpy(s2, " is power programming.");
Nirmala Memorial Foundation college of Commerce and Science

Page 11

cout << "lengths: " << strlen(s1) ;


cout << ' ' << strlen(s2) << endl ;
if(!strcmp(s1, s2))
cout << "The strings are equal"<<endl ;
else
cout << "The Strings not equal<<endl;
strcat(s1, s2);
cout << s1 << endl;
getch( );
}

3)
#include <iostream>
#include<conio.h>
#include <string.h>
void main( )
{
char S1[ ] = "Nirmala College" ;
Cout<<strlen(S1)<<endl ;
getch( );
}
Output :

Nirmala Memorial Foundation college of Commerce and Science

Page 12

You might also like