Familiarization about the Visual Basic IDE Components
Visual Basic :
Visual Basic is a third-generation event-driven programming language and integrated development environment (IDE) from Microsoft.
Visual Basic is an Object-Oriented Programming development system for creating applications that run under any of the Microsoft Windows Environment. It was derived from BASIC and enables the rapid application development of Graphical User Interface (GUI) applications.
Event-driven programming :
Event-driven programming is a programming paradigm in which the flow of the program is determined by events.
Ex : mouse click , keypress …..
The program’s response to an action taken by the user is reffered to as event.
Event procedure :
The group of basic commands that brings about the response is an event procedure.
Integrated Development Environment (IDE):
An IDE is a software application that provides comprehensive facilities to computer programmers for software development.
An IDE consists of :
• A source code editor
• Build automation tools
• A debugger
Source code editor : It is a text editor program designed specifically for editing source code of computer programs by programmers.
Build automation tools : It is the act of scripting the tasks which include
• Compiling source code
• Running tests
Debugger : A debugger or debugging tool is a computer program that is used to test and debug the programs.
Graphical User Interface (GUI) :
GUI is a type of user interface that allows users to interact using images through graphical icons.
VB 6.0 Programming Environment :
Visual Basic provides IDE (Integrated Development Environment), which provides developers all the tools they need to develop applications.
To start Visual Basic from Windows:
• Click Start on the Task bar.
• Select Programs.
• Select Microsoft Visual Studio 6.0 and then Microsoft Visual Basic 6.0
The Visual Basic Environment
You can choose to start a new project, open an existing project or select a list of recently opened programs. A project is a collection of files that make up your application. There are various types of applications we could create, however, we shall concentrate on creating Standard EXE programs (EXE means executable program). Now, click on the Standard EXE icon to go into the actual VB programming environment.
The Visual Basic Environment consists of the:
• A Blank Form for you to design your application's interface.
• The Project window, which displays the files that are created in your application.
• The Properties window, which displays the properties of various, controls and objects that are created in your applications.
It also includes a Toolbox that consists of all the controls essential for developing a VB Application. Controls are tools such as text box, command button, label, combo box, picture box, image box, timer and other objects that can be dragged and drawn on a form to perform certain tasks according to the events associated with them. Additional objects can be added by clicking on the project item on the menu and click on components on the drop-down list, then select those controls you need to use in your program
Components of IDE:
VB’s IDE has a collection of components. Each component has a specific task. The following are VB IDE Components:
• Menu bar
• Tool bar
• Project Explorer Window
• Properties Window
• Form Layout Window
• Form designer
• Code window
• Object Browser
• Immediate, Locals, and Watch Windows
Menu Bar :
• File :- File handling & printing and access to make EXE file.
• Edit :- Standard editing functions like Undo , Search etc.
• View :- Displays and hides windows and toolbar.
• Project :- Sets project properties , adds/removes forms and modules and
add/remove references and components.
• Format :- Aligns or Sizes controls.
• Debug :- Starts/Stops debugging and stepping through programs.
• Run :- Starts or compiles a program.
• Tools :- Adds procedures , starts the menu editor , sets IDE options.
• Add-Ins :- Add-In manager , lists add-ins like Application Wizard and API Viewer.
• Window :- Arranges or selects open windows.
• Help :- Handles help.
Tool Bar :
This tool bar contains buttons matching to the popular menu items
Standard exe project Find Redo
Add Form Copy break Object Browser
Menu Editor Cut Paste Undo End ToolBox
Open Project Run Project Explorer
Save project Properties Window
Form layout window
Data View Window
Visual Component Manager
To display one of these tool bar’s item just select it and double click on it.
Project Explorer :
It allows to coordinate the parts of program into folders for easy manipulation.
It is very useful while working for large projects. We can add and remove items by right clicking there with the mouse.
Properties Window :
This is the place where we set the object’s properties.
Ex : caption of a form or a command button , text of a textbox etc.
When we select an object, the corresponding object’s properties will appear in the properties window as shown in above figure.
Form Layout Window :
Using this window we can position forms as we want them to appear on the screen. For positioning the form , drag the form on form layout wondow to the required new location.
Tool Box :
• The use of tool box is to add controls or objects to the form.
• To add the required object click on that particular object & drag it on to the form (or) Double click on the required object .
• The tool box is loaded with controls such as :
Pointer , Picture Box , Label , Text Box , Frame , Command Button , Check Box , Option Button , Combo Box , List Box , Horizontal Scrollbar , Vertical Scrollbar , Timer , Drive List box , Directory List box , File List box , Shape , Line , Image Box , Data , OLE (Object Linking Embedding).
Form Designer :
These are just windows in which a particular form appears. We can place objects on a form by double clicking on them. The sample form is as shown below
Code Window :
• The place where the code is written is Code Window.
• To open the code window of a particular object, just double click on that object.
• There are two drop-down list boxes in the code window.
1. Light list box lets to select the object to add code into.
2. Right list box lets to select the procedures to add all the methods the object control supports.
Object Browser :
The object browser lists all the Modules and classes (including the functions, constants and properties that they contain) that are available for use in vb. We can refer to more classes by adding a reference from the project menu and choosing references.
The reference added from the project menu will also be listed by the obect browser. The object browser uses different icons to differentiate between classes, constants, functions and properties. There is also a search box to search for functions, classes etc.
Immediate , Locals and Watch Windows :
Immediate Window - is a great place for you to modify data or to test the function during development. You can enter valid expression in the Immediate window, and VB will execute it.
Local Window - enables you to see the value of every variable, and each member of all the objects which are in current scope.
Watch Window - enables you to monitor the value for a certain state. You might want the program execution to pause on an instruction that sets a certain date. You might want to set watch expression that cause VB to break when a variable changes its value or when an expression's value us True.
Object Related Concepts in VB
• Forms: In visual basic, a window is called as a form. Each form includes a title bar at the top.
• Controls: The icons with which the user interacts are called controls. The user activate a control to produce an event.
• Objects: Forms and controls are referred to collectively as objects. Most objects are associated with events, objects may include their own unique event procedures. Objects are associated with their own properties & methods.
• Properties: It defines their appearance or behaviour
• Methods: Some objects also include special program statements called methods. It is the predefined action affecting the associated object.
A Visual Basic application is developed in three steps:
• Create the user interface
• Change properties
• Write code
In the first step, we lay down control on the form and arrange them in the way we want.
In the second step, we change properties of controls and form to required objects.
In the third step, we write code for events.
Creating user interface
Our first application needs only one control – command button. Select command button in Toolbox and drag it and drop it on the form at the required location.
Here are the required steps.
Invoke Form designer by double clicking on Form in Project Explorer, if designer is not already displayed. (or)
Select the icon for command button in Toolbox. The background of the icon becomes gray.
Go to form. The moment you enter into the form the mouse pointer changes to cross hair (plus sign).
Place the cross hair at the bottom of the form and drag mousepointer (means hold down the left button and move the mouse pointer). While you move mousepointer you see a gray rectangle.
Once you dragged from the required location to required location, release the mouse button.
Control appears at that location with the size that is same as the gray rectangle.
Once the required controls are placed on the form, we have to change the properties of the control to get the required look and behavior.
Each control has a collection of properties.
Each property represents an attribute of the control.
For example, Name property identifies the name used to reference the control in the code and so on.
So to change the properties of control (and form), we have to use Properties Window, which allows you to change properties by displaying properties on left and values on the right.
The following are the steps required to change properties of command button and form.
Invoke Properties Window for command button by pressing F4 after selecting command button.
In Properties Window select Caption property and enter &Exit on the right of it.
Select Name property and change it to cmdExit.
Then click anywhere on the form (to unselect command button) and press F4 to invoke Properties Window for Form.
Select Caption property and change it to First Visual Basic Application.
Select Name property and change it to frmFirst.
The syntax to set the property of an object or to pass certain value to it is :
where Object and Property is separated by a period (dot).
For example, the statement Form1.Show means to show the form with the name Form1 , label1.caption means the caption of label will be effected.
• The final step is writing code for events.
• Just before we proceed further, let us understand the event to which we need to respond.
• We need to respond to Click event of the form, as we want to display the name whenever user clicks on the form.
• We need to respond to Click event of command button (cmdExit) as we have to terminate program when user clicks on the command button.
• Here are the steps to write code for these two events.
Select form and press F7 to invoke code window.
Double click on the form to invoke code window.
Code window displays Load event on the right (as that is default event for form).
Select Click event using the dropdown list and write code as shown in figure
Double click on the command button to invoke code window. By default click event is selected.
Write the following code.
Print is a statement that displays the given data on the form.
When you save a project, Visual Basic prompts you to enter filenames in which it has to save components, such as Form of the project and then the project.
Each component is saved in a separate file and project is saved in a separate file.
Save the project using following steps.
Select File->Save Project
Click on Save Project icon in Toolbar.
When Visual Basic prompts you to enter filename for form, select directory in which you want to save the form file and enter filename.
By default the name of the form will be the name of the file. The default extension is .FRM (form).
After entering file name Click on Save button to save form.
Then it prompts you to enter name of the file for project.
Enter a filename. Visual Basic adds extension .VBP (Visual Basic Project).
Click on Exit button to terminate application.
Now, run the application using any of the following methods.
By pressing F5
By clicking on Start icon in Toolbar
By selecting Run menu and Start option.
Once an application is loaded it can be run by click on the icon from the toolbar, to pause press and to terminate use .
Data Types :
There are many types of data we come across in our daily life. For example, we need to handle data such as names, addresses, money, date, stock quotes, statistics and etc everyday. Similarly in Visual Basic, we are also going to deal with these kinds of data. However, to be more systematic, VB divides data into different types.
Types of Visual Basic Data Types
• Numeric Datatype
• Non-numeric Datatype
Numeric data are data that consists of numbers, which can be computed mathematically with various standard operators such as add, minus, multiply, divide and so on.
In Visual Basic, the numeric data are divided into 7 types
Type Storage Range of Values
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,648
Single 4 bytes -3.402823E+38 to -1.401298E-45 for negative values
1.401298E-45 to 3.402823E+38 for positive values.
Double 8 bytes -1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 12 bytes +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use
+/- 7.9228162514264337593543950335 (28 decimal places).
Non-Numeric Datatype :
Nonnumeric data types are data that cannot be manipulated mathematically using standard arithmetic operators.
The non-numeric data comprises text or string data types, the Date data types, the Boolean data types that store only two values (true or false), Object data type and Variant data type.
Data Type Storage Range
String(fixed length) Length of string 1 to 65,400 characters
String(variable length) Length + 10 bytes 0 to 2 billion characters
Date 8 bytes January 1, 100 to December 31, 9999
Boolean 2 bytes True or False
Object 4 bytes Any embedded object
Variant(numeric) 16 bytes Any value as large as Double
Variant(text) Length+22 bytes Same as variable-length string
A variable is a name that represents a numerical quantity, a string or some other basic data type.
The rules for naming variables are :
• A variable must begin with a letter.
• A variable name cannot exceed 255 characters.
• Reserve words cannot be used as a variable names.
• VB does not distinguish between upper and lower case letters.
Ex : Valid : myname , my_name
Invalid : my name , my&name
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 :
Dim variable name as Data Type
If data type is not specified, VB will automatically declare the variable as a Variant.
For string declaration, there are two possible formats, one for the variable-length string and another for the fixed-length string.
For the fixed-length string, you have to use the format as shown below:
Dim VariableName 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.
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
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 more.
Label1.Visible = True
An operator is a symbol or a word that performs an operation. Depending upon the type of operation an operator performs, operators are classified as follows:
Category What it does?
Arithmetic Operators Used to perform arithmetic operations such as addition, subtraction etc.
Relational Operators Used in forming conditions used to compare values.
Logical Operators Used to combine conditions or to negate condition
Assignment Operator Used to assign a value to a variable
Concatenation Operator Used to concatenate ( join) two strings
Arithmetic Operators :
The following are various arithmetic operators.
Operator What it does
- Subtraction. This operator can also be used as unary operator.
In this case, it negates the sign of the number.
For example, if variable A contains –10 then –A will result in 10(positive 10).
Mod Modulus. Returns the remainder of the division.
\ Integer Division. Returns only integer portion of the quotient.
The following are the operators used to form conditions. The result of any expression using a relational operator will be either True or False.
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
= Equal to
<> Not equal to
These operators are used to combine two relational operators. The following are the available logical operators.
AND Both the conditions must be true for the entire condition to be true.
OR If either condition is true then the entire condition is true.
NOT Reverses the result of the condition.
XOR The entire condition is true only when either of the conditions is true. If both the conditions are true or if both the conditions are false then entire condition is false.
EQV The entire condition is true only when both the conditions are either true or
IMP The entire condition is true except when the first condition is true and the
second condition is false.
Control Statements are used to control the flow of program's execution. Visual Basic supports control structures such as if... Then, if...Then ...Else, Select...Case, and Loop structures such as Do While...Loop, While...Wend, For...Next etc method.
If...Then selection structure
The If...Then selection structure performs an indicated action only when the condition is True; otherwise the action is skipped.
Syntax of the If...Then selection
If <condition> Then
e.g.: If average>75 Then
txtGrade.Text = "A"
If...Then...Else selection structure
The If...Then...Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False.
Syntax of the If...Then...Else selection
If <condition > Then
e.g.: If average>50 Then
txtGrade.Text = "Pass"
txtGrade.Text = "Fail"
Nested If...Then...Else selection structure
Nested If...Then...Else selection structures test for multiple cases by placing If...Then...Else selection structures inside If...Then...Else structures.
Syntax of the Nested If...Then...Else selection structure
You can use Nested If either of the methods as shown above Method 1
If < condition 1 > Then
ElseIf < condition 2 > Then
ElseIf < condition 3 > Then
If < condition 1 > Then
If < condition 2 > Then
If < condition 3 > Then
e.g.: Assume you have to find the grade using nested if and display in a text box
If average > 75 Then
txtGrade.Text = "A"
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S"
txtGrade.Text = "F"
Select...Case selection structure
Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a single block of statements from among multiple block of statements. Select...case is more convenient to use than the If...Else...End If. The following program block illustrate the working of Select...Case.
Syntax of the Select...Case selection structure
Select Case Index
e.g.: Assume you have to find the grade using select...case and display in the text box
Dim average as Integer
average = txtAverage.Text
Select Case average
Case 100 To 75
Case 74 To 65
Case 64 To 55
Case 54 To 45
Case 44 To 0
MsgBox "Invalid average marks"
Visual Basic allows a procedure to be repeated as many times as long as the processor could support. This is generally called looping .
The format are
a) Do While condition
Block of one or more VB statements
Block of one or more VB statements
Loop While condition
c) Do Until condition
Block of one or more VB statements
Block of one or more VB statements
Loop Until condition
Do while counter <=1000
* The above example will keep on adding until counter >1000.
The above example can be rewritten as
Loop until counter>1000
Looping with While-Wend
The format are :
While logical expression
The format is:
For counter=startNumber to endNumber (Step increment)
One or more VB statements
(a) For counter=1 to 10
(b) For counter=1 to 1000 step 10
(c) For counter=1000 to 5 step -5