Unit 1 Visual Programming
Unit 1 Visual Programming
UNIT – I
Getting Started with VB6,Programming Environment, Working with Forms, Developing an
application, Variables, Data types and Modules, procedures and control structures, arrays.
Working with Controls: Creating and using controls, working with control arrays.
One of the most significant changes in Visual Basic 6.0 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”.
Menu Bar
The second line is called a Menu Bar, selecting one of the choices(File, Edit, view,
Project, Format,… Help) causes one of Visual Basic‟s drop-down menus to appear.
The menus present logical groupings of Visual Basic‟s individual features.
Fig (1) Visual basic environment
Toolbar
The third Line is called a Standard Toolbar.
The icons on this line duplicate several of the more commonly used menu selections
that are available via the drop-down menus accessed from the Menu Bar,
For example, the standard toolbar contains the icons that will open an existing project; Save
the current project, cut, copy and delete, undo the most recent changes; start, pause and end
program execution; and add/delete windows from the current overall environment
Project Window
The project window displays a hierarchical list of the files associated with a given
project. These files represent individual forms and modules.
The user can display a form or module within the Project Container Window by
double-clicking on the corresponding icon within the Project Window.
The user can select either the Object View or the Code view by clicking on one of the
two leftmost icons within the toolbar at the top of the Project Window.
Properties Window
Every object has properties associated with it. Each object has its unique list of
Properties.
The properties window allows the user to assign or change the properties associated
with a particular object.
Active the object by clicking on it; then choose from the corresponding list of
properties shown in the left column of the properties window.
Once a property is selected, the adjoining box in the right column may change its
appearance, showing a drop-down menu so the user can choose from a list of
permissible values.
Form Layout Window
The Form Layout window allows the user to specify the screen location of the form
within a project.
To change the form location simply drags the form icon to the desired position.
Tool Box
The Toolbox contains icons that represent commonly used controls, such as label, text
box, command button, picture box, frame, picture box, option button, file list box, and
so on.
The user can select a control from a toolbox and place it in the current form design
window by double clicking on the control icon, or by clicking once the control icon,
then clicking on the desired location within the Form design Window and dragging
the mouse so that the control has the desired size.
Form Design and Project Container Window
The form design window is where the user interface is actually designed.
This is accomplished by selecting the desired control icons from the Toolbox and
placing them in the Form design window.
Each control can be moved or resized, and its properties can be reassigned as
required.
If the user selects code view within the Project window, or if the user can double-
click on the control icon within a Form Design Window, the Code Editor Window
will open, displaying the Visual Basic code associated with the currently active form.
Immediate window:
The Immediate Window is very useful when debugging a project, Whenever the user
enters a variable or expression within this window the corresponding value will be
shown immediately.
DATA TYPES
Visual basic supports all common data types including Integer, long integer, single,
double and string.
The language also supports other data types such as Boolean, byte, currency and date
data, as well as variant-type data and user-defined data types.
The Dim statement is used to associate variables with specific data types.
This process, which is common to all modern programming languages, is known as
data declaration.
Variants: the data type of the variable is determined implicity by the value that is assigned to
the variable.
Explicit Declaration:
Declaring a variable tells VB to reserve space in memory.
It is not a must that a variable should be declared before using it.
Automatically whenever visual basic encounters a new variable, it assigns the default
variable type and value. This is called implicit declaration.
Dim variable name 1 As data type 1, variable name 2 As data type 2, etc.
Example for variable declaration:
Dim Count As
Integer Dim Area As
Single
Option explicit statement:
It may be convenient to declare variable implicitly, but it can lead to errors that may
not be recognized at run time. So the option explicit keyword is used to stop this type run
time errors in visual basic.
To prevent errors of this nature, user can declare a variable by adding the following
statement to the general declaration section of the form.
Option Explicit
This forces the user to declare all the variables. The option explicit statement checks
in the module for usage of any undeclared variables and reports an error to the user. The user
can thus rectify the error on seeing this error message.
Scope of variables:
A variable is scoped to a procedure-level (Local) or module level variable depending
on how it is declared.
Three types of scope are followed in Visual Basic.
Local variable.
Module level
variable, Static
Variable.
Local Variable:
A local variable is one that is declared inside a procedure. This variable is only
available to the code inside the procedure and can be declared using the dim
statement as given below,
Sub add()
Dim count as
integer End sub
The local variables exist as long as the procedure in which they are declared, is
executing.
Once a procedure is executed, the values of its local variables are lost and the
memory used by these variables is freed and can be reclaimed.
Static Variable:
Static variables are not re-initialized each time visual basic invokes a procedure and
thus retains or preserves value even when a procedure ends.
These static variables are also ideal for making controls alternately visible or
invisible.
Example:
Static counter as integer
Variables have a lifetime in addition to scope.
The value of a local variable can be preserved using the static keyword.
To make all variables in a procedure static, the static keyword is placed at the
beginning of the procedure heading as given in the statement below.
Static Function add( )
Module level variables:
A module level variable is available to all the procedures in the module.
They are declared using the public or the private keyword.
Public temp as integer
Private count as integer
Public:
Declaring a variable using the public keyword makes it available throughout the
application even for the other modules. Public variable in different modules can share
the same name and they can be differentiated in code.
Private:
At the module level there is no difference between dim and private but private is
preferred because it makes the code easier to read.
A variable is declared in general declaration section of a form and hence is available to all the
procedures.
1.7 MODULES
Code in visual basic is stored in the form of modules.
The three kinds of modules are in Visual basic.
1. Form Module
2. Standard Module
3. Class Module
Form Module:
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.
Standard Module:
To avoiding 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) are the foundation of object oriented programming in visual basic.
New objects can be created by writing code in class modules.
Each module contains declarations and procedures.
Declaration: May include constant, type, variable and DLL procedure declarations.
1.8 PROCEDURES
Procedures:
A sub function or property procedure that contains pieces of code that can be executed
as a unit.
Visual Basic programs can be broken into smaller logical components called
procedures.
Procedures are useful for condensing repeated operation such as the frequently used
calculations, text and control manipulations.
It is easier to debug a program with procedures which breaks a program into discrete
logical limits.
Procedures used in one program can act as building blocks for other programs with
slight modifications.
Procedures can be three types.
1. Sub procedures
2. Function procedures
3. Property procedures
Sub procedures:
Sub procedures can be placed in standard, class and form modules. Each time the
procedure is called the statements between sub and End Sub are executed.
Syntax:
[Private | Public | Static] Sub procedure_name [(Argument list)]
[Statements]
End Sub
Argument list is a list of argument names separated by commas. Each argument acts like a
variable in the procedure. There are two types of sub procedures (General procedures and
event procedures)
Event Procedures:
An event procedure is a procedure block that contains the controls actual name an
underscore ( _ ), and the event name. Event procedures acquire the declaration as private by
default.
Syntax:
Private Sub Form_Load( )
Statement block
End sub
Syntax : 1 Syntax : 2
IfLogical Expression Then If logical expression Then
executable ………………………
Statement End if Executable Statements
(OR) ………………………
IfLogical Expression Then Else
……………………… ………………………
executable Statements Executable Statements
………………………… ………………………
End If End If
The statement is executed only if the condition is true. The condition is usually a
comparison, but it can be any expression that evaluates a numeric value.
If then else block is used to define several blocks of statement in order to execute one
block.
Select Case statement
Select case structure is an alternative to if then else if for selectively executing a
single block of statement from among multiple bocks of statements.
Syntax
Select
CaseExpression
Casevalue1
Executable statements
Casevalue2
Executable statements
Case Else
Executable statements
End Select
Looping:
Many programs require that a group of instruction be executed repeatedly, until
particular condition has been satisfied. This process is known as Looping.
1. Do While loop
2. For Next loop
Do While loop statement: The do while … loop is used to execute statements until a
certain condition is met.
Do …Loop While Statement: The do Loop while statement first executes the statements
and then tests the condition after each execution.
While loop Syntax - 1 Do While loop Syntax
Do While logical expression Do
Executable statements Executable statements
Loop Loop While logical expression
Do Until logical expression Do
Executable statements Executable statements
Loop Loop Until logical expression
Do …Loop Until Statement: The Do … loop Until structure executes the statements until
the condition is satisfied. It is an infinite loop if the test condition is fails and to get
released from this infinite loop we can use the CTRL + BREAK combination or end from
the run menu.
In the syntax(A) denote normal for loop. This loop will increase index values with 1. But in
the syntax(B) increment or decrement depend on the value 3.
Example:1
For a = 1 to 10
[Link]= str(a)
Next a
This loop will execute 10 times. Because its default increment is 1.
Example:2
For b = 1 to 10 step 3
[Link] = str(b)
Next b
This above loop execute 3 times only. Because its increment value is 3.
Jumping statements:
1. GotoLabel_name – It is used to move control to specified label name.
2. Exit for – It is used to terminate the current for loop.
3. Exit do - It is used to terminate the current do loop.
4. Continue – It is used to continue the current loop execution.
5. Stop – It is used to terminate the execution at any point in the
program. With ….. End With statement:
When assigning values to several properties within the same object at run time, it is
often convenient to do so using a With Block.
This construct allows the object name to be specified only once, followed by each of
the property assignments.
The use of with blocks is logically more concise than individual, independent property
assignments.
Syntax
With object name
.property 1 = ……….
.property 2 = ……….
……………………...
.property n = ……….
End With
Row 2
Row m
DYNAMIC ARRAYS
There will be a situation when the user may not know the exact size of the array at
design time. Under such circumstances, a dynamic array can be initially declared and can add
elements when needed instead of declaring the size of the array at design time.
Dynamic arrays are more flexible than fixed-arrays, because they can be resized
anytime to accommodate new data. Like fixed-sized arrays, dynamic arrays have Public (in
code modules), module or local scope.
The actual number of elements can be allocated using a ReDim statement. The Redim
Statement can appear only in a procedure, which is an executable statement.
Redimitems(32)
Each time on executing the ReDim statement, the current data stored in the array is lost and
the default value is set. But if we want to change the size of the array without losing the
previous data, we have to use the Preserve keyword with the ReDim statement.
Redim Preserve items ( 44 )
When the Preserve keyword is used, only the upper limit of the last dimension in a
multidimensional array can be changed. No other dimensions or the lower limit of the last
dimension can be changed.
A dynamic array is an array whose size can be changed at various places within a
program. To declare a dynamic array, we use the Dim statement, followed by the array name
and en empty pair of parentheses; i.e
Dim array name ( ) As data type
1.11 WORKING WITH CONTROLS
Creating and Using controls
An control is an object that can be drawn on a form object to enable or enhance user
interaction with an application. Controls have properties that define aspects of their
appearance, such as position, size and color and aspects of their behavior such as their
response to the user input.
They can respond to events initiated by the user or triggered by the system. For
example a code could be written in a command button controls click event procedures that
would load a file or display a result.
In addition to properties and events, methods can also be used to manipulate controls
from code. For example, the move method can be used some controls to change their location
and size.
Classification of controls:
Visual basic controls are broadly classified as standard controls, ActiveX controls and
insertable objects.
Standard controls are command button, text box, label etc..are contained inside .EXE file and
are always included in the toolbox which cannot be removed.
ActiveX controls exist as separate files with either. .VBX or .OCX extension. Some ActiveX
controls are listed below,
MSChart control
The Communication control
The Animation control
The ListView control
An ImageList control
The Internet Transfer control
The Picture clip control.
TabIndex property of Controls:
Visual Basic uses the TabIndex property to determine the control that wound receive
the focus next when a tab key is pressed.
Evert time a Tab key is pressed, VB looks at the value of TabIndex for the control that
currently has focus and then it scans through the control searching for the next highest
TabIndex number.
Standard controls:
Combo Box : Combines the capabilities of a text box and a list box. Thus, it provides
a collection of text items, one of which may be selected from the list at any time during
program execution. Text items can be assigned initially, or they can be assigned during
program execution. In addition, the user can enter a text item at any time during program
execution.
Command Button : Provides a means of initiating an event action by the user clicking
on the button.
Picture Box
Pointer
Text Box
Label Box
Command Button
Frame
Option Button
Check Box
Horizontal
Scroll Bar Vertical Scroll Bar
Timer
Drive List Box
Directory List
Box File List Box
Shape Line
OLE Container
Fig(7) Standard Tool Box
File List Box : Provides a means of selecting files within the current directory.
Frame : Provides a container for other controls. It is usually used to contain a group
of option buttons, check boxes or graphical shapes.
Horizontal Scroll Bar : Allows a horizontal Scroll Bar to be added to a control (if
horizontal scroll bar is not included automatically).
Image Box : Used to display graphical objects and to initiate event actions (Note :
an Image Box is similar to a Picture Box. It redraws faster and can be stretched, though it has
fewer properties than a Picture Box)
Label : Used to display text on a form. The cannot be reassigned during program
execution, though it can be hidden from view and its appearance can be altered
List Box : Provides a collection of text items. One text item may be selected from the
list at any time during program execution.( Note : Combo box combines the features of a list
box and a text box)
Option Button : Provides a means of selecting one of several different options. Within a
group of option buttons, only one option can be selected
Picture Box : Used to display graphical objects or text, and to initiate event actions. (
Note : the picture box is similar to an Image Box. It has more properties than an image box,
though it redraws slower and cannot be stretched.)
Pointer : The pointer is not only a control tool, in the true sense of the word. When the
pointer is active, the mouse can be used to position and resize other controls on the design
form and to double click on the controls, resulting in a display of the associated Visual Basic
Code.
Shape : Used to draw circles, ellipses, squares and rectangles within forms, frames
or picture boxes
Text Box : Provides a means of entering and displaying text. The text can be
assigned initially, it can be reassigned during program execution, or it can be entered by the
user during program execution.