Lecture 02
Lecture 02
After this session you will be able to scripting language and a subset of the Visual Basic language.
• Comprehend the different editions of Visual Basic The investment you make in learning Visual Basic will carry over
to these other areas.
• Identify the Hardware and System Requirements for
Installing Visual Basic Whether your goal is to create a small utility for yourself or your
work group, a large enterprise-wide system, or even distributed
• Install Visual Basic
applications spanning the globe via the Internet, Visual Basic
• Invoke Visual Basic has the tools you need.
• Understand the various components of Visual Basic
Visual Basic Editions
Environment
Visual Basic is available in three versions, each geared to meet a
• Identify the different Visual Basic Environment Windows specific set of development requirements.
• Explain the concept of properties, methods and event • The Visual Basic Learning edition allows programmers to
• List and describe the three steps for writing a Visual Basic easily create powerful applications for Microsoft Windows
Project and Windows NT. It includes all intrinsic controls, plus grid,
• Describe the various files that make up a Visual Basic project tab, and data-bound controls. Documentation provided with
this edition includes the Learn VB Now CD plus the
• Look up Visual Basic topics in Help
Microsoft Developer Network (MSDN) Library CDs
Welcome to Visual Basic containing full online documentation.
Welcome to Microsoft Visual Basic, the fastest and easiest way • The Professional edition provides computer professionals
to create applications for Microsoft Windows. It’s no secret that with a full-featured set of tools for developing solutions for
Visual Basic is the favorite programming environment of many others. It includes all the features of the Learning edition,
programmers. Whether you are an experienced professional or plus additional ActiveX controls, the Internet Information
brand new to Windows programming, Visual Basic provides Server Application Designer, integrated Visual Database
you with a complete set of tools to simplify rapid application Tools and Data Environment, Active Data Objects, and the
development1. It is one of the most powerful and enjoyable Dynamic HTML Page Designer. Documentation provided
Windows development tools available today. Visual Basic really with the Professional edition includes the Visual Studio
is fun. This book will familiarize you with the big picture of Professional Features book plus Microsoft Developer
Visual Basic. Network CDs containing full online documentation.
So what is Visual Basic? The “Visual” part refers to the method • The Enterprise edition allows professionals to create robust
used to create the graphical user interface (GUI). Rather than distributed applications in a team setting. It includes all the
writing numerous lines of code to describe the appearance and features of the Professional edition, plus Back Office tools
location of interface elements, you simply add prebuilt objects such as SQL Server, Microsoft Transaction Server, Internet
into place on screen. If you’ve ever used a drawing program Information Server, Visual SourceSafe, SNA Server, and
such as Paint, you already have most of the skills necessary to more. Printed documentation provided with the Enterprise
create an effective user interface. edition includes the Visual Studio Enterprise Features book
The “Basic” part refers to the BASIC (Beginners All-Purpose plus Microsoft Developer Network CDs containing full
Symbolic Instruction Code) language, a language used by more online documentation.
programmers than any other language in the history of Hardware and System Requirements for Installing Visual Basic
computing. Visual Basic has evolved from the original BASIC
Before you install Visual Basic, make sure that your computer
language and now contains several hundred statements,
meets the minimum requirements, and read the Readme file,
functions2, and keywords, many of which relate directly to the
located at the root directory on your installation disc.
Windows GUI. Beginners can create useful applications by
learning just a few of the keywords, yet the power of the Check the Hardware and System Requirements
language allows professionals to accomplish anything that can To run Visual Basic, you must have certain hardware and
be accomplished using any other Windows programming software installed on your computer. The system requirements
language. include:
The Visual Basic programming language is not unique to Visual • Microsoft Windows 95 or later, or Microsoft Windows NT
Basic. The Visual Basic programming system, Applications Workstation 4.0 (Service Pack 3 recommended) or later.
Edition included in Microsoft Excel, Microsoft Access, and
many other Windows applications uses the same language. The
VISUAL BASIC
processor recommended), or any Alpha processor running in Windows. You are then ready to start Visual Basic from
Microsoft Windows NT Workstation. Windows.
• A CD-ROM disc drive. To start Visual Basic from Windows
• VGA or higher-resolution screen supported by Microsoft 1. Click Start on the Task bar.
Windows. 2. Select Programs, Visual Studio and then Microsoft Visual
• 16 MB of RAM for Windows 95/98, 32 MB of RAM for Basic 6.0.
Windows NT Workstation. -or-
• A mouse or other suitable pointing device.
Click Start on the Task bar.
Setting Up Visual Basic Select Programs.
When you run the Setup program, a directory is created for
Use the Windows Explorer to find the Visual Basic executable
Visual Basic; you can then select the components of Visual
file.
Basic that you want to install.
3. Double-click the Visual Basic icon.
With the exception of the operating system files in the \Os
directory, files on the compact disc are not compressed, so You can also create a shortcut to Visual Basic, and double-click
they’re usable directly from the disc. For example, there are the shortcut.
numerous tools and components in the \Tools directory that When you first start Visual Basic, you see the interface of the
can be run or installed directly from the CD-ROM. integrated development environment, as shown in Figure.
To set up from Compact Disc Figure The Visual Basic integrated development environ-
ment
1. Insert the compact disc in the CD-ROM drive.
Use the appropriate command in your operating environment
to run the Setup program, which is available in the root
directory on Disk 1. If AutoPlay is enabled on your system, the
Setup program will automatically load when you insert the
compact disc.
2. Select Install Visual Basic 6.0.
3. Follow the setup instructions on the screen.
Adding or Removing Components of Visual Basic
You can run Setup as many times as necessary. For example, you
can run Setup to reinstall Visual Basic in another directory, or to
install other portions of Visual Basic.
To add or remove components of Visual Basic
1. Insert the compact disc in the CD-ROM drive.
2. Use the appropriate command in your operating
environment to run the Setup program, which is available in
the root directory on the compact disc. If AutoPlay is
enabled on your system, the Setup program will Integrated Development Environment
automatically load when you insert the compact disc. Elements
3. Select the Custom button in the Microsoft Visual Basic 6.0 The Visual Basic integrated development environment (IDE)
Setup dialog box. consists of the following elements.
4. Select the components to be installed (or deselect the Menu Bar
components to be removed) in the Options list box of the Displays the commands you use to work with Visual Basic.
Custom dialog box. Besides the standard File, Edit, View, Window, and Help
5. Follow the setup instructions on the screen. menus, menus are provided to access functions specific to
programming such as Project, Format, or Debug.
Starting Visual Basic
Once you have completed the Setup procedure, you can start Context Menus
Visual Basic by using the Start button on the Windows task Contain shortcuts to frequently performed actions. To open a
bar. If AutoPlay is enabled on your system, you can also start context menu, click the right mouse button on the object you’re
Visual Basic by inserting the Visual Basic compact disc. using. The specific list of shortcuts available from context
menus depends on the part of the environment where you click
Starting the Visual Basic IDE
the right mouse button. For example, the context menu
When you run the Visual Basic Setup program, it allows you to
displayed when you right click on the Toolbox lets you display
place the program items in an existing program group or create
VISUAL BASIC
• Pressing a key on the keyboard
• Clicking with the mouse
The interface components have the ability to respond to events.
E.g. when we click in button it recognized as an event by the
button. Similarly a text box recognizes events like a key press
Te ability to recognize events is built into an interface compo-
nent. The events that an interface component recognize are
different for each type of component
An event driven application operates by responding to the user
Immediate, Locals, and Watch Windows events
These additional windows are provided for use in debugging
your application. They are only available when you are running Methods
your application within the IDE. A method is code that is built into the interface component and
can be executed as required. Each component has several
Exit Visual Basic methods associated with it. Each method provides a distinct
You’ll exit from Visual Basic and return to Windows the same functionality. Unlike properties some of the methods require
way you exit most Windows applications: Select File - Exit, click additional information in the form of arguments.
Visual Basic’s main window close button, press Alt+F4, or
double-click VB’s Control menu icon that appears in the upper- Understanding Properties, Methods and
left corner of the screen. Events
Visual Basic forms and controls are objects that expose their
If you have made changes to one or more files within the
own properties, methods and events. Properties can be thought
currently open project (remember that a project is the collection
of as an object’s attributes, methods as its actions, and events as
of files that comprise your application), Visual Basic gives you
its responses.
one last chance to save your work before quitting to Windows.
An everyday object like a child’s helium balloon also has
Understanding Properties, Methods and properties, methods and events. A balloon’s properties include
Events visible attributes such as its height, diameter and color. Other
Properties properties describe its state (inflated or not inflated), or
Associated with each interface component are certain attributes that aren’t visible such as its age. By definition, all
characteristics. These characteristics are used to describe the balloons have these properties; the settings of these properties
component may differ from one balloon to another.
For example A balloon also has inherent methods or actions that it might
• Name of the component perform. It has an inflate method (the action of filling it with
helium), a deflate method (expelling its contents) and a rise
• Height
method (if you were to let go of it). Again, all balloons are
• Color capable of these methods.
The characteristics of the component are referred to as its Balloons also have predefined responses to certain external
properties events. For instance, a balloon would respond to the event of
The properties of the component are set to default values when being punctured by deflating itself, or to the event of being
the component is created released by rising into the air.
The look of the component is decided by the values assigned to Figure Objects have properties, respond to events, and
its properties. Thus the user customizes an interface component perform methods
provided by the visual development environment using the
properties For example a button may have a default caption
command1. The label can be changed to more meaningful text
such as Exit to indicate the action it provides
Events
A major par of the interactions between people in every day life
is in the form of events and responses to events
For example when the telephone rings we pick up the phone.
The ringing of the telephone is the event and action of our
picking it up is the response to the event
Since computers seek to emulate everyday life applications also If you were able to program a balloon, the Visual Basic code
respond to eventsAn event is any user action directed at the might look like the following. To set the balloon’s properties:
application
Balloon.Diameter = 10 properties that you plan to set or change during the design
of the form.
Balloon.Inflated = True
3. Plan the Basic code. This step is where you plan the
Note the syntax of the code — the object (Balloon) followed by
procedures that will execute when your project runs. You will
the property (.Color) followed by the assignment of the value
determine which events require action to be taken and then
(Red). You could change the color of the balloon from code by
make a step-by-step plan for those actions.
repeating this statement and substituting a different value.
Later, when you actually write the Visual Basic code, you
Properties can also be set in the Properties window while you
must follow the language syntax rules. But during the
are designing your application.
planning stage, you will write out the actions using
A balloon’s methods are invoked like this: pseudocode, which is an English expression or comment
Balloon.Inflate that describes the action. For example, you must plan for the
Balloon.Deflate event that occurs when the user clicks on the Exit command
button. The pseudocode for the event could be” Terminate
Balloon.Rise 5
the project”.
The syntax is similar to the property — the object (a noun)
followed by the method (a verb). In the third example, there is Programming
an additional item, called an argument, which denotes the After you have completed the planning steps and have
distance to rise. Some methods will have one or more argu- agreement from your user, you are ready to begin the actual
ments to further describe the action to be performed. construction of the project. You will use the same three-step
process that you used for planning.
The balloon might respond to an event as follows:
1. Define the user interface. When you define the user interface,
Sub Balloon_Puncture() you create the forms and controls that you designed in the
Balloon.Deflate planning stage.
Balloon.MakeNoise “Bang” Think of this step as defining the objects you will use in
Balloon.Inflated = False your project.
Balloon.Diameter = 1 2. Set the properties. When you set the properties of the
objects, you give each object a name and define such
End Sub attributes as the contents of a label, the size of the text, and
In this case, the code describes the balloon’s behavior when a the words that appear on top of a command button and in
puncture event occurs: invoke the Deflate method, then invoke the form’s title bar.
the MakeNoise method with an argument of “Bang” (the type You might think of this step as describing each object.
of noise to make). Since the balloon is no longer inflated, the 3. Write the Basic code. You will use Basic programming
Inflated property is set to False and the Diameter property is set statements (called Basic code) to carry out the actions needed
to a new value. by your program. You will be surprised and pleased by how
While you can’t actually program a balloon, you can program a few statements you need to create a powerful Windows
Visual Basic form or control. As the programmer, you are in program.
control. You decide which properties should be changed, You can think of this third step as defining the actions of
methods invoked or events responded to in order to achieve your program.
the desired appearance and behavior.
Visual Basic Projects
Writing Visual Basic Projects Each Visual Basic project consists of at least two, and usually
When you write a Visual Basic project, you follow a three-step more, files.
process for planning and then repeat the process for creating the 1. The .vbp file, called the project file, is a small text file that
project. The three steps involve setting up the user interface, holds the names of the other files in the project, as well as
defining the properties, and then creating the code. some information about the VB environment. Note: If you
The Three-Step Process are using release 2.0 or 3.0 of Visual Basic, project files have a
Planning .mak extension.
1. Design the user interface. When you plan the user interface, 2. Each form in your project is saved in a file with a .frm
you draw a sketch of the screens the user will see when extension. To begin, your projects will have only one form
running your project. On your sketch, show the forms and (and therefore one form file). Later, you can expect your
all the controls that you plan to use. Indicate the names that projects to have several forms, with one .frm file for each
you plan to give the form and each of the objects on the form.
form A form file holds a description of all objects and their
Before you proceed with any more steps, consult with your properties for the form, as well as the Basic code you have
user and make sure that you both agree on the look and feel written to respond to the events.
of the project.
VISUAL BASIC
form module. Development Environment
3. Optionally, your project can have .bas extension files. These • What is a Visual Basic event? Give some examples of events
files hold Basic statements that can be accessed from any • What are the three steps for planning and creating Visual
form. As soon as you begin writing multiform projects, you Basic project? Describe what happens in each step
will need .bas files. .bas files are called standard code
• What is the purpose of these Visual Basic file types .vbp,
modules.
,frm .bas
4. Additional controls, called custom controls, are stored in files
• What does context-sensitive Help mean? How can you use it
with a .ocx extension. If you include controls in your project
to see the Help page for a command button
that are not part of the standard control set, the .ocx file
names will be included in the project. • When is Visual basic in design mode run mode and break
mode
5. After you save a project, Visual Basic automatically adds one
more file to your project with an extension of .vbw. This file Notes
holds information about each of your project’s forms.
Design Time, Run Time and Break Time
Visual Basic has three distinct modes. While you are designing
the user interface and writing code you are in design time. When
you are testing and running your project you are in run time. If
you get a run-time error or pause project execution you are in
break mode. The title bar notation indicates the mode.
Visual Basic Help
The Visual Basic Help facility is great! With Help, you really
don’t need a printed manual. You can look up any Basic
statement, object, property, method, or programming concept.
Many coding examples are available, and you can copy and paste
the examples into your own project, modifying them if you
wish.
The VB Help facility is greatly changed and expanded in Version
6.0. Help now includes all of the Microsoft Developer Network
library (MSDN), which contains several books, technical articles,
and the Microsoft Knowledge Base, a database of frequently
asked questions and their answers. You can either install the
entire MSDN library on your hard drive or network or keep the
CD in the drive to reference its topics.
The expanded Help is a two-edged sword: You have available a
wealth of materials, but it may-take some time to find the topic
you want.
Context-Sensitive Help
A quick way to view Help on any topic is to use context-
sensitive Help. Select a VB object, such as a form or control, or
place the insertion point in a word in the editor, and press F1.
The MSDN library viewer will open on the correct page, if
possible, saving you a search.