0% found this document useful (0 votes)
5 views25 pages

Unit 1 Visual Programming

The document provides an overview of Visual Basic 6 (VB6) as a programming language for developing Windows applications, highlighting its user-friendly GUI and features. It covers the programming environment, including the Integrated Development Environment (IDE), forms, controls, variables, data types, modules, and procedures essential for application development. Additionally, it discusses control structures and the importance of proper variable declaration and scope in programming.

Uploaded by

Nithya Devi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Unit 1 Visual Programming

The document provides an overview of Visual Basic 6 (VB6) as a programming language for developing Windows applications, highlighting its user-friendly GUI and features. It covers the programming environment, including the Integrated Development Environment (IDE), forms, controls, variables, data types, modules, and procedures essential for application development. Additionally, it discusses control structures and the importance of proper variable declaration and scope in programming.

Uploaded by

Nithya Devi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SUBJECT NAME : VISUAL PROGRAMMING

DEPARTMENT : [Link] Computer Science


CLASS : III [Link] CS
SEMESTER V

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.

GETTING STARTED WITH VB6:


Introduction to Visual Basic:
 Visual basic is an ideal programming language for developing professional
applications fro Microsoft windows.
 It makes use of Graphical User Interface (GUI) for creating robust and powerful
applications.
 The GUI as the name suggests used illustrations for text, which enables users to
interact with an application. This feature makes it easier to comprehend things in a
quicker and easier way.
 In a GUI environment, the number of opitions open to the user is much greater
allowing more freedom to the user and developer.
 It was developed from the programming language – BASIC in the 1970‟s, Microsoft
started developing Rom-based interpreted BASIC for the early microprocessor-based
computers. Later it was named as Visual Basic.
 Complete version of visual basic software is VB 6.0 it requires 250 MB memory in
Hard disk in windows based system.
Features:
 Easier comprehension,
 User-friendliness
 Faster application development
 ActiveX technology with internet features.
PROGRAMMING ENVIRONMENT

 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.

Code editor window:

 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.

WORKING WITH FORMS


 Visual basic has several different types of objects.
 A form is one of the most basic objects in which the application is developed.
 A form is a window that contains an application code and has other objects placed on
it to create the user interface.
 Every form object has its own properties, events and methods associated with it.
Setting form Properties:
Most of programming language every control properties is changed by coding.
1. Border Style:
Forms can have a variety of types of borders.
E.g. Fixed single, Sizeable,FixedDouble,Fixed tool window,Sizable tool
window.
2. Caption:
The title for the window is stored in the caption property.
3. Control Box:
This property determines whether the control box, available by clicking the
upper left corner / icon of a window, will be shown or not.
4. Icon:
This property specifies the icon for the window in the upper left corner of the
window.
5. Mouse Pointer:
This property sets the value for mouse pointer displayed in the appearance of
the form.
6. MDIChild:
This property specifies if this window must be shown within a Multiple
Document Interface (MDI) window.
7. StartupPosition:
This is a property used to set the start up position of the screen.
8. WindowState:
This property indicates whether the window is shown normally, maximized or
minimized.
9. Font:
This property used to set type of font to that respective control.
DEVELOPING AN APPLICATION
 An application can be created with the project. A project is a collection of files that
are used to build an application.
Two steps are required for developing a visual basic project.
1. Visual programming (Designing and property changes)
2. Code programming (Sub, event, function procedures)
 Visual programming involves designing an application with various tools that come
along with the visual basic package.
 Code programming step involves writing programs using a text editor.

Fig(3)Designing form Fig(4) Coding window


VARIABLES
Variables:
 A variables are used for storing values temporarily.
 It may be represents as numerical quantity, string or some other data types (e.g., a
date, true/false condition, etc).

The following rules apply to naming of the variable:


 A variable name must begin with a letter. Additional characters may be letter or
digits. Certain other characters may also be included, though the period and special
data-typing characters (e.g., %, &,!, #, and $) are not permitted.
 A variable name cannot exceed 255 characters. As a practical matter, however,
variable names rarely approach this size.
 Visual basic does not distinguish between uppercase and lowercase.
 Many programmers use uppercase letters as word separators within a single variable
name(e.g.. FreezingPoint, TaxRate, etc.)
 Visual basic includes a number of reserved words (e.g., dim, if, else select, case, do,
etc.).These reserved words cannot be used as function names.
Example:
Tax, income, username, mark1, password, product_id

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

Fig (5) Event Procedure


General Procedures:
A general procedure is declared when several event procedures perform the same
actions. It is a good programming practice to write common statements in a separate
procedure and then call them in that event procedure. In order to add a general procedure
following steps are followed.
 The code window is opened for the module to which the procedure is to be added.
 The add procedure option is chosen from the tools menu, which opens an add
procedure dialog box.

Fig (6) Add Procedure


 The name of the procedure is typed in the name textbox.
 Under Type, Sub is selected to create a sub procedure. Function to create a function
procedure or property to create a property procedure.
 Under scope, Public is selected to create a procedure that can be invoked outside the
module or private to create a procedure that can be invoked only from within the
module.
 A function procedure return a value but sub procedure doesn‟t return a
values. Function procedure:
Functions are like sub procedures, except that they returns value tp the called
procedure. They are especially useful for taking one or more pieces of data called arguments
and performing some tasks with them.
Then the function returns values that indicate the results of the tasks which are
complete within the function.
Syntax
Function procedure name (arguments) As data type
……….
Statements
……….
Procedure name = ……..
………..
End function
The data type designation in the Function statement refers to the data item being
returned
Property Procedures:
A property procedure is used to create and manipulate custom properties. It is used to
create read only properties for forms, standard modules and class modules.
Visual basic provides three kinds of property procedures –
1. Property let procedure that sets the values of a property.
2. Property get procedure that returns the value of a property
3. Property set procedure that sets the reference to an object.

1.9 CONTROL STRUCTURES


Control statements are used to control the flow of programs execution. There are two
types of control structures. Branching and looping.
Branching:
Developer can choose one of two different paths, depending on the outcome of a
logical test (i.e., depending whether a logical expression is true or false) this process is known
as Branching.
1. If – Then
2. If-Then-Else
3. Select - Case
If – Then and If – Then – Else Statement
The if then block is used for conditional execution of one or more statements.

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.

The For …. Next Loop:


The For….Next loop is another way to make loops in Visual Basic.
Syntax:A Syntax:B
For starting value1 to ending value2 For starting value1 to ending value2 step value3
Statements Statements
Statements Statements
Next index Next index

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

[Note the period ( . ) preceding each property specification.


Example:
Private SubForm_Load()
With Combo1
.List(0) = “Red”
.List(0) = “Blue
.List(0) = “Green”
End With
End Sub
Example Explanation: Combo1 is assigned its initial property values within event procedure
Form_Load. Separate With block is used for each object
1.10 ARRAY:
An array is a collection of item which shares common characteristics.
For example, Many applications require the processing of multiple data items that
have common characteristics, such as a set of numerical date items represented by
x1,x2… xn. In such situation, it is often convenient to place the data items into an array,
where they will all share the same name (E.g. x). The data items that make up an array can be
any data type, though they must all be the same data type.
Each individual array element (I.e., each individual data item) is referred to by
specifying the array name followed by one or more subscripts, enclosed in parentheses. Each
subscript is expressed as an integer quantity, beginning with 0. Thus, in the n-element array
x, the array elements are x (0), x(1), x(n-1).
The number of subscripts determines the dimensionality of the array.

Element 1 element 2 element 3 element n


Similarly, y ( I , j ) refers to an element in the two-dimensional array y. think of a two-
dimensional array as a table, where I refers to the row number and j refers to the column
number.
Row 1

Row 2

Row m

Column 1 column 2 column 3 column n


Declaring arrays
Arrays occupy space in memory. The programmer specifies the array type and the
number of elements required by the array so that the compiler may reserve the appropriate
amount of memory.
Fixed-size array : The size of array always remains the same-size doesn't change during the
program execution.
Dynamic array : The size of the array can be changed at the run time- size changes during
the program execution.
Fixed-sized Arrays
When an upper bound is specified in the declaration, a Fixed-array is created. The upper limit
should always be within the range of long data type.
Declaring a fixed-array
Dim numbers(5) As Integer

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:

Check Box : Provides a means of specifying a Yes / No response. Within a group of


check boxes can be checked, including none.

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.

Data : Provides a means of displaying information from an existing database

Directory List Box : Provides a means of selecting paths and


directories(folders) with in the current drive.

Picture Box
Pointer
Text Box

Label Box
Command Button
Frame
Option Button
Check Box

Combo Box List Box

Horizontal
Scroll Bar Vertical Scroll Bar

Timer
Drive List Box
Directory List
Box File List Box

Shape Line

Image Box Data

OLE Container
Fig(7) Standard Tool Box

Drive List Box : Provides a means of selecting among existing drives.

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

Line : Used to draw straight line segments within forms.

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)

OLE Container : Allows a data object to be transferred from another Windows


application and embedded within the Visual Basic application.

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.

Timer : Allows events to occur repeatedly at specified time intervals

Vertical Scroll Bar : Allows a vertical scroll bar to be added to a


control.

1.12 WORKING WITH CONTROL ARRAYS


A control array is a group of controls that share the same name and type. They also
share the same event procedures. Adding controls with control arrays used fewer resources
than adding multiple controls of same type at design time.
Creating control arrays at Design time
There are Three way to creating control array.
1. Assigning the same name in the Name property for more that one control.
2. Copying an existing control and then pasting it into the form. This can be done by
focusing the control and choosing the copy edit menu and then choosing the paste
option from edit menu.
3. Setting the index property to a value is not null.
Adding a control array at Run time:
Control arrays can be created at run time using the following statements.
 Load object (index %)
 Unload Object (index %)
Where object is the name of the control which is to be added or deleted from the control
array. Index % is the value of the index in the array.
The control array to be added must a new an element of the existing array created at
design time with an index value of 0. When a new element of a control array is loaded, most
of the property settings are copied from the lower existing element in the array.

You might also like