You are on page 1of 34

VISUAL BASIC

Controls

Version 1
1
Trg. Div., NIC HQRs
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.

Version 1
2
Trg. Div., NIC HQRs
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

Version 1
3
Trg. Div., NIC HQRs
Pointer Picture Box

Label control Text box


Frame control Command Button
Check box Option Button
Combo box List box
Horizontal scroll bar Vertical scroll bar
Timer Control Drive list
Directory list box File list box
Shape Object Line object
Image object Data control
OLE control Version 1
4
Trg. Div., NIC HQRs
Visual Basic Controls
Some of the common terminology associated with
control
? Control as a object
? Property
? Method
? Event
? Procedure
? Program Statement
? Variable

Version 1
5
Trg. Div., NIC HQRs
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.
Version 1
6
Trg. Div., NIC HQRs

? .
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.

Version 1
7
Trg. Div., NIC HQRs
Variable
? Itis 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
Country = “India” etc.
Version 1
8
Trg. Div., NIC HQRs
Method
? Itis 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.
Version 1
9
Trg. Div., NIC HQRs
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
action needs to be taken by writing your own code

Version 1
10
Trg. Div., NIC HQRs
Event Procedure
? Itis 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.
Version 1
11
Trg. Div., NIC HQRs
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

Version 1
12
Trg. Div., NIC HQRs
Nomenclature convention of
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

.
Version 1
13
Trg. Div., NIC HQRs
Nomenclature convention of
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.

Version 1
14
Trg. Div., NIC HQRs
Pointer
The pointer is used for
selecting objects which is
to placed on the form.

Version 1
15
Trg. Div., NIC HQRs
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
.EMF Enhanced MetaFile
Version 1
16
Trg. Div., NIC HQRs
Image Control

Image control is designed


specifically for displaying images
and not for creating new images or
manipulating

Version 1
17
Trg. Div., NIC HQRs
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
or Cancel, Start or Quit.

Version 1
18
Trg. Div., NIC HQRs
USE OF TEXTBOX & COMMAND BUTTON

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

Version 1
19
Trg. Div., NIC HQRs
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.
Version 1
20
Trg. Div., NIC HQRs
Check Boxes

Check boxes act as toggle


switches, turning options
on or off.

Version 1
21
Trg. Div., NIC HQRs
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
enclosed in a frame.
Version 1
22
Trg. Div., NIC HQRs
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.
Version 1
23
Trg. Div., NIC HQRs
Use of List Box

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

Version 1
24
Trg. Div., NIC HQRs
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

Version 1
25
Trg. Div., NIC HQRs
Use of Combo box
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
Version 1
26
Trg. Div., NIC HQRs
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.

Version 1
27
Trg. Div., NIC HQRs
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.

EXAMPLE

Version 1
28
Trg. Div., NIC HQRs
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
End Sub
Version 1
29
Trg. Div., NIC HQRs
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.

Version 1
30
Trg. Div., NIC HQRs
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.
SelectFile = File1.Path & “\” & File1.filename
Image1.Picture = LoadPicture(SelectedFile)
Version 1
8. Now Run the program. Trg. Div., NIC HQRs 31
The Shapes (either a circle, oval, square or
rectangle)
and Line are purely for decorative purpose

Version 1
32
Trg. Div., NIC HQRs
Data Control

The Data control allows you to read


records in from a database

Version 1
33
Trg. Div., NIC HQRs
OLE
OLE stands for Object Linking
and Embedding. This control
allows you to make a two way
link with an object in another
Windows application.

Version 1
34
Trg. Div., NIC HQRs

You might also like