- 1 -
Introduction to Visual Basic 5
- What kinds of programs can you write in Visual Basic?
- Visual Basic makes it easy for you to create simple and complex Windows programs, as
well as your own customized ActiveX documents, ActiveX Controls, and Visual Basic Add-ins.
- What are the new features of Visual Basic 5?
- Visual Basic 5 is full of new and improved features and controls that make it possible
for you to create more powerful programs faster than ever.
- What is object-oriented programming?
- You learn how Visual Basic uses object-oriented techniques to create programs that are
powerful, robust, and efficient.
If you have visited a local computer store lately, you've seen that there is an
overwhelming number of software packages that you can purchase for your computer. In fact,
there are literally thousands of programs available that run under Windows 95, not
including those written by companies and individuals for their own use. Looking through
the software titles, you can find game programs, productivity programs (such as word
processors and spreadsheets), communications programs, databases, and many others. And if
you have surfed the Internet, you've seen even more types of programs there.
In addition to all these commercial or publicly available programs, many companies need
custom programs to handle their business. These programs can range from custom report
writers that prepare expense statements to complex client/server programs that handle
billing by using the information stored on a mainframe. Whatever the case may be, one
thing these programs probably have in common is a Windows "look-and-feel."
Visual Basic makes this easy. Not content just to use available "off-the-shelf"
software, you have decided to embark on the adventure of creating your own programs. Well,
you've come to the right place and chosen the right programming language. Visual Basic is
capable of producing almost any program that your imagination can come up with, including
the following:
- Simple single-purpose applications
- Games
- Point-of-sale systems
- Internationally distributed database applications
And while another programming language might be better suited to a particular
specialized situation, Visual Basic is perhaps the most versatile and easy-to-use
programming language available.
Exploring the New Features of Visual Basic 5
As with a new version of any product, Visual Basic 5 incorporates a number of new and
enhanced features that make it more powerful and easier to use than previous versions. In
addition to a new and improved development environment (IDE), VB version 5.0 includes some
performance enhancements. One of the most requested features for Visual Basic was the
capability to compile a program to native code, or code that is optimized
specifically for the microprocessor on which it runs. Microsoft has finally granted this
wish. VB5's native code compiler allows your programs to run much faster than before.
Also, a faster forms engine greatly enhances the speed of loading forms, adding to the
improved performance of applications created with VB5.
In addition to these two performance features, there are a number of other major
features that were added to version 5 of Visual Basic. These features are covered in
functional groups in the next few sections.
- A native code compiler and improved forms engine make your programs run faster.
- The development environment has been enhanced to make entering code and designing forms
easier.
- A variety of program types--other than just the standard EXE, such as DLLs and OCXs--can
be created.
- A slew of new controls has been added, including several specifically designed for using
the Internet.
Native Code Compiler and Other General Features
One of the key general features of Visual Basic is the capability to compile your
programs to native code, much as you can do with C++. As stated previously, this gives you
faster programs. However, don't confuse native code with code that doesn't require the
Visual Basic runtime library. Native-code compiled programs are optimized for the
microprocessor(s) that they run on; however, the Visual Basic runtime library is still
required to provide a fully functional program.
NOTE: The Visual Basic runtime library
contains functions needed to operate your program. For example, the library includes the
code for drawing a window on the screen. As a programmer you may not be aware of such
functions, so distributing your program may involve including some additional files.
Visual Basic also gives you some new design capabilities. As always, you can create
programs that use a series of independent forms to display and handle information, or you
can create Multiple Document Interface (MDI) programs (which have one main
"parent" form and one or more internal "child" forms). Visual Basic 5
also has added the capability to create programs that will run inside a Web browser,
either on your local machine or anywhere in the world via the Internet!
See "Introducing MDI Applications," Chapter 20
Another new feature is the capability to edit multiple projects in a single Visual
Basic session. You'll find this to be a very convenient tool when you use such other new
features as custom-created ActiveX controls and Dynamic Link Libraries (DLLs). These types
of programs typically involve interaction among multiple VB projects. Therefore, the
capability to switch between related projects or compile several projects at a time is
very useful.
Development Environment Features
If you have used previous versions of Visual Basic, you will notice an entirely new
interface the first time you start the program (see Figure 1.1). The interface has been
redesigned to be more compatible with Microsoft's other programming languages--Visual C++,
Visual J++, and so on. This makes it easier for programmers who work in multiple
environments to move back and forth between them. In fact, the default installations of
many of Microsoft's development products now share a common "parent" directory
(usually C:\Program Files\DevStudio\).
FIG. 1.1
Visual Basic version 5.0 sports a totally new user interface.
All of the various windows that make up the Visual Basic development environment are
now set up in an MDI-style interface contained within a parent Microsoft Visual Basic form
(see Figure 1.2). This makes it easier to manage all the pieces of your program. Many of
the windows, such as the Toolbox, Properties window, and Debug window are dockable,
meaning that they can be placed in a fixed position that remains consistent as you work in
the environment. A handy new Form Layout window shows you at a glance how all of your
application's forms will be placed on the screen at run time.
FIG. 1.2
In the new development environment, all win-dows are contained within the Visual
Basic "parent" window.
Other great features of the development environment include the following:
- The capability to edit multiple projects simultaneously
- Dockable toolbars and development windows
- Code editor enhancements, including a more robust, context-sensitive right-click menu
- A richer debugging environment
- Pop-up lists to help you remember the syntax of available functions, methods, and
properties.
Of course, if you're resistant to change or just don't like the new development
environment, Visual Basic 5 has an option to enable you to return to the
"classic" Single Document Interface (SDI) environment.
See "Understanding the Environment's Key Features," Chapter 2
Enhancements to the Code Editor
Because a great deal of the work done in creating programs takes place in the code
editor, Microsoft has added some new features that make it easier to use. These features
include the following:
- Drop-down properties list Displays a dynamic list of the properties and methods
available for an object. You can select the property or method by typing only the first
couple of characters instead of having to type out the entire word. Pressing Enter or the
spacebar selects the desired entry in the list. This saves time and cuts down on
typographical errors.
- Quick Info Provides you with the syntax of a function, statement, or method. This
information appears in the form of an oversized ToolTip. Having this information appear
on-screen saves you the trouble of looking it up in the Help file.
- Parameter Info Displays the parameters that are required for a particular
function or method. This is similar to Quick Info.
- The Data Tips window While in Break mode, displays as a ToolTip the value of a
variable over which the mouse pointer is placed.
- Block Comment commands Allow you to comment out an entire section of code simply
by selecting the code and invoking the command. Similarly, you can remove the comment
character from the block of code. This makes it easier to enable and disable sections of
code for debugging or other reasons.
New Database Features
Since version 3, Visual Basic has been able to access a native database--the Jet
database engine that it shares with Microsoft Access. With each new version of Visual
Basic, more and more powerful database features have been added. Version 5 is no
exception. Also, the new ODBCDirect functionality provides a truly efficient mechanism for
dealing directly with Open Database Connectivity (ODBC)-compliant database engines.
NOTE: Most database features are available only in
the Professional and Enterprise editions of Visual Basic.
A nice new database feature is the Visual Data Manager application that is
automatically installed as an add-in to the Visual Basic environment (see Figure 1.3).
This add-in makes it easy for you to create and edit the structure of a database, as well
as to input and edit the actual data. Additionally, the Visual Data Manager lets you
create, test, and save SQL statements for use in your programs. As a bonus, the full
source code for the Visual Data Manager add-in is included so you can study and utilize
its data management techniques. (If you're a user of an earlier version of Visual Basic
and used the old Data Manager add-in, give Visual Data Manager a try. It's a lot better!)
FIG. 1.3
The Visual Data Manager is a powerful new add-in for working with databases.
Version 3 of the Jet engine, which appeared in Visual Basic 4 and Microsoft Access 95,
added the capability to replicate databases. This was a great feature for allowing
multiple locations to work on the data and then combining all the changes into a central
master database. Version 5 of Visual Basic takes this a step further with version 3.5 of
the Jet engine. Now you can do partial replication of a database, meaning that you
can provide copies of portions of the database instead of the entire thing. This will cut
down on network traffic for updating the master database, as well as prevent people from
seeing parts of the database that they don't need to see.
Internet Features
Whether you are a seasoned Internet developer or a neophyte, there are several features
in Visual Basic that will help you write programs for the Internet. The first of these is
the capability to create ActiveX controls, which was formerly the domain of C++
programmers. Now you can quickly write controls from within Visual Basic. You can use
these controls in your Visual Basic programs (just like regular OCXs), place them on a Web
page, or deploy them as part of an ActiveX document. These controls can work with any
browser that supports ActiveX.
Another major feature gives you the capability to create ActiveX documents. These are
applications that run inside of Internet Explorer. You can also use ActiveX documents
(whether created by Visual Basic or other products) within Visual Basic. This allows you
to run programs such as an icon editor or HTML editor from within Visual Basic as if it
were part of the development environment.
Finally, there are new controls that make it easier for you to create Internet-enabled
applications. The WebBrowser control lets you incorporate browser features into your
application. With a few lines of code, you can add a fully functional Microsoft Internet
Explorer Window to your program. The Winsock control makes it easier to connect your
application to the Internet. Finally, the Internet Transfer control helps manage the
sometimes mundane task of transferring files via two widely-used Internet transport
mechanisms: HyperText Transfer Protocol (HTTP) and File Transfer Protocol (FTP).
Control Features
What new version would be complete without new controls? A couple of the Internet
controls have already been mentioned, but there are a few other noteworthy additions.
The new MSChart control enables you to create many types of business charts in your
programs. This control can create bar, pie, line, area, and scatter charts, just to name a
few. The new MSFlexGrid control works like a regular grid but allows formatting of
individual cells. The MSFlexGrid also gives you other advanced features such as sorting,
cell grouping, pivoting, multiple selections, and in-cell pictures.
A couple of other new controls also deserve mentioning:
- Animation control This allows you to display silent AVI (Audio Video Interleaved)
clips. AVI clips are a series of bitmaps that are viewed like a movie. With this control,
you can add a continuous animation much like the "flying paper" displayed as
Windows 95 copies files from one folder to another.
- UpDown control This is a pair of arrow buttons that the user can click with the
mouse to modify the value of a number in a "buddy" control. For example, without
writing any code you can combine a TextBox control with arrow buttons that modify its
contents.
- Internet Transfer control This allows you to send and retrieve files on HTTP and
FTP servers.
In addition to the new controls, several of the old controls have been enhanced to
provide greater functionality. For example, the PictureBox and Image controls now have the
capability to display GIF or JPEG files, formats that are commonly used on the Internet.
Also, most controls now have a ToolTipText property, which allows you to specify
text that appears when the mouse pointer is rested on the control.
Finally, if you haven't found what you need among the provided controls, Visual Basic
gives you the capability to create your own ActiveX controls and Dynamic Link Libraries
(DLLs).
Checking Out the Wizards
Visual Basic has had a Setup Wizard for a number of versions. This made it easy to
create the distribution disks that you needed in order to pass your program on to your
users. The Setup Wizard is still present in version 5, but with some nice enhancements.
Now you can create setup programs to handle installations from floppy disks or CDs, across
a network, or even from the Internet. In addition to the Setup Wizard, Visual Basic
includes several other wizards to make it easier for you to develop programs. These
include the following:
- Application Wizard Creates a fully functional application that includes a toolbar
and status bar on the main form. It can include such specialized forms as a splash screen,
a login screen, an options dialog, and an About box. You can then customize the code
created by the wizard to meet your programming needs.
- ActiveX Control Interface Wizard Helps you create the public code interface of an
ActiveX control.
- ActiveX Document Migration Wizard Helps you create an ActiveX document from your
existing forms. ActiveX documents can be run in Internet Explorer and other
ActiveX-enabled programs.
- Data Form Wizard Creates a basic data entry form based on the structure of a
table in a database. This fully functional form can be used as-is or modified to handle
more complex database programming.
- PropertyPage Wizard Helps you build property pages for the ActiveX controls you
create.
Types of Programs You Can Create in Visual Basic
Visual Basic's version 5 lets you create many different types of 32-bit programs for
the Windows operating systems. (A few of these types of programs are listed in the
Introduction.)
NOTE: Programs created with Visual Basic 5.0 are
32-bit applications and can only be run on either the Windows NT or Windows 95 operating
systems. They cannot be run on 16-bit Windows systems.
While you will most likely create stand-alone programs that are used directly by end
users, Visual Basic 5 also gives you the ability to create libraries of functions that can
be compiled into DLL files. These functions can be used by other programs to handle
specialized tasks. In addition, Visual Basic 5 allows you to create your own ActiveX
components, which can be used by your programs, other programs, and even accessed over the
Internet.
Checking Out Visual Basic's Background
Before diving into the details of Visual Basic, let's take a brief look at the history
of pro-gramming and some of the basic concepts that apply to programming in any language.
This understanding of program basics will make it easier for you to write better and more
efficient programs.
What Is a Program?
To begin, you eed to know the answer to the question, "Just what is a
program?" A computer program is simply a set of instructions that tells the
computer how to perform a specific task.
Computers need explicit instructions for every single task they perform. They even need
instructions for the simplest tasks, such as how to get a keystroke, place a letter on the
screen, or store information to a disk. Fortunately, many of these instructions are
contained on the processor chip or are built into the operating system, so you don't have
to worry about them.
Even with less advanced programming languages, you must still be concerned with mundane
tasks such as drawing command buttons, repainting screens, and so on. Visual Basic takes
care of much of this detail work for you. Instead, you'd rather concentrate on providing
instructions for the tasks, such as calculating employee payroll, creating the mailing
list for your neighborhood, or formatting text to display the information in the latest
annual report. Visual Basic 5 and the Rapid Application Development (RAD) concept make
this possible.
NOTE: Rapid Application Development means exactly
what it sounds like: an environment that lets you develop applications rapidly! Visual
Basic makes it easy to spend your programming time creating a good program without having
to worry about mundane "behind-the-scenes" details.
For example, if you want to use a command button in a program, all you have to do is
draw one on a form (screen) as you're designing the program. You then set its properties
to define how it looks and acts; you could also write small pieces of program code to tell
it what to do when it's clicked. You don't have to be concerned with how the button is
actually painted on the screen; for example, the button looks like it's pushed down.
Visual Basic takes care of these lower-level tasks for you.
A Few Definitions
Now for a few technical terms. You will hear these terms often in discussions of Visual
Basic, so a general understanding of the following terms should be helpful to you:
- Controls Reusable objects that provide the pieces of the visual interface of a
program. Examples of controls are a text box, a label, or a command button.
- Event An action initiated by the user, the operating system, or the program
itself. Examples of events are a keystroke, a mouse click, the expiration of a specified
amount of time, or the receipt of data from a port.
- Methods Predefined actions that can be performed by an object. For example, a
form has a Hide method that makes it invisible to the user.
- Object A basic element of a program, which contains properties to define its
characteristics, contains methods to define its tasks, and recognizes events to which it
can respond. Controls and forms are examples of the objects used in Visual Basic.
- Procedures Segments of code that you write to accomplish a task. Procedures are
often written to respond to a specific event. Types of procedures include Sub procedures,
which consist of a sequence of statements; and Functions, which return a value.
- Properties The characteristics of an object, such as its size, position, color,
or text font. Properties determine the appearance and sometimes the behavior of an object.
Properties are also used to provide data to an object and to retrieve information from the
object.
Event-Driven Programming
Visual Basic lets you create programs that respond to user actions and system events.
This type of programming is known as event-driven programming. To get some insight
into how event-driven programming works, let's take a look at how programs ran in the past
and how things are different in the Windows environment.
See "Handling Events in Your Programs," Chapter 7
Before the advent of Windows (back in the old days of DOS and the "prehistoric
times"--before PCs), programs were written to be run in a sequential fashion.
That is, when the program started, it proceeded, instruction by instruction, until it
reached the end of the program or a fatal error occurred. The general steps for running a
program that processed a data file were as follows:
- 1. Create an input file.
2. Start the program.
3. Wait until the program finishes--often overnight for large programs.
4. Examine the output file or printed report.
5. Check the output for errors and, if necessary, repeat the entire process.
As software matured, we saw the gradual introduction of a limited form of interactive
processing, in which the user had some control over the sequence of events. Such
improvements as menu systems and hot-key commands allowed a little more flexibility to the
user, but the program itself still retained much control over its flow.
Even programs that most people take for granted, such as word processing packages,
worked this way. For the early word processors, you would create your file with formatting
codes embedded in the actual document (much like RTF or HTML codes today), and then run
the file through a formatter to be printed. These programs got the job done, but they
weren't nearly as easy to use as today's programs. They were, however, easier to write.
This is because each program had a clearly defined task and little or no user interaction.
The programmer had nearly complete control over the sequence of events required to
complete a task.
Then came Windows. Windows programs (and many later-generation DOS programs) provided
the user with the ability to interact with the objects that made up the program's
interface to a much greater extent. Programs would now respond to occurrences such as
mouse movements and clicks, and would respond differently depending upon where the mouse
pointer was located. Because this sequential programming structure would no longer work in
such a wide-open environment, a new model was needed: the event model.
In the event model, each user interaction--such as a mouse click or a keystroke--is
known as an event. Therefore, programs that respond to these events are known as event-driven
programs. These programs provide almost immediate feedback to the users and give them
greater control over the programs' activity. For example, an order-taking program written
in a sequential processing style might ask the users for information about the items they
are ordering one line at a time. They must follow the sequence of events determined by the
programmer; if they were to make an error, they would have to start over. An event-driven
application, on the other hand, might present the users with a visual form to fill out, as
well as several processing options. Controls such as command buttons, menus, and text
boxes would allow the users to determine the order in which they input the data and when
the program processes the data. Correcting an error would be as simple as retyping the
erroneous information. Figure 1.4 shows the interface for a typical event-driven program.
FIG. 1.4
In an event-driven program, the user might have several options governing the flow
of events.
While event-driven programs are great for users, they were very difficult for
developers to write until the introduction of advanced programming languages like Visual
Basic. Visual Basic was designed with the event model in mind, making it much easier for
people to create Windows applications.
Object-Oriented Programming
One key concept that makes it easier to create Windows programs is object-oriented
programming, or OOP. This technology makes it possible to create reusable
components that become the building blocks of programs.
Figure 1.5 shows the interface for a typical object-oriented program.
FIG. 1.5
In an object-oriented program, the user has an interactive order form.
What Is Object-Oriented Programming? The OOP model provides support for three
basic principles--encapsulation, inheritance, and polymorphism. Let's
take a brief look at each of these terms.
Encapsulation means that the information about an object (its properties) and
the processes that are performed by the object (its methods) are all contained
within the definition of the object. A real-world example of an object is a car. You
describe a car by its properties, such as a red convertible or a black four-door sedan.
Each characteristic--color, number of doors, convertible, or hardtop--is a property of the
car. As for the methods, these are the things that a car does in response to an event. For
example, you initiate an event when you turn the key to start the car. The car's
"start method" takes over at that point, providing instructions such as
"engage the starter gear, turn the starter, start fuel flow, initiate power to spark
plugs, and disengage the starter." You don't have to tell the car how to start
because it was taught how to start when it was designed.
Inheritance means that one object can be based upon the description of another object.
Continuing with the car example, I can define a car as something that has four wheels, an
engine, and passenger seats. I can then define a convertible as a car that has a
retractable top. The convertible inherits the properties of the car and adds a new
property, the retractable top. I don't have to redefine the car's properties for the
convertible. Therefore, the convertible is said to inherit the properties of the car. In
addition to properties, objects can also inherit methods and events from other objects.
NOTE: This discussion of inheritance is included
for the purpose of fully describing OOP. Visual Basic does not directly support
inheritance in its implementation of object-oriented programming.
P olymorphism means that many objects can have the same method and that the appropriate
action is taken for the specific object calling the method. For example, in your programs,
you display text to the screen and output text to the printer. Each of these objects (the
screen and the printer) can have a print or display method that tells the object to place
text in a certain location. The method knows what to do, based on the object calling the
method.
What OOP Does for You The key element of OOP with which you will be working is
reusable components, known as controls. The controls that you will use in building
your programs are objects that have properties and methods and respond to events. You
control the appearance and behavior of a control through its properties. For example, you
specify how the text in a TextBox control will look by setting its Font and Color
properties. The controls you use have methods built into them that shield you from many of
the tedious tasks of programming. Again look at the TextBox control as an example. It
knows how to retrieve a keystroke and display it in the edit region of the box in the
proper format. You don't have to supply the details. Each control also recognizes specific
events. Most controls know if the mouse has been moved over them or if a mouse button has
been clicked. They even know which button was clicked. Components that handle text know
when a key was pressed and which one it was. And, for most events, you can write code that
will take specific action when the event occurs.
The Parts of a Program
As you begin to create a program, there are three basic parts of the program that you
need to consider--the user interface, the processing of information, and the
storage of information.
The user interface is the part of the program that your users see and with which they
interact. This user interface is composed of the screens you design by using Visual
Basic's forms and controls. A few key objectives for a good user interface are the
following:
- Present information in a neat manner.
- Make instructions clear.
- Make the appropriate parts of the interface (such as menus) consistent with
corresponding parts of other programs.
- Make key tasks easily accessible by providing menu shortcuts and/or toolbars.
The processing of information is handled by the code that you write to respond to
events in the program. One of your objectives here is to make the code as efficient as
possible, thereby providing good response time for your users. It's also important to make
the code easy to maintain so that future modifications or updates to the code can be made
with relative ease. Key components in making code easy to maintain include making it easy
to read and using code modules to keep individual tasks small and simple. Making your code
easy to read is done by properly formatting the lines of code and providing comments
within the code to describe what it does.
From Here...
Now that you have been introduced to some of Visual Basic's capabilities, you are
probably ready to jump right in and get started creating programs. A few chapters you
might want to explore include the following:
- See Chapter
2, "Introduction to the Development Environment," to learn more about Visual
Basic 5's IDE (Integrated Development Environment).
- See Chapter
3, "Creating Your First Program," to get started creating program
interfaces.
- See Chapter
8, "Programming Visual Basic," for more information about writing program
code to have your programs perform their assigned tasks.
|