VISUAL BASIC
Controls
These are the objects which can be
pasted onto a form which display text,
through picture boxes for graphic
images, to buttons, check boxes, lists
and other menus. Each control can have
code attached to it.
ToolBox
ToolBox
?
Tool Box contains the standard controls.
?
You can add more controls by from
components
?
You can create your own controls ActiveX
controls customised to specific purposes
Picture Box
Text box
Command Button
Option Button
List box
Vertical scroll bar
Drive list
File list box
Line object
Data control
Pointer
Label control
Frame control
Check box
Combo box
Horizontal scroll bar
Timer Control
Directory list box
Shape Object
Image object
Visual Basic Controls
Visual Basic Controls
Some of the common terminology associated with
control
?
Control as a object
?
Property
?
Method
?
Event
?
Procedure
?
Program Statement
?
Variable
Version 1
Property
Property
?
With each object , various properties are
associated which describe its characteristics. The
properties can be set at design time by using
property window or at run time through program
statements
Eg: Command1.Caption = “ Hello”
Few common properties are:
Name, Caption, Appearance, Enable (True / False),
Visible ( True / false) etc.
Program Statement
Program Statement
It is a keyword in the code that does the work of a
program
Eg: Unload formissue
Call DISABLE ( Call procedure Disable)
Form2.Show
Setdb = OpenDatabase(C:inventory.mdb)
Data1.UpdateRecord etc.
Variable
Variable
?
It is a special container that holds data
temporarily in a program
?
Numbers, Charatcter strings, property
values etc can be stored in variables
Eg : Dim db As Database
Dim strdiv As String
Dim rs As Recordset
Method
Method
?
It is a special statement that performs an
action or a service for a particular object in
a program
Syntax : Object.Method Value
Eg : List1.AddItem “Printer”
ShowOpen
SowPrint etc.
Form Events
Form Events
?
Whenever an event is triggered ( click, drag,
resize, double click, key press etc.) a message is
generated that describes your action.This message
is sent to appropriate control, which in turn
generates appropriate action based on pre-defined
method.
?
The event message can be customised the way
Event Procedure
Event Procedure
?
It is a block of code that is executed when
an object is manipulated in a program.
Eg: When an event takes place, block of
program code associated with it gets
executed.
Few common events are :
Click, Dbclick, MouseDown, MouseUp,
MouseMove etc.
Form Methods
Form Methods
?
It is a command that allows you to tell an
object what to do
eg: Show
hide, Unload, Print
An example of programming construct:
If Lable1.Caption = “xxx” Then hide form1
Object Property Assigned VB
method Object
name name Operator Function
Nomenclature convention of
Nomenclature convention of
the objects
the objects
For forms (
three nomenclatures are associated with it)
1. Name of the object form
2. Name of the caption of the form
3 Form file name for saving it as a part of project
To avoid any confusion later, one should follow a convention of
nomenclature, and should strictly follow throughout the project
It is suggested to follow a convention:illustrated as follows
1 name the form object as -> frmSample
2. Name of the caption as -> Sample
3. Name of the file for saving it: -> Sample
Nomenclature convention of
Nomenclature convention of
the objects
the objects
For other objects
It is suggested to follow a convention:illustrated as follows
1 Name of an object frmSample, txtName, mnuMain, cmdOK,
cmdAddItem , datCourses etc.
2. Caption of the object The way, you want it to appear on the
screen etc.
Pointer
The pointer is used for
selecting objects which is
to placed on the form.
Picture box
A Picture box holds pictures created with the
paintbrush or similar art packages. These must be
in acceptable format like .BMP, .WMF, .ICO etc.
.BMP Bitmap
.WMF Windows MetaFile
.ICO Icons
.GIF Graphic Interchange Format
.JPG Joint Photographic Expert Group
. DIB Device Independent Bitmap
Image control is designed
specifically for displaying images
and not for creating new images or
manipulating
Labels and Text Boxes
Labels and Text boxes both holds
text, but only Text Box is capable of
receiving input from the user.
Command Button
Command buttons are one method of selection.
They are typically used Where there are limited
number of options - the choice may be be
OK
1
. Click the textbox control in the toolbox.
2. Move the mouse pointer to the form. Draw the Textbox of your need.
3. Click the command button control to the form
4. Draw the command button of any size.
5. Use the Properties set window & set the
Properties as
Control
Property
Setting
Text1 Text (empty)
Command1 Caption “OK”
6. Double click the OK command button. Type the
following
Text1.Text = “ Starting of Visual Basic”
7. Now Run the program
Frame Control
A Frame simply holds things together. If
you place a set of controls with in a
frame, they are all moved together as the
frame is moved.
Note:- that once a control has been
placed in a frame, it cannot be
moved out onto the form, and vice
versa.
Check Boxes
Check boxes act as toggle
switches, turning options
on or off.
Option buttons
Option buttons are normally
used to select one from a set
of mutually exclusive
options. At any one time
there can be only one set on
a form, unless they are
List box
List boxes display list of items,
so that user can see what is
available and select one. If the
list is too long to fit in box,
vertical scroll bar will be added
at run time. Items can only be
added to the list at the run- time ,
not at design time
.
1. Drag the List Box on the Form
2. Double Click the List Box and type the following
Private Sub Form_Load()
List1.AddItem "White"
List1.AddItem "Black"
List1.AddItem "Yellow"
List1.AddItem "Green"
List1.AddItem "Red
"
End Sub
3. Run
Combo box
A Combo box combine a
drop-down list with a slot in which
user can enter their own data
when the program is running
1. Drag the List Box on the Form
2. Double Click the List Box and type the
following
Private Sub Form_Load()
Combo1.AddItem "one"
Combo1.AddItem "two"
Combo1.AddItem "three"
Combo1.AddItem "four"
Combo1.AddItem "five"
Combo1.AddItem "six”
End Sub
3. Run
Horizontal and Vertical scroll bars
The Horizontal and Vertical scroll
bars are used on forms to give a
very flexible means of setting
values. When the Program is
running, the position of the slider
determines the value returned by
the scroll bar.
Timer
The Timer is unusual in that it is to be fixed size
and is invisible once the program is running. Its
purpose is to control actions the must take place
at or after set intervals.
Private Sub Command1_Click()
If Text1.Text = "secret" Then
Timer1.Enabled = False
MsgBox ("Welcome To the System")
Else
MsgBox ("Sorry, Friend, I don't know you.")
End If
End Sub
Private Sub Timer1_Timer()
MsgBox ("Sorry , Your time is up.")
End
Drive List box , Directory List box
and File List box
The Drive List box,Directory List box and File List box
can be used together to give the standard Windows
file management facilities. When the program runs
they will initially display the current drive and
directory. These can be changed by clicking and
highlighting in the usual way. Three boxes together
can produce a highly efficient file selection routine.
USE OF DRIVELISTBOX, DIRECTORY LISTBOX ,
FILE LISTBOX & IMAGE CONTROLS
1. Click theDriveListBox in the toolbox.
2. Move the mouse pointer to the upper-left corner of the form and then draw a drivelistbox.
3. Similarly draw the Directorylistbox and filelistbox.
4. Add the image box control to the right side of your form. 5. Double click the drivelistbox and type the following
Dire1.Path = Drive1.Drive Close the Code window
6. Double click the directorylistbox and type the following File1.Path = Dir1.Path
Close the Code window
7. Double click the Filelistbox and add the following code to the File1_Click event procdure.