Sei sulla pagina 1di 36

Welcome to Visual Basic

Visual Basic IDE-Interactive


Development Environment
Visual Basic IDE

                                                                                                                                                                                               
• Visual Basic - The Basic Canvas
Everytime you load a VB or VBA project, you will be
greeted by roughly the layout shown in Figure 1 and
these five GUI tools. First, the toolbox(1) contains all
the GUI elements/controls needed to create any VB
form and the front end to all VB programs.
For example, after the pointer tool there is the image
control, label, textbox, frame and command button as
the first five of 20 standard controls which are used
constantly in VB programs.
Another advanatge of these basic controls is that they fill
60-90% of all your programming needs and are
automatically included in the VB runtime. It is possible
to add many other Micrsoft supplied or third party
components/controls to this toolbox.
• Second is form(2). you can size it, color it,
give it a caption ("Database Test" in this case)
and fill the form with GUI controls which help
your program do useful works. Putting
controls on your form is as easy as clicking on
the control (say the command button) in the
toolbox and then dragging and sizing it on
your form(see the "Exit" button on the form).
• The third part of the Basic canvas are the
menus and toolbars(3) which manage and
control all of VB. Yes indeed there are lots of
commands and operations to learn. Most
students will be familiar with many of the
menu and icons. File, Edit, View, Run,
Window, Help menus should be familiar to
any Word Perfect, Excel, or Netscape users.
Likewise icons for File Open, File Save, Cut,
Copy, Paste, Find, Run programs, Pause
Program, Halt Program can be seen along the
toolbar.
• Fourth is the Project Explorer
(4)which you use to access all the forms
and coding files in your VB program.
The PE-Project Explorer is such a handy
navigation device you will find yourself
using it all the time to switch among
different forms and code.Yes, even in
small to medium-sized programs
multiple forms are easy to do and quite
common.
• Fifth, and even more frequently used than the
Project Explorer is the Properties
sheet(5). Note that the "Exit" command button
is highlighted in the form and is also the control
listed in the Properties sheet. If you want to
change the property of any control like its
color, shape, caption, or whatever - the Property
sheet is the place to go. But a command button
has 32 properties - and some controls have
hundreds. Fortunately, the 80-20 rule applies -
for example, 80 % of operations can be done
with 6 properties on the command button -
Appearance, Caption, Enabled, Tooltip,
TabIndex, and Visible.
• You will find in developing in Visual
Basic that you spend a large percentage
of time using the Project Explorer
and Property sheet. It is worthwhile
to get to know them well. Project
Explorer is your means of navigating
around the various parts of VB; while
Property sheets allows you to set the
very basic look and feel plus behaviour
of all your forms in Visual Basic.
VB Interface

• Return to VB Project Explorer
VB's Project Explorer is key navigation aid among all the forms
and code that make up a Visual Basic program.
• If the Project Explorer window is not showing just click the View
| Project Explorer menu items (or Ctrl+R keyboard shortcut) to
pop it up like in Figure on previous slide.
• As you might expect the Project Explorer works very much like
Windows Explorer.
There are two folders - one for all the forms used in a VB
program project and another for all the modules. Modules are
Visual Basic program code that can be called and used anywhere
in the VB program.

By clicking on the forms folder it expands out to show all the forms
used in a VB program project. In effect the project file (.vbp file
extension) keeps track of all the resources used in a VB program.
Project Explorer is a window into that project file.
• To add new forms to a project click on
the Project | Add Form menu item. To
delete a form no longer needed in a
project click on the form in Project
Explorer the select the Project | Remove
menu items. However, the most
common use of the Project Explorer is to
quickly move among forms and
modules.
• As shown in Figure , clicking on
the code icon brings up the text
editor loaded with any code
associated with the highlighted
form or module. Right next to
the code icon, is the form icon -
clicking on this icon brings up
the form ready for visual
programming. Allternately, one
can double or rightmouse click
on the form or module also to
quickly navigate to where you
want to go.
The Visual Basic Text Editor
• The Visual Basic Text Editor

The VB development team has lavished


great effort on making editing in VB as
easy yet efficient as possible. Figure on
previous slide highlights some of the
key features of the text editor.

The first thing you notice is that the


editor uses color coding: comments are
in green; VB keywords are in blue; and
most other code is in black.
• Even more important is the fact that VB has a very smart text
editor. Everytime a line of code is entered, VB checks its syntax
and immediately flags any errors.
• VB's Intellisense feature gives users lots of help in getting the
syntax right in the first place. Figure 2 shows Intellisense in
action - as I started to type the text2 property "text", the
Intellisense combo popped up with all the possible properties
and had honed in on the right suggestion by the time I had type
"te". All I had to do was to hit the Tab key and Intellisense filled
in the rest of the command.
• Not only that but on completion of the line it checks the syntax
and, if correct, immediately fancy formats the line. Fancy
formatting includes adding blank spaces around operators (like
"+", "-", "*", etc), auto-indenting code blocks
• In Figure we point to the two combo
box coding aids. The first combo box
lists all the controls used in a form. The
second combo lists all the events that a
specific control (a text field in the
example)can respond to as shown in
Figure.
Visual Basic
An Overview
VISUAL BASIC is a high level programming language evolved from the
earlier DOS version called BASIC. BASIC means Beginners' All-purpose
Symbolic Instruction Code. It is a fairly easy programming language to learn.
The codes look a bit like English Language. Different software companies
produced different version of BASIC, such as Microsoft QBASIC,
QUICKBASIC, GWBASIC ,IBM BASICA and so on.

VISUAL BASIC is a VISUAL and  events driven Programming Language.


These are the main divergence from the old BASIC. In BASIC, programming
is done in a text-only environment and the program is executed sequentially.
In VISUAL BASIC, programming is done in a graphical environment.
Because  users may click on a certain object randomly, so each object has to
be programmed independently to be able to response to those actions (events).
Therefore, a VISUAL BASIC Program is made up of many subprograms,
each has its own program codes, and each can be executed independently and
at the same time each can be linked together in one way or another
The Visual Basic Integrated Development Environment
(IDE)

Has Standard MicroSoft “feel”.


• Title Bar. Status : Design, run, debug.
• Menu Bar. Pull down menus.
• Tool Bar.
• Tool Box. For adding controls.
• Project Explorer. List project modules.
• Properties Window.
• Form Layout.
The Visual Basic Integrated Development Environment (IDE)

On start up, Visual Basic 6.0  will display the following dialog box as shown in figure

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 the
following :
Visual Basic Environment 
• 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
Creating Your First Application

In this section, we are not going into the


Building a Visual
technical aspects of VB programming;
just have a feel of it. Now, you can try
out the examples below:
Basic Application
Example 1
It is a simple program. First of all, you
have to start Microsoft Visual Basic.
Normally, a default form Form1 will be
available for you to start your new
project
Now, double click on form1, the source code
window for form1 (as shown ) will
appear.

The top of the source code window consists


of a list of objects and their associated
events or procedures. In figure , the
object displayed is Form and the
associated procedure is Load.  
Source code window ---------------------
When you click on the object box, the
drop-down list will display a list of
objects you have inserted into your
form as shown in figure.
Objects &
Here, you can see a form,
command button with the name
Command1,
Events
a Label with the name Label1
a PictureBox with the name Picture1.

Similarly, when you click on the


procedure box, a list of procedures
associated with the object will be
displayed as shown in figure 2.
Some of the procedures associated with
the object Form are
Activate, Click, DblClick (which
means Double-Click) , DragDrop,
keyPress and etc.
Each object has its own set of
procedures. You can  always select an
object and write codes for any of its
procedure in order to perform
certain tasks.(AS per requirement)
Structure of VB Program.
VB programs are made up of different
subroutines (or procedures) of the form:

Private Sub <name>()


Comment statement(s)
Declaration statement(s)
BASIC statement(s)
End Sub
VB Statements.
• Comments : Used to document programs and
make them more readable. USE THEM !!!

‘ A Comment!

• Terminate a program.

End
Example 1
List of Procedures Private Sub Form_Load ( )
Form1.show
Do not have to worry about the beginning and the
Print “Welcome VB”
end statements (i.e. Private Sub Form_Load.......End End Sub
Sub.);

Just key in the lines in between the above two


statements exactly as are shown here.
Example 2
When you run the program, you will be surprise that Private Sub Form_Activate ( )
nothing shown up .In order to display the output of Print 20 + 10
Print 20 - 10
the program, you have to add the Form1.show Print 20 * 10
statement like in Example1   or you can just use Print 20 / 10
Form_Activate ( )  event procedure as shown in End Sub
example 2.
 
The command Print does not mean printing using a
printer but it means displaying the output on the
computer screen.

Now, press F5 or click on the run button to run the


program and you will get the output as shown on
next slide 
Arithmetic calculations

• You can also perform


simple arithmetic
calculations
• VB uses * to denote the
multiplication operator
• / to denote the division
operator.
• The output is shown in
figure, where the results
are arranged vertically.
Print command
Output in a horizontal line
• Example 2. can also be
written as 
Private Sub Form_Activate ( )
Print 20 + 10, 20 – 10, 20 * 10, 20 / 10
End Sub

• The numbers will be


arranged in a horizontal line
separated by spaces as
shown in figure.
Variables
Example is an improved version of previous example as it
employs two variables x and y and assigned initial values of
20 and 10 to them respectively. When you need to change the
values of x and y, just change the initial values rather than
changing every individual value which is more time
consuming.

Example 
Private Sub Form_Activate ( )
x = 20
y = 10
Print x + y
Print x - y
Print x * y
Print x / y
End Sub
Concatenation Example
Private Sub  Form_Activate( )                
A = "Tom"
B = “likes"
use the + or the & C = “to"
operator to join two or D = “eat"
E = “burger"
more texts (string) Print A + B + C + D + E
together like in End Sub
examples Example
Private Sub Form_Activate()
A = "Tom"
B = “likes"
C = “to"
D = “eat"
E = “burger"
Print A & B & C & D & E
End Sub
Steps in Building a Visual Basic Application

Generally, there are three basic steps in building a


VB application. The steps are as follows:

Step 1 : Design the interface

Step 2 : Set Properties of the controls (Objects)

Step 3 : Write the events' procedures


Example
Go to the properties window and change the This program is a
form caption to Volume of Cylinder simple program
then drag and insert three labels into the that calculates the
form and change their captions to Base volume of a
Radius, height and volume respectively. cylinder. Lets
design the
After that, insert three Text Boxes and clear interface:
its text contents so that you get three empty
boxes. Named the text boxes as radius, hght
(we cannot use height as it is the built-in Figure   A program to calculate the Volume of a
control name of VB) and volume Cylinder
respectively.
Lastly, insert a command button and change
its caption to O.K and its name to OK.
Now save the project as cylinder.vbp and
the form name as cylinderl.
Working with
Controls

The Control Properties

Before writing an event procedure for


the control to response to a user's
input, you have to set certain
properties for the control to
determine its appearance and how it
will work with the event procedure.
You can set the properties of the
controls in the properties window
Properties window for a form.
Working with Controls
• You can rename the form caption to
any name that you like best.
(the object selected is Form1).
• At the bottom part, the items listed
in the left column represent the
names of various properties
associated with the selected object
while the items listed in the right
column represent the states of the
properties.
• Properties can be set by highlighting
the items in the right column then
change them by typing or selecting
the options available. For example,
in order to change the caption, just
highlight Form1 under the name
Caption and change it to other
names.
Working with Controls

Change its foreground and background


color, change the font type and font size,
enable or disable minimize and maximize
buttons and etc. For example the following
code will change the form color to red
every time the form is loaded. VB uses
hexadecimal system to represent the color.
You can check the color codes in the
properties windows which are showed up
under ForeColor and BackColor .

Private Sub Form_Load()


Form1.Show
Form1.BackColor = &H000000FF&
End Sub
Color
• BackColor — returns or sets the background color of an
object.

• ForeColor — returns or sets the foreground color used to


display text and graphics in an object.

• Visual Basic uses the Microsoft Windows operating


environment red-green-blue (RGB) color scheme.

Potrebbero piacerti anche