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

Visual Basic6

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

Visual Basic

Precision Infomatics (M) Pvt. Ltd.


What is Visual Basic?
 VISUAL BASIC is a high level programming language which was evolved
from the earlier DOS version called BASIC.

 Visual Basic is an Object based Event Driven Programming Langugage.

 However, it seems people only use Microsoft Visual Basic today, as it is a


well developed programming language and supporting resources are
available everywhere.

 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.
Visual Basic Environment
 On start up, Visual Basic 6.0  will
display the following dialog box as
shown in figure.

 You can choose to either 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.

 There are various types of applications


we could create, however, we shall
concentrate on creating Standard EXE
programs
Visual Basic Environment
Visual Basic Environment
Building Visual Basic
Applications
 First, you have to launch
Microsoft Visual Basic. Normally,
a default form Form1 will be
available for you to start your
new project.
 Double click on form1, the source
code window for form1 as shown
in figure will appear.
 The top of the source code
window consists of a list of
objects and their associated
events or procedures. In the
given figure the object displayed
is Form and the associated
procedure is Load.
Building Visual Basic
Applications
 Private Sub Form_Load()
Form1.Show
Print "Welcome to Visual Basic
Tutorial"
End Sub
Building Visual Basic
Appliations
Building Visual Basic
Applications
Working With Controls
 Some of the Common Controls in VB
 Text Box
 Label
 Command Button
 Picture Box
 Image Box
 List Box
 Combo Box
 Check Box
 Option Box
 Driver list Box
 Directory list Box
 File List Box
Controls Properties
 Before writing an event
procedure for the control
to response to a user's
input, you have to set
certain properties for the
control to determine its
appearance and how it
will work with the event
procedure. You can set
the properties of the
controls in the properties
window or at runtime.
Control Properties
 You can also change the  Private Sub Form_Load()
properties at runtime to give Form1.Show
special effects such Form1.BackColor =
as change of color, shape, &H000000FF&
animation effect and so on. End Sub
 For example the following
code will change the form
color to red every time the
form is loaded.
 VB uses hexadecimal system
to represent the color.
 You can check the color
codes in the properties
windows which are showed
up under ForeColor and
BackColor .
Text Box Control
 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.
 String in a text box can be
converted to a numeric data
by using the function
Val(text).
 The following example
illustrates a simple program
that processes the inputs
from the user. 
Text Box Control
 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.
 String in a text box can be
converted to a numeric data
by using the function
Val(text).
 The following example
illustrates a simple program
that processes the inputs
from the user. 
Text Box Control
 In this program, three text boxes are
inserted into the form together with a few
labels.
 The two text boxes are used to accept
inputs from the user and one of the text
box will be used to display the sum of two
numbers that are entered into the two text
boxes.
 Besides, a command button is also
programmed to calculate the sum of the
two numbers using the plus operator.
 The program use creates a variable sum
to accept the summation of values from
text box 1 and text box 2.The procedure to
calculate and to display the output on the
textbox3 is shown below.

 Private Sub cmdcalculate_Click()


txtsum = Val(txtnumber1.Text) +
Val(txtnumber2.Text)
End Sub
Label Control
 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 outputs.
 One of its most important properties is
Caption. Using the syntax label.Caption, it
can display text and numeric data .
 You can change its caption in the properties
window and also at runtime. 
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 click 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
Picture Control
 The Picture Box is one of the controls that used to
handle graphics.
 You can load a picture at design phase by clicking on
the picture item in the properties window and select the
picture from the selected folder.
 You can also load the picture at runtime using the
LoadPicture method. For example, the statement will
load the picture sample.gif into the picture box.

Picture1.Picture=LoadPicture ("C:\VB program\Images\sample.gif")


Image Box
 The Image Box is another control that handles images
and pictures.
 It functions almost identically to the picture box.
However, there is one major difference, the image in
an Image Box is stretchable, which means it can be
resized.
 This feature is not available in the Picture Box. Similar
to the Picture Box, it can also use the LoadPicture
method to load the picture.
 For example, the statement loads the picture
Sample.gif into the image box.
Image1.Picture=LoadPicture ("C:\VB program\Images\sample.gif")
List Box
 The function of the List Box is to present a list of items where the user can click and select the
items from the list.

 In order to add items to the list, we can use the AddItem method.

 For example, if you wish to add a number of items to list box1, you can key in the following
statements.

 Private Sub Form_Load ( )


 
List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”
End Sub

 The items in the list box can be identified by the ListIndex property, the value of the ListIndex
for the first item is 0, the second item has a ListIndex 1, and the second item has a ListIndex 2
and so on.
Combo Box
 The function of the Combo Box is also to present a list of items where the
user can click and select the items from the list.
 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.
For example, if you wish to add a number of items to Combo box 1, you
can key in the following statements
Example 3.3
Private Sub Form_Load ( )
 
Combo1.AddItem “Item1”
Combo1.AddItem “Item2”
Combo1.AddItem “Item3”
Combo1.AddItem “Item4”
End Sub
Check Box
 The Check Box control lets the user to select or
unselect an option.
 When the Check Box is checked, its value is set to 1
and when it is unchecked, the value is set to 0.  You
can include the statements Check1.Value=1 to mark
the Check Box and Check1.Value=0 unmark the Check
Box, and use them to initiate certain actions.
 For example, the program will change the background
color of the form to red when the check box is
unchecked and it will change to blue when the check
box is checked.
Option Box
 The Option Box control also lets the user selects one of the
choices.
 However, two or more Option Boxes must work together because
as one of the Option Boxes is selected, the other Option Boxes
will be unselected.
 In fact, only one Option Box can be selected at one time. When an
option box is selected, its value is set to “True” and when it is
unselected; its value is set to “False”.
 In the following example, the shape control is placed in the form
together with six Option Boxes. When the user clicks on different
option boxes, different shapes will appear.
 The values of the shape control are 0, 1, and 2,3,4,5 which will
make it appear as a rectangle, a square, an oval shape, a
rounded rectangle and a rounded square respectively.
Option Box Example
 Private Sub Option1_Click ( )
Shape1.Shape = 0
End Sub
Private Sub Option2_Click()
Shape1.Shape = 1
End Sub
Private Sub Option3_Click()
Shape1.Shape = 2
End Sub
Private Sub Option4_Click()
Shape1.Shape = 3
End Sub
Private Sub Option5_Click()
Shape1.Shape = 4
End Sub
Private Sub Option6_Click()
Shape1.Shape = 5
End Sub
Driver List Box
 The Drive ListBox is
used to display a list of
drives available in your
computer. When you
place this control into the
form and run the
program, you will be able
to select different drives
from your computer as
shown in Figure.
Directory List Box
 The Directory List Box is
used to display the list of
directories or folders in a
selected drive.
 When you place this
control into the form and
run the program, you will
be able to select
different directories from
a selected drive in your
computer as shown in
Figure
File List Box

 The File List Box is used to display the


list of files in a selected directory or
folder. When you place this control into
the form and run the program, you will be
able to a list of files in a selected
directory.
Data Types in Visual Basic

 Visual Basic classifies the information mentioned into two

major data types, they are numeric data type and non-

numeric data types.

Data Types

Non Numeric
Numeric Data Type
Data Type
Numeric Data Type
Non Numeric Data Type

You might also like