Visual Basic Programming
Visual Basic Programming
Visual Basic Programming
Find Visual Basic on the Windows Start menu and start it.
The large VB window appears with a New Project dialog box (if there is
no dialog box, bring it up by File > New Project).
Select Project > Project1 Properties. In the Project Properties dialog box,
change Project1 to your project name, say counter.
4. Save the form (you should always name and save the form before you
save the project).
Select File > Save frmCounter As.... The Save Form as dialog box
appears. The Save in: textbox shows the name of the folder where the
form will be saved. You should create a new folder for this project.
Navigate to the folder where you want to keep all your VB projects and
click on the new folder icon (a picture of a folder with a little highlight).
After you create the new folder, select that folder. Its name should
appear in the Save in: box. The form file name frmCounter.frm should
appear in the File name: box (if not, type in the correct name). Click
on Save.
5. Save the project (be sure to name and save the form before you save the
project).
Select File > Save Project As.... The Save Project as dialog box appears.
The Save in: textbox should show the correct folder (the one you just
created for the form). The File name: box should show the correct name
(Counter.vbp in this example). Click on Save.
The program window appears, looking much like the form you designed. The
controls on the window are active. While the program is running, it behaves
like any other window on the desktop: you can move it, minimize it, etc.
The indicator on the VB window title bar changes from design mode to run
mode. Many items in the VB window disappear during run mode and many
menu and toolbar operations not enabled.
In your program's window, click the Exit button or menu (if you provided
one)
In your program window's title bar, click the Close button (VB always
provides one)
On the VB menu bar, Run > Stop
On the VB toolbar, click the VB Stop icon (the box)
The indicator on the VB window title bar changes from run mode to design
mode. Items in the VB window reappear, and menu and toolbar operations are
enabled again.
Forms
Visual Basic Form is the container for all the controls that make up the user
interface. Every window you see in a running visual basic application is a form,
thus the terms form and window describe the same entity.
The main characteristic of a Form is the title bar on which the Form's caption
is displayed. On the left end of the title bar is the Control Menu icon. Clicking
this icon opens the Control Menu. Maximize, Minimize and Close buttons can
be found on the right side of the Form. Clicking on these buttons performs the
associated function.
7 Text The text, which will appear at the title bar of the
form.
Form Events
In order to load and unload the forms, Load and Unload statements are used.
The Load statement has the following syntax:
Load FormName
Unload FormName
The Hide method is used to hide a Form. The following is the syntax of the
Hide Method.
FormName.Hide
To hide a Form from within its own code, the following code can be used.
Me.Hide
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.
All the controls in the ToolBox except the Pointer are objects in Visual Basic.
These objects have associated properties, methods and events.
Real world objects are loaded with properties. For example, a flower is loaded
certain color, shape and fragrance. Similarly programming objects are loaded
with properties. A property is a named attribute of a programming object.
Properties define the characteristics of an object such as Size, Color etc. or
sometimes the way in which it behaves.
For example, a TextBox accepts properties such as Enabled, Font etc. Enables
property allows the TextBox to be enabled or disabled at run time depending
on the condition set to True or False. Font property sets a particular font in
the TextBox.
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.
Control Description
ListBox Displays a list of items from which a user can select one.
Contains a TextBox and a ListBox. This allows the user to
ComboBox select an ietm from the dropdown ListBox, or to type in a
selection in the TextBox.
Events
There are different types of events and different ways to trigger them. An event
can be triggered in the following ways:
Event Procedures: Code related to some object. This is the code that is
executed when a certain event occurs.
Unit 2
Loops
Loops are control structures used to repeat a given section of code a certain number of times or until a particular
condition is met.
We can write a Visual Basic procedure that allows the program to run repeatedly until a condition or a
set of conditions is met. This is procedure is known as looping. Looping is a very useful feature of
Visual Basic because it makes repetitive works easier.
Unit 4
Procedures
Visual Basic offers different types of procedures to execute small sections of
coding in applications. Visual Basic programs can be broken into smaller
logical components called Procedures. Procedures are useful for condensing
repeated operations such as the frequently used calculations, text and control
manipulation etc. The benefits of using procedures in programming are:
It is easier to debug a program 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.
A Procedure can be Subroutines, Function or Property Procedure.
Subroutines
A subroutine is a block of statements that carries out a well-defined task. The
block of statements is placed with a pair of sub/end sub statements and can
be invoked by name.
Example:
Sub sayhello()
MsgBox “Hello User”
End Sub
The statements in a subroutine are executed and when the End Sub statement
is reached, control returns to the calling program.
Functions
A function is similar to a subroutine, but a function returns a result.
Subroutines perform a task and don’t report anything to the calling program;
functions commonly carry out calculations and report the result. The
statements that make up a function are placed in a pair of Function/End
Function statements. Moreover, because a function reports a result, it must
have a type, for example:
Function ShowMonth() As String
ShowMonth = Month (date)
End Function