Unit-5 CC
Unit-5 CC
Structure
5.0 Introduction
5.1 Objectives
5.2 Functions and Procedures
5.3 Form, Standard & Class Module
5.4 Sub Procedure
5.5 Do-Event Functions
5.6 Control Arrays
5.7 Summary
5.0 INTRODUCTION
The Visual Basic language program comprises of certain building blocks of Visual Basic
programming constructs. These constructs include statements, procedures, functions, events etc.
Now you are familiar with various kinds of controls and available in Visual Basic. In this chapter
we will be looking at the user-defined functions and procedures.
A Function is a set of statements (code that someone writes) that perform a specific task within a
program and return either a number or a string value. Visual Basic has number of functions. A
Function begins with a Function statement and ends with an End Function statement. A function
may also accept arguments. These arguments are enclosed in parentheses () following the function
name.
A Procedure is a sequence of code statements that performs a specific task within a program but
return no explicit value. These statements are executed as a unit. A sub procedure begins with a Sub
statement and ends with an End Sub statement. Sub procedure performs actions but do not return
any values whereas Function procedures perform actions and return values.
A Function.or Procedure tells an application what and how to perform a specific task when an event
is generated. Once the Function or Procedure is defined, it must be explicitly invoked by the
application. An event procedure remains idle until called upon to respond to events caused by the
user. You can place a Function or Procedure in either a form module, standard module, or class
mudule.
r uncrlons ai
5.3 FORM, STANDARD & CLASS MODULE Procedures
Form module: A Form module is a file in Visual Basic project with a .FRM filename extension
that can contain graphical descriptions of a form, its controls and property settings, form level
declarations of constants, variables and external procedures, and event and procedures.
Standard module: Standard module is a module containing only procedure, type and data
declarations and definitions. Module level declarations and definitions in a standard module are
Public by default.
Class module: A module that contains the definition of a class. Functions or Procedures can be
either public or private in this module. Public Functions or Procedures can be accessed from any
module in an application, provided you qualify the name of the Function or Procedure with the
name of the module that contains the Function or Procedure. Functions or Procedures can help
divide complex application code into more manageable units.
SUB PROCEDURE
A Procedure is a sequence of code that performs a specific task within a program but returns no
explicit value. A Sub procedure begins with a sub statement and ends with an End Sub statement.
Code within a module is organised into procedures. A procedure tells an application how to perform
a specific task.
To create a procedure
1 . Open the module (either Form, Standard or Class) for which you want to write the procedure.
2. Type Sub and the name of the procedure.
3. Type in sequence of code statements for the procedure.
Visual Basic concludes the procedure with the appropriate End Sub statement.
L
Syntax
[statements]
[statements]
End Sub
This function on execution performs action so that the operating system can process other events.
This function returns the number of open forms.
DoEventsO passes control to the operating system. Control is not returned until the operating
system has finished processing the events in its queue and all keys in the SendKeys queue have
been sent.
If parts of your code take up a lot of processor time, use DoEvents periodically to relinquish control
to the operating system so that events, such as keyboard input and mouse clicks, can be processed
without significant delay.
Make sure the procedure that has given up control with DoEvents is not executed again from a
different part of your code before the first DoEvents call returns; this could cause unpredictable
results. In addition, do not use DoEvents if other applications could possibly interact with your
procedure in unforeseen ways during the time you have yielded control. 1
CONTROL ARRAYS
An array is a set of variables of the same data types. These are used to group related variables
together i.e. all command buttons, all check boxes etc.
A control array is a group of controls that share the same Name, type, and event procedures. A I
control array has at least one element and can grow to as many elements as your system resources i
and memory permit. The size depends on how much of memory and Windows resources each
control requires. The maximum index you can use in a control array is 32767 with 32-bit Windows
platforms, and approximately 16006 with 16-bit Windows platforms.
There are two ways to create a control array: Assign the same name to more than one object of the
same type and Copy an existing object and paste it on the form.
1. All objects of the same type are drawn that are to be grouped together in a control array.
5. Put the next object and so on till all objects have been selected.
6. Select Edit -> Paste from the menu bar. Answer YES if you want to make this new object part
of a control array.
I 7. Follow the above procedure till all the objects are selected. There is no limit to the number of
objects as such.
Once the control array is created, no change is to be made to the name of an object otherwise the
new object that is created by changing the name of the object ceases to be part of the control array.
Elements of the same control array have their own property settings. Common uses for control
arrays include Menu controls and groups of OptionButton controls.
Control arrays are useful if one wants several controls to share the code. For example, if you create
three OptionButton controls as a control array, the same code executes regardless of which button
receives the Click event. If one wants to create a new control at run time, that control must be a
member of an existing control array. With a control array, each new element inherits the common
event procedures of the array.
Without the control array mechanism, creating new controls at run time is not possible, because a
completely new control would not have any predefined event procedures. Control arrays solve this
problem, because each new control inherits the common event procedures already written for the
array. For example, if your form has several TextBox controls that each receives a date value, you
can set up a control array so that all of the TextBox controls share the same validation code.
Components of
Windows Check Your Progress
Programming
&Visual Basic 1. Create the following form
The application will contain three image controls, a timer object and a command button. Tlie
properties and settings of each object will be
Caption "Phone"
Cancel - 1 True
Caption "Exit"
Interval 500
Picture c:\phone 1
Picture c:\phone2
Picture c:\phone3
if Pbmp Then
Main.Picture=RingZ.Picture
Else
Main.Picture=Ring 1 .Picture
Endlf
Pbmp=Not Pbmp
End Sub
SUMMARY
In this chapter, you have been given exposure to Functions, Procedures and control arrays:
2. A Procedure performs a specific task within a program but returns no explicit value.
3. A procedure perform a specific task within a Visual Basic program and returns a value.
4. A Function or Procedure tells an application what and how to perform a specific task when an
event is generated.
5. An event procedure remains idle until called upon to respond to events caused by the user.
6. A control array is a group of controls that share the same Name, type, and event procedures.
7. There are two ways to create a control array: Assign the same name to more than one object of
the same type and Copy an existing object and paste it on the form.