Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
102 views

Introduction To Visual Basic

The document outlines a course on visual basic programming. The aim is to equip learners with knowledge and skills to develop programs using visual basic. By the end of the course, learners should be able to apply programming skills in visual basic, understand various data types and structures, develop object-oriented programs, and link visual programs to databases. The course will be taught over 12 weeks covering topics like visual programming, program structures, control structures, error handling, sub-programs, and linking to databases. Learners will be evaluated based on assignments, presentations, and a final exam.

Uploaded by

Matthew Ndeto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Introduction To Visual Basic

The document outlines a course on visual basic programming. The aim is to equip learners with knowledge and skills to develop programs using visual basic. By the end of the course, learners should be able to apply programming skills in visual basic, understand various data types and structures, develop object-oriented programs, and link visual programs to databases. The course will be taught over 12 weeks covering topics like visual programming, program structures, control structures, error handling, sub-programs, and linking to databases. Learners will be evaluated based on assignments, presentations, and a final exam.

Uploaded by

Matthew Ndeto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

COURSE OUTLINE; LDIT 2116

VISUAL BASIC

Aim

To equip the learners with appropriate knowledge and skills on developing running programs using the
visual basic.

Learning Outcomes

By the end of the unit, a learner should be able to:

1. Apply programming skills in visual basic

2. Understand the various data types, control structures and data structures used in object

oriented programming

3. Develop object oriented programs

4. Linking visual program with database

Methodology

Lectures, Brainstorming, Individual and group assignments, practical

Learning Resources

Whiteboard, computers and Resources, Journals and books,Practicals.

Follow the link below to download visual basic 2010software

https://microsoft-visual-basic-2010-express.software.informer.com/download/#downloading

Course Evaluation

Cats/ Assignments/ Presentation 30%

Final Examination 70%

Total 100%
WEEK 1

VISUAL BASIC PROGRAMMING

1. Visual programming

2. Example of visual programming languages

3. Hardware and software considerations for visual programming

WEEK 2

VISUAL ENVIRONMENT

1. Description of visual environment

2. Integrated development environment

3. Visual objects

WEEK 3

PROGRAM STRUCTURE

1. Format of a visual program

2. Data types

3. Operators

4. Variables

WEEK 4

CONTROL STRUCTURES

1. Types of control structures

Implementation of control structures

WEEK 5 CAT ONE


WEEK 6

PROGRAM WRITING

1. Creating an application

WEEK 7

2. Compilation

3. Debugging

WEEK 8

4. Testing

5. Execution

WEEK 9

CONTROL STRUCTURES

2. Types of control structures

3. Implementation of control structures

WEEK 10 CAT 2

WEEK 11

ERROR HANDLING

1. Types of errors

2. Error handling techniques

WEEK 12

SUB-PROGRAMS

1. Meaning of subprograms

2. Types of subprograms

3. Scope of variables
WEEK13

LINKING TO DATABASES

1. Database controls

2. Reports

EMERGING TRENDS IN VISUAL PROGRAMMING

1. Emerging trends in visual programming

2. Challenges of emerging trends in visual programming


INTRODUCTION TO VISUAL BASIC
A brief description of Visual Basic
VISUAL BASIC is a high level programming language evolved from the earlier DOS version
called BASIC.
BASIC means Beginners' All-purpose Symbolic Instruction Code. It is a fairly easy programming
language to learn. The codes look a bit like English Language. Different software companies
produced different version of BASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC, and
IBM BASICA and so on.
VISUAL BASIC is a VISUAL and events driven Programming Language. These are the main
divergence from the old BASIC. In BASIC, programming is done in a text-only environment and
the program is executed sequentially. In VISUAL BASIC, programming is done in a graphical
environment. Because users may click on a certain object randomly, so each object has to be
programmed indepently to be able to response to those actions (events).Therefore, a VISUAL
BASIC Program is made up of many subprograms, each has its own program codes, and each can
be executed indepently and at the same time each can be linked together in one way or another.
Visual Basic is an excellent platform for writing a client or web application that accesses a
database, It is Microsoft’s proprietary programming language, Because it is a Microsoft product, it
is difficult to move the programs written in Visual Basic to other kinds of operating systems.

Why use Visual Basic?

1. You can develop Windows-based applications and the games with Visual Basic, and you can find
the answers to your programming problems much more easily than other programming languages.
2. It is easy to use and easy to learn, and it is so popular.
3. Visual basic provides a comprehensive interactive and context-sensitive online help system, and the
structure of the basic programming language is very simple, it is not only a language but primarily
an integrated, interactive development environment (IDE).
4. Visual Basic IDE has been highly optimized to support rapid application development (RAD), it is
particularly easy to develop graphical user interfaces and to connect them to handler functions
provided by the application.
5. The graphical user interface of the VB-IDE provides intuitively appealing views for the
management of the program structure in the large and the various types of entities (classes,
modules, procedures, forms,).
6. Visual Basic is a programming language offering general ease of use combined with ease of
implementing a graphical user interface, it is relatively simplistic and therefore limited in function
compared to more advance.

Creating a simple VB project


Creating event-driven programs in VB requires approach different from that used in procedural
languages. If you are experienced with procedural programming, the approach may seem unusual at
first. Both types of programming require logic and order; VB simply requires a different type of
logic and order. For example to create an interface in VB, you draw the object and controls on
blank form. Compared to the programming effort required to produce graphical objects in most
procedural languages, the actual coding needed to provide functionality in VB minimal.
Creating an application in visual basic involves eight basic steps:
1. Creating a design plan
The design plan should be the roadmap you use when creating an application. Take time
before writing any code to design the application you will be building. Although Visual
Basic provides tools to help you quickly develop a solution, having a clear understanding of
the user needs and initial feature s set will help you be more efficient in your development
efforts. This will also help you save time by minimizing the potential for re-coding due to
poor or non-existent design plan.

2. Create the user interface


To create the interface for your application, first place controls and objects on a form by
drawing or painting them in the Form Designer. You can look at other applications such as
Microsoft Excel or Microsoft Word for ideas on how to design the interface.

3. Set the properties of interface object


Once you have added objects to a form, you can set their properties at design time or you
can use code statements to set properties at run time.

4. Write code for events


After you have set the initial properties for the form and its object, you can add code that
run in response to events. Events occur when different actions are performed on a control or
object. For examples, a command button’s Click event occurs when the user clicks it with
the mouse.
5. Save the project
Next, save the project, giving it a unique and descriptive name. You should save the project
frequently as you add code to it. Saving a project also saves its forms and code modules.

6. Test and debug the application


As you add code to the project, you can run the application and view its behavior. Visual
basic also offers numerous tools for debugging your application.

7. Make an executable file


Upon completion of the project, you’ll create an executable file. This compiles the various
files making up the program into a stand-alone executable file.

8. Create a setup application


In order to run your application, the user usually needs other files, such as any DLL files or
custom control .ox files you used to create your application. Visual Basic provides the
Package & Development Wizard, which automates the creation of the setup program and
ensures the user has all of the necessary files
Starting Visual Basic

The Visual Basic startup dialog box


To start creating your first application, you need to click on the New Project. After clicking on the
new project, the following New Project dialog box will appear,

The dialog box offers you five types of projects that you can create. As we are going to learn to
create Windows Applications, we will select Windows Forms Application.

At the bottom of this dialog box, you can change the default project name WindowsApplication1 to
some other name you like, for example, myFirstProgram. After you have renamed the project, click
OK to continue.
THE VB INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)

One of the most significant changes in Visual Basic is the Integrated Development Environment
(IDE). IDE is a term commonly used in the programming world to describe the interface and
environment that we use to create our applications. It is called integrated because we can access
virtually all of the development tools that we need from one screen called an interface. The IDE is
also commonly referred to as the design environment, or the program.

Example of visual basic IDE


VISUAL BASIC IDE COMPONENTS

The Visual Basic IDE is made up of a number of components

 Menu Bar
 Tool Bar
 Project Explorer
 Properties window
 Form Layout Window
 Toolbox
 Form Designer
 Object Browser

In previous versions of Visual Basic, the IDE was designed as a Single Document Interface (SDI).
In a Single Document Interface, each window is a free-floating window that is contained within a
main window and can move anywhere on the screen as long as Visual Basic is the current
application. But, in the latest versions of Visual Basic the IDE is in a Multiple Document Interface
(MDI) format. In this format, the windows associated with the project will stay within a single
container known as the parent. Code and form-based windows will stay within the main container
form.

Menu Bar

This Menu Bar displays the commands that are required to build an application. The main menu
items have sub menu items that can be chosen when needed. The toolbars in the menu bar provide
quick access to the commonly used commands and a button in the toolbar is clicked once to carry
out the action represented by it.

Toolbox
The Toolbox contains a set of controls that are used to place on a Form at design time thereby
creating the user interface area. Additional controls can be included in the toolbox by using the
Components menu item on the Project menu. A Toolbox is represented in figure 2 shown below.

Toolbox window with its controls available commonly.


Control Description
Pointer Provides a way to move and resize the controls form
Displays icons/bitmaps and metafiles. It displays text or acts as a
Picture Box
visual container for other controls.
Textbox Used to display message and enter text.
Frame Serves as a visual and functional container for controls
Command
Used to carry out the specified action when the user chooses it.
Button
Checkbox Displays a True/False or Yes/No option.
Option Button control which is a part of an option group allows the
Option Button
user to select only one option even it displays multiple choices.
List Box Displays a list of items from which a user can select one.
Combo Box Contains a Textbox and a List Box. This allows the user to select an
item from the dropdown List Box, or to type in a selection in the
Textbox.
Scrollbar and These controls allow the user to select a value within the specified
Scrollbar range of values
Timer Executes the timer events at specified intervals of time
Displays the valid disk drives and allows the user to select one of
DriveListBox
them.
Allows the user to select the directories and paths, which are
DirListBox
displayed.
FileListBox Displays a set of files from which a user can select the desired one.
Shape Used to add shape (rectangle, square or circle) to a Form
Line Used to draw straight line to the Form
Used to display images such as icons, bitmaps and metafiles. But less
Image
capability than the Picture Box
Enables the use to connect to an existing database and display
Data
information from it.
Used to link or embed an object, display and manipulate data from
OLE
other windows based applications.
Label Displays a text that the user cannot modify or interact with.

Project Explorer

Docked on the right side of the screen, just under the toolbar, is the Project Explorer window. The
Project Explorer as shown in in figure serves as a quick reference to the various elements of a
project namely form, classes and modules. All of the object that make up the application are packed
in a project. A simple project will typically contain one form, which is a window that is designed as
part of a program's interface. It is possible to develop any number of forms for use in a program,
although a program may consist of a single form. In addition to forms, the Project Explorer window
also lists code modules and classes.

Figure 3 Project Explorer


Properties Window

The Properties Window is docked under the Project Explorer


window. The Properties Window exposes the various characteristics
of selected objects. Each and every form in an application is
considered an object. Now, each object in Visual Basic has
characteristics such as color and size. Other characteristics affect not
just the appearance of the object but the way it behaves too. All these
characteristics of an object are called its properties. Thus, a form has
properties and any controls placed on it will have properties too. All
of these properties are displayed in the Properties Window.
Object Browser

The Object Browser allows us to browse through the various properties, events and methods that are
made available to us. It is accessed by selecting Object Browser from the View menu or pressing the
key F2. The left column of the Object Browser lists the objects and classes that are available in the
projects that are opened and the controls that have been referenced in them. It is possible for us to
scroll through the list and select the object or class that we wish to inspect. After an object is picked up
from the Classes list, we can see its members (properties, methods and events) in the right column.

A property is represented by a small icon that has a hand holding a piece of paper. Methods are
denoted by little green blocks, while events are denoted by yellow lightning bolt icon.

Object naming conversions of controls (prefix)

Form -form
Label-lb.
Textbox-txt
CommandButton-cmd
CheckBox -chk
OptionButton -opt
ComboBox -cbo
ListBox-lst
Frame-fme
PictureBox -pic
Image-img
Shape-shp
Line -lin
HScrollBar -hsb
VScrollBar -vsb
FORM Modules

Code in Visual Basic is stored in the form of modules. The three kind of modules are Form
Modules, Standard Modules and Class Modules. A simple application may contain a single Form,
and the code resides in that Form module itself. As the application grows, additional Forms are
added and there may be a common code to be executed in several Forms. To avoid the duplication
of code, a separate module containing a procedure is created that implements the common
code.This is a standard Module.

Class module (.CLS filename extension) are the foundation of the object oriented programming in
Visual Basic. New objects can be created by writing code in class modules. Each module can
contain:

Declarations : May include constant, type, variable and DLL procedure declarations.

Procedures : A sub function, or property procedure that contain pieces of code that can be executed
as a unit.

These are the rules to follow when naming elements in VB - variables, constants, controls,
procedures, and so on:

 A name must begin with a letter.


 May be as much as 255 characters long (but don't forget that somebody has to type the
stuff!).
 Must not contain a space or an embedded period or type-declaration characters used to
specify a data type; these are ! # % $ & @
 Must not be a reserved word (that is part of the code, like Option, for example)
 The dash, although legal, should be avoided because it may be confused with the minus
sign. Instead of First-name use First_name or FirstName.
Data types in Visual Basic

By default Visual Basic variables are of variant data types. The variant data type can store numeric,
date/time or string data. When a variable is declared, a data type is supplied for it that determines
the kind of data they can store. The fundamental data types in Visual Basic including variant are
integer, long, single, double, string, currency, byte and boolean. Visual Basic supports a vast array
of data types. Each data type has limits to the kind of information and the minimum and maximum
values it can hold. In addition, some types can interchange with some other types. A list of Visual
Basic's simple data types are given below.

1. Numeric

Byte Store integer values in the range of 0 – 255


Integer Store integer values in the range of (-32,768) - (+ 32,767)
Long Store integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468)
Single Store floating point value in the range of (-3.4x10-38) - (+ 3.4x1038)
Double Store large floating value which exceeding the single data type value
store monetary values. It supports 4 digits to the right of decimal point
Currency
and 15 digits to the left

2. String

Use to store alphanumeric values. A variable length string can store approximately 4 billion
characters

3. Date

Use to store date and time values. A variable declared as date type can store both date and time
values and it can store date values 01/01/0100 up to 12/31/9999
4. Boolean

Boolean data types hold either a true or false value. These are not stored as numeric values and
cannot be used as such. Values are internally stored as -1 (True) and 0 (False) and any non-zero
value is considered as true.

5. Variant

Stores any type of data and is the default Visual Basic data type. In Visual Basic if we declare a
variable without any data type by default the data type is assigned as default.

Visual Basic Variables


Variables are like mail boxes in the post office. The contents of the variables changes every now
and then, just like the mail boxes. In term of VB, variables are areas allocated by the computer
memory to hold data. Like the mail boxes, each variable must be given a name. To name a variable
in Visual Basic,you have to follow a set of rules.
Variable Names
The following are the rules when naming the variables in Visual Basic
 It must be less than 255 characters
 No spacing is allowed
 It must not begin with a number
 Period is not permitted
Examples of valid and invalid variable names
Valid Name Invalid Name
My_Car My.Car
ThisYear 1NewBoy
Declaring Variables
In Visual Basic, one needs to declare the variables before using them by assigning names and data
types. They are normally declared in the general section of the codes' windows using the Dim
statement.
The syntax is as follows:

Dim VariableName as DataType


Examples
Dim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim doDate As Date
You may also combine them in one line , separating each variable with a comma, as follows:
Dim password As String, yourName As String, firstnum As Integer,
If data type is not specified, VB will automatically declares the variable as a Variant.
For string (data type ) declaration, there are two possible format, one for the variable-length
string and another for the fixed-length string. For the variable-length string, just use the same
format ,However, for the fixed-length string, you have to use the format as shown below:
Dim Variable Name as String * n, where n defines the number of characters the string can hold.
Dim yourName as String * 10
yourName can holds no more than 10 Characters.
Working with Variables
Assigning Values to Variables
After declaring various variables using the Dim statements, we can assign values to those variables.
The general format of an assignment is
Variable=Expression
The variable can be a declared variable or a control property value. The expression could be a
mathematical expression, a number, a string, a boolean value(true or false) and etc.

The following are


some examples:
firstNumber=100
secondNumber=firstNumber-99
userName="John Lyan"
userpass.Text = password
Label1.Visible = True
Command1.Visible = false
Label4.Caption = textbox1.Text
ThirdNumber = Val(usernum1.Text)
total = firstNumber + secondNumber+ThirdNumber

SCOPE OF VARIABLES

The scope of a variable determines the accessible range of a defined variable at the time of
declaration in any block, module, and class for example, when you declare a variable within a
procedure, only that code within that procedure can access or change the variables value. The scope
is limited to the procedure containing the variable. There are three variable scopes in Visual Basic

1. Local variable

2. Module variable
3. Public variable

1. local variable

A local variable is a type of variable defined within a procedure scope, block, or function. It is
available with a code inside the procedure, and it can only be visible to that specific procedure.local
variable be declared using the Dim or static statement. Its syntax is:

Dim Varname [As type] e.g Dim X As Integer  

These variables are not accessible from outside of the local method. However, the local variable can
be easily accessed by the nested programming function in the same method.
2. Module/Form variable

All existing procedures can easily identify a variable that is declared inside a module sheet is called
a module-level variable. The defined module variable is visible to all procedures within that
module or form only, but it is not available for other module's procedures. The Dim or private
statement at the top of the first procedure declaration can be declared the module-level variables. It
means that these variables cannot be declared inside any procedure block. Further, these variables
are useful to share information between the procedures in the same module. And one more thing
about the module-level variable is that these variables can remains existence as long as the module
is executed.it can be declared using either Dim or Private keywords.

its syntax is:

Dim Varname [As type] or Private Varname [As type]

e.g Private X As Integer

3. Public variable/global variable

As the name defines, a public variable is a variable that is used to access the variables globally in a
program. It means these variables can be accessed by all the procedures or modules available in a
program. To access the variables globally in a program, you need to use the friend or public
keyword with a variable in a module or class at the top of the first procedure function. Global scope
is also known as the Namespace scope.it is declared using the Public Keyword.Its syntax is

Public Varname [As type] e.g Public x As integer


VISUAL BASIC OBJECTS,METHODS,EVENTS

1. Objects

An object in a VB program is a component of the user interface, such as a form (which represents
the window in which the program executes) or a control (a button, list, checkbox, textbox, or some
other component that is contained in the form). Every object has properties which define the
characteristics of the object. One property that every object has is (Name) -- the name of the object.
Every object is given a default name when you create it, but we always change the names, using a
style of naming that helps us remember the type of the object. We start the name with an
abbreviation of the object type:

Type of Object Name Prefix Example

Button btn btnClear

Label lbl lblTitle

text box txt txtName

2. Events

While a program is running, the user can do many things, such as move the mouse, click the mouse
button, enter text, or move to a different window. Each of these actions is called an event. Each
event has two components: what the user did (click, type, etc.) and on which object did they
perform the event (which button did they click on, which textbox did they type into). In Visual
Basic you can write code that will perform actions in response to events. Some examples of events
are:

Common Events of Visual Basic Controls


Event Occurrence
Change The user modifies text in a combo box or text box.
Click The user clicks the primary mouse button on an object.
DblClick The user double-clicks the primary mouse button on an object.
DragDrop The user drags an object to another location.
DragOver The user drags an object over another control.
GotFocus An object receives focus.
KeyDown The user presses a keyboard key while an object has focus.
KeyPress The user presses and releases a keyboard key while an object has focus.
KeyUp The user releases a keyboard key while an object has focus.
Event Occurrence
LostFocus An object loses focus.
MouseDow The user presses any mouse button while the mouse pointer is over an object.
n
MouseMove The user moves the mouse pointer over an object.
MouseUp The user releases any mouse button while the mouse pointer is over an object.

Event Procedures procedures. Each event procedure contains the statements that execute when a
particular event occurs on a particular object. Event procedure names have the
form objectName_event, where objectName is the name of the object on which the event occurred
and event is the type of the event. For example, an event procedure named cmdClear_Click will be
executed when the user clicks on the button named cmdClear.

The event procedures are written in the code window. To open the code window, double click on
the control for which you want to write the event procedure. The code window will open and it will
contain the shell of the event procedure for that object:

Private Sub btn Clear_Click(...) Handles btnClear.Click

End Sub

The code that performs actions in response to events is written in event

Methods
A method causes an object to perform an action or task. Methods are actions that you want to
perform. For example, Add is a method of the Combo Box object, because it adds a new entry to a
combo box.
Its Syntax is:
Object. Method (arg1, arg 2,)
Example
Sub Add Entry (new Entry as String)
Combo.Add new Entry
End Sub

NB: The above procedure uses the Add method to add a new item to a Combo Box.

You might also like