8/8/2019 Unit II Basic VB
1/84
Understanding concepts in VB
The user interface is the link between the user and the application. The
usability of the application depends on the interface. While creating anapplication in VB, the form is the basic element of the user interface.
Controls are then added to provide specific functionality. Both these
objects can be modified by the programmer by changing their
properties, methods and events.
8/8/2019 Unit II Basic VB
2/84
Designing the interface
To Draw a Control on a form
To remove a control from the form
To move / resize a control
Aligning controls
Container control / child control
8/8/2019 Unit II Basic VB
3/84
Naming Controls
The names of controls and forms must satisfy the following
criteria: It must begin with a letter.
must contain only letters, numbers and underscore character(
_ ).
Punctuation characters and spaces are not allowed.
must not be longer than 40 characters.
8/8/2019 Unit II Basic VB
4/84
Which of the following are valid object names:
Name
Lbx12
@Rs
_lb1
123_label
?1
Star1
%age
Valid / Invalid
Valid
Invalid
Invalid
Invalid
Invalid
Valid
Invalid
8/8/2019 Unit II Basic VB
5/84
We can use a prefix to describe a class, followed by a
descriptive name for the control. Using this naming
convention makes the code more self-descriptive and
alphabetically groups similar objects in the object list box.
E.g. we can name a checkbox control as ChkReadOnly
8/8/2019 Unit II Basic VB
6/84
Control Prefix
Label Lbl
Frame Fra
Chekbox chk
Combobox Cbo
Hscrollbar Hsb
Timer Tmr DirListBox Dir
Shape Shp
8/8/2019 Unit II Basic VB
7/84
Control Prefix
Image Img
Line Lin
PictureBox Pic
TextBox Txt
CommandButton Cmd
OptionButton Opt
ListBox Lst
8/8/2019 Unit II Basic VB
8/84
Properties
Properties are the object attributes. The controls / objects that
we draw on a form, have some properties associated with
them. After a control is added to a form, the programmer can
set the characteristics of the controls and the form such as
size color, screen location or the state of the control, such as
enabled or disabled.
8/8/2019 Unit II Basic VB
9/84
A few common properties are :
Name
Appearance
Backcolor
Forecolor Font
Caption
Width
Height
Left
Top Enabled
Visible
8/8/2019 Unit II Basic VB
10/84
Working with Forms
In VB, the Form acts as the container for all the controls that
make up the interface. The form is the top-level object in a VBapplication, and every application starts with the Form.
8/8/2019 Unit II Basic VB
11/84
Characteristics of a form:
y Title bar: on which the forms caption is displayed.
y Control menu: On left end of the title bar is the control menu icon.Clicking this icon opens the control menu. The control menucontains the following commands:
1. Restore
2. Move
3. Size4. Minimize
5. Maximize
6. Close
8/8/2019 Unit II Basic VB
12/84
On the right side of the title bars are the three buttons:
1. Minimize
2. maximize3. close buttons
Clicking these buttons perform the associated function.
8/8/2019 Unit II Basic VB
13/84
Customizing form properties:
Property Description
MinButton Specifies whether a minimize window button appears on
the form.
MaxButton Specifies whether a maximize window button appears on
the form.
Controlbox This property displays the control menu, if it is set to true.
By default it is true. We can set it to false to hide the
control menu icon and disable the control menu.When the controlbox property is set to false the three
buttons (Minimize, Maximize and close) on the title bar
also gets disabled.
8/8/2019 Unit II Basic VB
14/84
Property Description
Borderstyle This property determines the borders style for a Form
and also its appearance. It can take any of the
following values:Value Description
0-None No border for form. Form
cannot be resized. This
setting should be avoided.
1-Fixed Single Visible border. Form cannot be
resized.
2- Sizeable Visible border. Form can be
moved and resized.
8/8/2019 Unit II Basic VB
15/84
Property Description
Value Description
3- Fixed dialog For fixed dialog boxes
4- Fixed tool window Form has a closed button
only; cant be sized.
5- Sizeable Tool Window Same as the fixed tool /
window, but the Form can
be resized.
8/8/2019 Unit II Basic VB
16/84
Property Description
Backcolor Specifies the forms background color.
Enabled Determines whether the form is active.
Font Produces a font dialog box in which we can set the
texts font name, style and size
Icon Describes the icon graphic image displayed on the
taskbar when the user minimizes the form
Moveable Specifies whether the user can move the form at
runtime.
Picture Determines the graphic image that appears on the
forms background at runtime.
8/8/2019 Unit II Basic VB
17/84
Setting the startup form
An application generally has more than one form. When an
application starts, the main form that gets loaded first of all iscalled Startup form.
By default, the first form in the application is designated as the
start up form. If we want a different form to display when the
application starts, we must change the startup form.
8/8/2019 Unit II Basic VB
18/84
To change the startup form
1. From the project menu, choose Project properties.
2. Choose general tab3. In start up object list box, select the form we want as the new
startup form.
4. Choose OK.
8/8/2019 Unit II Basic VB
19/84
Loading ,Showing and Hiding forms
A form can be in one of the following states:
(i) Not loaded : Form lives on the disk file and doesnt take upany resources ( such as memory etc.)
(ii) Loaded and hidden: Form is loaded into memory and is
ready to be displayed.
(iii) Loaded and shown : Form is shown and the user can
interact with it.
8/8/2019 Unit II Basic VB
20/84
State of a form can be changed by using Load, Unload, show
and Hide methods.
Loading ,Unloading forms
Load
Unload
8/8/2019 Unit II Basic VB
21/84
The load method does not display the form on the desktop, it
just loads the form in the memory.
The unload method removes a form from the memory and the
resources occupied by it are released so that they can be
used by other applications
8/8/2019 Unit II Basic VB
22/84
Showing Forms
Syntax
< formname>.show []
The argument can take one of the following values:
0 Modeless (Default) allows the user to switch to any
other form of the application
1 Modal A modal form takes control of
the application and does not letthe user work with other parts
of the application unless this
form is closed.
8/8/2019 Unit II Basic VB
23/84
Note : The Show method also loads the form if it is not
loaded already.
Hiding forms
.hide
A hidden form remains in memory and can be displayed instantly
with show method.
8/8/2019 Unit II Basic VB
24/84
Label Control
A label control displays text that user cannot directly change. The
actual text displayed in a label is controlled by its caption property.
MajorProperties of Label control are:
y 1. Caption - the propery used to display text in the label control
y 2. Font the property used to specify the display font, style and size
for the caption of the label
y 3. BackColorand ForeColor- colors of the background and the text
8/8/2019 Unit II Basic VB
25/84
y 4. BackStyle - Opaque or Transparent - whether the background isvisible or not
y 5. Name the property used to name the label control.
y 6. Alignment specifies the alignment of labels caption. Alignment
can be left justified, right justified or centered.
8/8/2019 Unit II Basic VB
26/84
y 7. Wordwrap this property is used to set the word wrapping option
for the caption text. By default, if the text exceeds the width of the
label, the text wraps to the next line. If the text goes beyond the
height of label, then it is clipped. If we set this property to True then
the text is wrapped and expanded vertically.
y 8. Autosize this property causes the control to horizontally expand
and adjust to the size of its contents
y 9. Borderstyle- this property, if set to 1, the label appears with a
border.
8/8/2019 Unit II Basic VB
27/84
Textbox Control
Textboxes are versatile controls that can be used to get input fromthe user or to display text.
Major properties of a textbox control are:
1. Name: property to specify name
2. Text : property to specify text to be displayed in the textbox.This text can be accessed as well as edited.
3. Font the property used to specify the display font, style and size forthe caption of the label
8/8/2019 Unit II Basic VB
28/84
4. Maxlength: property used to set the maximum number of charactersallowed in the textbox
Note : The amount of text that can be placed in a multiline Textboxcontrol is limited approximately to 32 KB. A single line textbox can
hold only 255 characters.
5. Enabled : this property is used to set a value that determines if thetext box can respond to the events that are generated by the user
6.Multiline: this property, allows multiple lines of text in the text box.
7. Scrollbar: this property allows us to attach a built in scrollbar to thetextbox, so that the user can scroll text in the textbox, when theprogram is running.
8/8/2019 Unit II Basic VB
29/84
8. PasswordCharacter: This property is important if we plan to
read a password through textbox. If we specify a character in
this property, then in place of the text being entered, these
characters are displayed.
9. Seltext: property that returns the selected text.
10. Selstart: property that returns or sets the position of the first
character of the selected text.
11. SelLength: property that returns the length of the selected
text.
8/8/2019 Unit II Basic VB
30/84
8/8/2019 Unit II Basic VB
31/84
BackColor
Sets the colour of the CommandButton's background. This
property only takes effect when the Style property is set to 1 -
Graphical.
Cancel
If this property is set to True, pressing the Escape key at runtime
will activate the CommandButton's Click event. Only one
CommandButton on the Form can have this property set to
True.
8/8/2019 Unit II Basic VB
32/84
Default
This property determines if the command button responds to an
Enter keypress even if another control has the focus. The
CommandButton, which has this property set to True, will also
appear with a darker border.
Enabled
When this property is set to False, the user is not able to interact
with the CommandButton in any way e.g. it cannot be clickedon or have focus. The caption will appear greyed out.
8/8/2019 Unit II Basic VB
33/84
Font
In design time, this brings up the Font dialog box to allow us to
choose the font name, style, size and whether the Strikeout
and Underline attributes are set.
Height
Sets the height of the CommandButton.
Left
Sets the X coordinate of the CommandButton.
8/8/2019 Unit II Basic VB
34/84
Top
Sets the Y coordinate of the CommandButton.
VisibleDetermines whether the CommandButton appears on the
Form. I.e. if set to False, the CommandButton will be
unavailable to the user - even by trying to set focus to it with
the tab key.
Width
Sets the width of the CommandButton.
8/8/2019 Unit II Basic VB
35/84
Picture
Sets the picture, which is displayed in the background of the
CommandButton. The Style property needs to be set to 1 -
Graphical for this property to work.
Style
This needs to be set to 1 - Graphical if you want to use the
graphical properties of the CommandButton (apart from
Fonts).
8/8/2019 Unit II Basic VB
36/84
TabIndex
Sets the order in which the controls will get focus when the tab
key is pressed. The control that has 0 as its TabIndex will have
focus when the Form is loaded.
TabStop
If you set this to False, the CommandButton will not get focus
when the tab key is pressed.
Tag
This property can be set to a string if you need to store any extra
information related to the CommandButton.
8/8/2019 Unit II Basic VB
37/84
ToolTipText
When this property is set, the text will popup over the
CommandButton when the user hovers the mouse pointer over
it. This can be useful if we cannot fit all the text you need to in
the Caption.
8/8/2019 Unit II Basic VB
38/84
Assigning access keys
To assign a keyboard access key to a command button, we need
to place an ampersand (&) in front of the letter that is to be
used as access key, while setting the caption property of
command button.
For example, if we want to assign Alt + P as the access key for a
print command button, we can set the caption property to&Print. VB shows this key by underlining it i.e. this caption
appears as Print.
8/8/2019 Unit II Basic VB
39/84
8/8/2019 Unit II Basic VB
40/84
Frame control
A frame control is used to group various controls. A frame
control is a container control , i.e. it contains other controls
in it. It does not carry out any job / action by itself , it does not
respond to any events by itself.
8/8/2019 Unit II Basic VB
41/84
Major properties of frame control
Caption
P
roperty to display text in the frame control
Font
Property to set font, style, and size of captioned text of frame
(Name)
property to name the frame
8/8/2019 Unit II Basic VB
42/84
Visible
Property used to set a value indicating whether the frame should bevisible ( value True) or not (value false)
BorderstyleProperty used to specify the style of border.
0- None
1- Fixed single
Appearance
Property used to set look of the frame
0 Flat
1- 3D
8/8/2019 Unit II Basic VB
43/84
OptionButton Control (Radio Button)
The option buttons are used to offer a small set of options from
which a user can choose one.
Option buttons should always work as part of a group; selecting
one option button immediately clears all the other
buttons in the group.
Option button groups operate in a container control, such as aframe. Therefore, different sets of option button groups should
be placed in their own frame on the form.
8/8/2019 Unit II Basic VB
44/84
Major properties of option button control are :
Caption
P
roperty to display text for option button
Name
Property to name the option button
Value
Property to set the state of option button. If set to True, the
option button appears selected.
8/8/2019 Unit II Basic VB
45/84
Alignment
Property to set the alignment of the option button.
0- Left justify option button appears on the left of caption
1-Right justify option button appears on the right of thecaption.
Style
This property is used to set the appearance style which can either be 0-standard (default) or1- graphical ( it makes option button appearas a command button.
8/8/2019 Unit II Basic VB
46/84
Enabled
Property used to set a value that determines whether a
checkbox can respond to user-generated events
8/8/2019 Unit II Basic VB
47/84
Checkbox control
The checkbox controls are used for offering a small set of
choices from which a user can choose one or more options.
A check box indicates whether a particular condition is on or off.
Checkboxes work independently of each other, a user
can select any number of checkboxes at the same time.
8/8/2019 Unit II Basic VB
48/84
Major properties of checkbox control are :
Caption
Property the specify text to be displayed.
Name
Property to specify name
Alignment
Property to set the alignment of the checkbox.0- Left justify checkbox appears on the left of caption
1-Right justify checkbox appears on the right of thecaption.
8/8/2019 Unit II Basic VB
49/84
Enabled
Property used to set a value that determines whether a
checkbox can respond to user-generated events
Value
Property used to specify the state of checkbox. If its value is :
0 or Unchecked , it means the checkbox is unchecked
1 or Vbchecked it means the checkbox is checked
2 or Vbgrayed it means the checkbox is disabled
.
8/8/2019 Unit II Basic VB
50/84
8/8/2019 Unit II Basic VB
51/84
8/8/2019 Unit II Basic VB
52/84
8/8/2019 Unit II Basic VB
53/84
ListBox Control
The listbox control presents a list of choices to the user. The
choices are presented in one or more columns. If the number
of items exceeds what can be displayed in a list box,
scrollbars automatically appear on the control.
8/8/2019 Unit II Basic VB
54/84
Major properties of listbox control are :
Multiselect : This property determines whether the user can select
the lists multiple items or not. This property must be set at design
time. At runtime we cant change this property, we can only read itsvalue.
It can take following values:
0 multiple selection not allowed, default setting1 Simple multiple selection (mouse click, pressing space bar)
2 Extended multiple selection (Shift key + mouse click/ Ctrl + mouse
click)
8/8/2019 Unit II Basic VB
55/84
Sorted this property determines whether the items inthe list is sorted or not.
( Value True / False)
Style 0 Standard appearance
1 Checkbox appearance
Listindex Gives the index of selected item in the list. I fmultiple items are selected, then listindexproperty stores the index of most recentlyselected items.
8/8/2019 Unit II Basic VB
56/84
Newindex this property returns the index of the item most
recently added to the listbox
8/8/2019 Unit II Basic VB
57/84
Adding items to list at runtime
Syntax
.Additem ,
Deleting items from the list
Syntax
.Removeitem
8/8/2019 Unit II Basic VB
58/84
Clearing the List
.Clear
Accessing items in the list
.list()
Counting the number of items in list
.listcount
8/8/2019 Unit II Basic VB
59/84
Combo Box
It combines the features of a listbox and a textbox.
Combo box is similar to a listbox control it contains multiple
items of which the user may select one, but it takes less
space on screen.
It is an expandable listbox control, which can grow when theuser wants to make a selection and retract after the selection
is made.
8/8/2019 Unit II Basic VB
60/84
8/8/2019 Unit II Basic VB
61/84
Hscrollbar and Vscrollbar controls
A scrollbar is a long stripe with an indicator that lets the user select avalue between the two ends of a control.
The scrollbar comes in two forms :
a) Horizontal scrollbar HScrollbar
b) Vertical Scrollbar VScrollbar
The only difference between the two is their orientation. The left end (in a horizontal scroll bar) and the top end ( in a vertical scroll bar)corresponds to its minimum value and the other end corresponds toits maximum value.
8/8/2019 Unit II Basic VB
62/84
The current value of the scrollbar control is determined by the
position of the indicator ( or scrollbox) which is scrolled
between the minimum and the maximum values.
The basic properties are:
Min this property returns the scrollbars minimum
value
Max this property returns the scrollbars maximum
value
8/8/2019 Unit II Basic VB
63/84
value this property returns the current value of
scrollbar
Largechange specifies the change in value if user clicks on
the scrollbar in an area other than the indicatorand the arrow keys
Smallchange specifies the change in value if the user clicks at
the arrow keys of the scrollbar.
8/8/2019 Unit II Basic VB
64/84
The scroll bar controls use the Scroll and Change events to
monitor the movement of the scroll box along the scroll bar.
Event Description
Change occurs after the scroll box is moved
Scroll occurs as the scroll box is moved. Does
not occur if the scroll arrows or scrollbar
8/8/2019 Unit II Basic VB
65/84
Image Control
Image control is used to display graphics. Image control can
display graphics in the following formats: bitmaps (BMP),
device independent bitmaps (DIB), metafiles (WMF),enhanced metafiles (EMF), GIF and JPEG files, and icons
(ICO) and cursor( CUR) files.
Image controls can be used as a substitute for command buttonsas items in a toolbar.
8/8/2019 Unit II Basic VB
66/84
Some properties of image control
Appearance 0 Flat
1 3D . We can view the result of this property
if we set a border to the image control.
Borderstyle 0 None
1 Fixed Single
Picture this property determines which picture is
displayed in the image box.
8/8/2019 Unit II Basic VB
67/84
8/8/2019 Unit II Basic VB
68/84
Loading graphics into Imagebox Control
Pictures can be loaded into the imagebox control at design time
by selecting the Picture property.
If we want to load or change image at runtime, we can do this by
using the picture property and the Loadpicture function.
E.g.
Image1.Picture= Loadpicture(c:\images\fountain.gif)
8/8/2019 Unit II Basic VB
69/84
To clear the graphics from the image control, we set the picture
property to Nothing;
Image1.Picture= Nothing
Or use the loadpicture function without specifying the file name:
Image1.Picture=Loadpicture
8/8/2019 Unit II Basic VB
70/84
Picturebox control
The picture control is used to display graphics, to act as
container for other controls and display output from graphics
methods or text using the print statement.
Picturebox control is similar to the image control in that each can
be used to display graphics in the application and each
supports the same file formats. The picture box control ,contains additional functionality which the image control does
not for example the ability to act as a container for other
controls and support for graphics methods.
8/8/2019 Unit II Basic VB
71/84
Picture box control can display graphics in the following formats:
bitmaps (BMP), device independent bitmaps (DIB), metafiles
(WMF), enhanced metafiles (EMF), GIF and JPEG files, and
icons (ICO) and cursor( CUR) files.
8/8/2019 Unit II Basic VB
72/84
Some properties of picture box control
Appearance 0 Flat
1 3D . We can view the result of this property
if we set a border to the picture box
control.
Borderstyle 0 None
1 Fixed Single
Picture this property determines which picture is
displayed in the picture box.
8/8/2019 Unit II Basic VB
73/84
Autosize this property determines whether picture box is
automatically resized to display its image
entirely.
True Control is resized to the dimensions ofthe image it conatins.
False Only the part of the image that can fit in
the control is displayed
8/8/2019 Unit II Basic VB
74/84
Loading graphics into Picture box Control
Pictures can be loaded into the picture box control at design time
by selecting the Picture property.
If we want to load or change picture at runtime, we can do this
by using the picture property and the Loadpicture function.
E.g.
picture 1.Picture= Loadpicture(c:\images\fountain.gif)
8/8/2019 Unit II Basic VB
75/84
To clear the graphics from the picture control, we set the picture
property to Nothing;
picture 1.Picture= Nothing
Or use the loadpicture function without specifying the file name:
picture 1.Picture=Loadpicture
8/8/2019 Unit II Basic VB
76/84
Difference between Image box control and picture box control
Image box control is used only to display graphics.
Picture box control can be used to display graphics and can also beused as a container control. It also provides support for graphics
properties and methods. Picture Box controls support all the
properties related to graphic output, including AutoRedraw,
ClipControls, HasDC, FontTransparent, CurrentX, CurrentY, and all
the Drawxxxx, Fillxxxx, and Scalexxxx properties.P
ictureBoxcontrols also support all graphic methods, such as Cls, PSet, Point,
Line, and Circle and conversion methods, such as ScaleX, ScaleY,
TextWidth, and TextHeight.
8/8/2019 Unit II Basic VB
77/84
The sizing behavior of the image control differs from that of
picture box control. It has the Stretch property while the
picture box has as Autosize property.
Stretch property of Image box
this property determines whether the image is resized if the sizeof the imagebox control is different from the picture loaded into
it.
True: image is resized to fill the area of imagebox control.
false: Imagebox control behaves like a picture box control with
Autosize property set to true.
8/8/2019 Unit II Basic VB
78/84
Autosize Property of picture box
this property determines whether picture box is automatically
resized to display its image entirely.
True Control is resized to the dimensions of the image it
conatins.
False Only the part of the image that can fit in the control isdisplayed
8/8/2019 Unit II Basic VB
79/84
The image box control is good for displaying graphics and uses
fewer resources than picturebox controls. The picture box
control provides methods for drawing at runtime and is much
more flexible than image box control.
8/8/2019 Unit II Basic VB
80/84
Timer Control
The Timer control allows you to perform a task at a specified
interval or to wait for a specified length of time.
Following are the facts about Timer control:
It is not visible to the user at run-time
The actions that you want to happen at the specified interval
are coded in the Timer's Timerevent.
8/8/2019 Unit II Basic VB
81/84
We specify the interval at which we want actions to occur, in
the Timer control's Interval property. The value is specified in
milliseconds (1000 milliseconds = 1 second). The timer
control triggers a timer event after each interval of time goes
by.
The interval property contains a value that must range from 1to
65535.
8/8/2019 Unit II Basic VB
82/84
We turn the timer "on" or "off" by setting its Enabled property
to True or False, respectively. The Enabled property is set to
True by default. Also setting the interval property to 0 disables
the timer control.
8/8/2019 Unit II Basic VB
83/84
Program for Blinking text
The Timer control can be used to make an object on your screenappear to blink. This is done by toggling the Visible property
of the object on and off at short intervals. The Visible propertyis available to nearly all controls. It is a Boolean property thatdetermines whether or not the control is visible to the user atrun time by setting its value to True or False To build a simpledemo, perform the following steps.
Start a new VB project.
Place a label on the form and name it lblProcess. Set its Captionto Processing . . ..
8/8/2019 Unit II Basic VB
84/84
Set it to a large font and set its Forecolor property to the color of
your choice. Center it on your form.
Place a timer on the form and name it tmrBlink. Set its Interval
property to 250.
In the tmrBlink_Timerevent, place the following single line ofcode:
lblProcess.Visible = Not lblProcess.Visible
Run the project. The label will blink