173004675859_Faculty E Notes Unit 1 Introduction To Visual Basic
173004675859_Faculty E Notes Unit 1 Introduction To Visual Basic
Faculty E- Notes
1
TABLE OF CONTENTS
S.No. Topic Page No.
1. Introduction to Visual Basic 3
2. Procedural Oriented Programming 6
3. Object Oriented Programming 7
4. Difference between Procedural and Object-Oriented 8
Programming
5. Event Driven Programming 9
6. Visual Basic Integrated Development Environment 10-26
2
1. Introduction to Visual Basic
Visual Basic is a third-generation event-driven programming language first released by
Microsoft in 1991. It evolved from the earlier DOS version called
BASIC. BASIC means Beginners' All-purpose Symbolic Instruction Code. Since then,
Microsoft has released many versions of Visual Basic, from Visual Basic 1.0 to the
final version Visual Basic 6.0.
Note:
• Visual Basic is a tool that allows you to develop Windows (GUI)
applications.
• The Visual part in Visual Basic refers to the method used to
create GUI.
• The Basic part in Visual Basic refers to the “BASIC” language.
VB is much enhanced version of the BASIC programming
language. VB contains large number of statements, built-in
functions and keywords, many of which directly relate to the
Windows GUI.
• VB is one of the first programming languages which
incorporates WYSIWYG, i.e.,
What You See is What You Get environment.
• VB supports Event Driven Programming.
3
1.1Visual Programming and Non-Visual Programming
Visual programming is a way of creating software programs or applications by using
visual components like buttons, checkboxes, dialog boxes, forms, etc. This approach
allows users to drag and drop pre-built components and connect them together to create
an application.
4
1.2Features of Visual Basic
a) Easy to learn and use: The syntax is so simple that one can easily use and understand
the commands.
b) Built-in capability to create User Interface: User interface can be easily created by
using the tools available with VB.
c) Quick development: Programming time is reduced as compared to other command
line programming languages because VB has got built-in capability to create user
interface.
d) Event Driven Programming: VB supports event driven programming.
e) Common Programming Platform: VB provides a common programming platform
across all MS-Office applications.
f) Wizards: VB provides many Wizards that can automate tasks or even automate coding.
g) N-Tier Architecture: A traditional client/server model has two layers. In two tier
architecture, there is no middle layer of application between the client and the server.
However, if there are some layers between client and server, then, such an architecture
is known as n-tier architecture. VB supports n-tier architecture.
h) Quick Error Detection/Correction: The VB development environment provides tools
for quick editing, testing and debugging.
i) Database Support: VB provides database connectivity support for most popular
databases like Microsoft SQL Server and other enterprise level database.
j) ActiveX Support: ActiveX technologies allow to use the functionality provided by
other applications, such as, Microsoft Word, Microsoft Excel spreadsheet and other
Windows applications.
k) Internet Features: Internet capabilities make it easy to provide access to documents
and applications across the Internet from within your application.
l) Object Oriented Features: VB supports all object-oriented language features like
classes, encapsulation, data hiding, etc.
5
m) Both Interpreted and Compiled: VB is both interpreted and compiled. You can test a
VB program by running it interpretively until you debug it completely and then
compile it for faster execution when all the bugs are eliminated.
3. Object-Oriented Programming
The major motivating factor in the invention of object-oriented approach is to remove
some of the flaws encountered in the procedural approach. OOP treats data as a critical
element in the program development and does not allow it to flow freely around the
system.
OOP approach binds the data and the functions that operate on that data into a single
entity. Such an entity is called an object. It ties data more closely to the function that
operate on it, and protects it from accidental modification from outside function. OOP
allows decomposition of a problem into a number of entities called objects and then
builds data and function around these objects.
7
Object-oriented program typically consists of a number of objects which communicate
with each other by calling one another’s member functions. Calling an object’s member
function is also referred to as sending a message to the object.
Some Characteristics of Object-Oriented Programming are:
▪ Emphasis is on data rather than procedure.
▪ Programs are divided into what are known as objects.
▪ Data structures are designed such that they characterize the objects.
▪ Functions that operate on the data of an object are tied together in the data structure.
▪ Data is hidden and cannot be accessed by external function.
▪ Objects may communicate with each other through function.
▪ New data and functions can be easily added whenever necessary.
▪ Follows bottom-up approach in program design.
Adding new data and functions is not Adding new data and function is
easy. easy.
8
Procedural Oriented Programming Object-Oriented Programming
9
Procedural Oriented Programming Object-Oriented Programming
10
The Visual Basic environment consists of eight main windows which are listed as
follows
a- Menu Bar: It contains a standard command like: File, Edit, View, Window, Help
menus, and specific command such as: Project, Format, or Debug menus.
b- Toolbar: It contains the most commonly used commands (button), if clicked an
action represented by that button is carried out.
c- ToolBox: It contains a collection of tools that are needed for project design.
11
f- Project Explorer Window: It is a list of the forms and modules for the current
projects. lt is a hierarchical tree- branch structure, where the project lies at the
top of tree and other parts like forms, modules descend from this tree.
g- Form Layout Window: The Form Layout window is a small screen. Which is
used to reposition the form of the application so that it appears in proper place
when project is run.
h- Code Editor Window: It is used to write a VB code for an application. For each
form there is a separate code editor window. It is displayed when user clicks on
form or object in form.
6.1 Menu Bar:
The Menu Bar in Visual Basic refers to the horizontal strip of options that contains
various commands and functions. It is an essential component of the Visual Basic
integrated development environment (IDE) that allows developers to navigate and
access different tools and features.
12
Commands for debugging and stepping through
Debug
code.
Commands for testing code and displaying user
Run
forms.
Commands for accessing additional references and
Tools
user options.
Commands for loading and unloading additional
Add-Ins
COM add-ins.
Commands for quickly switching between all the
Window
open windows.
Help Commands for accessing the built-in help files.
13
6.2 Toolbox
It is a collection of pre-built controls, components, and tools that simplify the
process of creating applications. From buttons and text boxes to menus and dialog
boxes, the toolbox provides a wide range of elements that can be easily dragged and
dropped onto the interface, saving time and effort for developers.
Control Description
Pointer Used to interact with controls on the form (resize
them,move them, etc.). The pointer is not a control
PictureBox A control that displays images or prints the result.
Label A control that displays un-editable text to the user.
TextBox A control for accepting user input. Textbox can also
display text.
Frame A control for grouping other controls.
CommandButton A control that represents a button. The user presses or
clicks to initiate an action.
CheckBox A control that provides the user with a toggle choice
(checked or unchecked)
OptionButton Option buttons are used in groups where only one at a time
can be true.
ListBox A control that provides a list of items.
ComboBox A control that provides a short list of items.
HscrollBar A horizontal scrollbar.
14
VscrollBar A vertical scrollbar.
Timer A control that performs a task at programmer specified
intervals. A timer is not visible to the user.
DrivelistBox A control accessing the system disk drivers.
DirlistBox A control accessing directories on a system
Filelistbox A control accessing file in a directory
Shape A control for drawing circles, rectangles, squares or
ellipse
Line A control for drawing line.
Image A control for displaying images. The images control
does not provide as many capabilities as a picturebox.
OLE A control for interacting with another window
applications.
i. The Label
The label is for providing instructions and guides to the users, as well as for
displaying 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.
ii. The Command Button
The command button is used for executing a procedure triggered by an event
initiated by the users. 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
iii. The PictureBox
The Picture Box is one of the controls that is used to handle graphics. One 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. One can also load the picture at runtime
using the LoadPicture method. For example, the statement will load the picture
grape.gif into the picture box.
15
Picture1.Picture=LoadPicture ("C:\VBprogram\Images\grape.gif")
iv. The Image Control
The Image Control is another control that handles images and pictures. It functions
almost identically to the pictureBox. 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 PictureBox. Similar to the Picture Box, it can also use the
LoadPicture method to load the picture.
v. The ListBox
The function of the ListBox 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. 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 third item has a ListIndex 2 and more.
vi. The ComboBox
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 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.
vii. The CheckBox
The Check Box control lets the user selects or unselects 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 to unmark the Check Box.
viii. The OptionButton
The OptionButton control also lets the user selects one of the choices. However, two
or more Option buttons must work together because when one of the option buttons
is selected, the other Option button will be unselected. In fact, only one Option Box
16
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”.
ix. The Shape Control
In the following example, the shape control is placed in the form together with six
OptionButtons. To determine the shape of the shape control, we use the shape
property. The property values of the shape control are 0, 1, and 2,3,4,5 corresponding
to a rectangle, a square, an oval shape, a circle, a rounded rectangle and a rounded
square respectively.
x. The DriveListBox
The DriveListBox is for displaying 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.
xi. The DirListBox
The DirListBox means the Directory List Box. It is for displaying a 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.
6.3 ToolBar
Contains several icons that provide quick access to commonly used features.
17
6.4 Form Designer Window:
This window contains a form, which is where the program’s Graphical User
Interface (GUI) will be displayed. A GUI is the visual portion of the program, this is
where the user enters data (called inputs) to the program and where the program
displays its results (called outputs). Forms are the foundation for creating the
interface of an application. You can use the forms to add windows and dialog boxes
to your application. You can also use them as container for items that are not a
visible part of the application’s interface. For example, you might have a form in
your application that serves as a container for graphics that you plan to display in
other forms.
Visual Basic Form is the container for all the controls that make up the user
interface. Every window you see in a running visual basic application is a form,
thus the terms form and window describe the same entity. Visual Studio creates
18
a default form for you when you create a Windows Forms Application.
Form Properties
Following table lists down various important properties related to a form. These
properties can be set or read during application execution. You can refer to
Microsoft documentation for a complete list of properties associated with a Form
control –
19
Sl.No Properties Description
1 AcceptButton The button that's automatically activated when
you press Enter, no matter which control has
the focus at the time. Usually the OK button
on a form is set as
AcceptButton for a form.
2 CancelButton The button that's automatically activated
when you hit the Esc key.
Usually, the Cancel button on a form is set as
CancelButton for a form.
3 AutoScale This Boolean property determines whether the
controls you place on the form are
automatically scaled to the height of the
current font. The default value of this
property is True. This is a property of the form,
but it affects the controls on the form.
4 AutoScroll This Boolean property indicates whether scroll
bars will be automatically attached to the form
if it is resized to a point that not all
its controls are visible.
5 AutoScrollMinSize This property lets you specify the minimum
size of the form, before the scroll bars are
attached.
6 AutoScrollPosition The AutoScrollPosition is the number of
pixels by which the two scroll bars were
displaced from their initial locations.
7 BackColor Sets the form background color.
20
8 BorderStyle The BorderStyle property determines the style
of the form's border and the appearance of
the form −
• None − Borderless window that can't be
resized.
• Sizable − This is default value and
will be used for resizable window that's
used for displaying regular forms.
• Fixed3D − Window with a visible
border, "raised" relative to the main
area. In this case, windows can't be
resized.
• FixedDialog − A fixed window, used
to create dialog boxes.
• FixedSingle − A fixed window with a
single line border.
• FixedToolWindow − A fixed window
with a Close button only. It looks like the
toolbar displayed by the drawing and
imaging applications.
• SizableToolWindow − Same as the
FixedToolWindow but resizable. In
21
addition, its caption font is smaller than
the usual.
9 ControlBox By default, this property is True and you can
set it to False to hide the icon and
disable the Control menu.
10 Enabled If True, allows the form to respond to
mouse and keyboard events; if False,
disables form.
11 Font This property specify font type, style, size
12 HelpButton Determines whether a Help button should be
displayed in the caption box of the form.
13 Height This is the height of the Form in pixels.
14 MinimizeBox By default, this property is True and you can
set it to False to hide the Minimize button on
the title bar.
]15 MaximizeBox By default, this property is True and you can
set it to False to hide the Maximize
button on the title bar.
16 MinimumSize This specifies the minimum height and
width of the window you can minimize.
17 MaximumSize This specifies the maximum height and
width of the window you maximize.
18 Name This is the actual name of the form.
19 StartPosition This property determines the initial position of
the form when it's first displayed. It will have
any of the following values −
• CenterParent − The form is centered in
the area of its parent form.
• CenterScreen − The form is centered on
the monitor.
• Manual − The location and size of the
form will determine its starting position.
• WindowsDefaultBounds − The form is
positioned at the default location and
size determined by Windows.
• WindowsDefaultLocation − The form is
positioned at the Windows default
location and has the dimensions
you've set at design time.
20 Text The text, which will appear at the title bar
of the form.
21 Top, Left These two properties set or return the
coordinates of the form's top-left corner in
pixels.
22
22 TopMost This property is a True/False value that lets you
specify whether the form will remain on top of
all other forms in your application. Its
default property is False.
23 Width This is the width of the form in pixel.
Properties Objective
name
Name Used to represent
name of object in code.
Caption Name appears
on object.
Back Background color
color for object.
Fore Color of text written
color on object.
Font Font style type and
size
Visible The tool is visible
or invisible.
23
Enable The tool enable
or disable
Height Length of object
24
6.7 Form Layout Window
The Form Layout window specifies a form’s position on the screen at
runtime. The Form Layout window consists of an image representing
the screen and the form’s relative position on the screen. With the
mouse pointer positioned over the form image, drag the form to a new
location.
25
the control is drawn.
To move a control you have drawn, click the object in the form window
and drag it to the new location. Release the mouse button.
To resize a control, click the object so that it is select and sizing handles
appear.
26