Table of Contents
| Introduction | 1 |
| Ch. 1 | Welcome to Visual Basic .NET | 7 |
| Ch. 2 | The Microsoft .NET framework | 35 |
| Ch. 3 | Writing software | 47 |
| Ch. 4 | Controlling the flow | 89 |
| Ch. 5 | Working with data structures | 131 |
| Ch. 6 | Building Windows applications | 179 |
| Ch. 7 | Displaying dialog boxes | 221 |
| Ch. 8 | Creating menus | 261 |
| Ch. 9 | Debugging and error handling | 293 |
| Ch. 10 | Building objects | 323 |
| Ch. 11 | Advanced object-oriented techniques | 363 |
| Ch. 12 | Building class libraries | 407 |
| Ch. 13 | Creating your own custom controls | 427 |
| Ch. 14 | Programming custom graphics | 453 |
| Ch. 15 | Accessing databases | 499 |
| Ch. 16 | Database programming with SQL server and ADO.NET | 527 |
| Ch. 17 | Web forms | 583 |
| Ch. 18 | Creating web server controls | 629 |
| Ch. 19 | Visual Basic .NET and XML | 643 |
| Ch. 20 | Web services and .NET remoting | 683 |
| Ch. 21 | Deploying your application | 723 |
| Ch. 22 | Building mobile applications | 741 |
| App. A | Where to now? | 758 |
| App. B | Architecture and design patterns | 761 |
| App. C | An introduction to code security | 767 |
| App. D | Exercise answers | 775 |
| App. E | Differences between .NET framework versions 1.0 and 1.1 | 785 |
| Index | 791 |
Read a Sample Chapter
Beginning VB.NET
By Richard Blair Jonathan Crossland Matthew Reynolds Thearon Willis
John Wiley & Sons
Copyright © 2002
Richard Blair, Jonathan Crossland, Matthew Reynolds, Thearon Willis
All right reserved.
ISBN: 0-7645-4384-9
Chapter One
Welcome to Visual Basic .NET
The goal of this book is to help you come up to speed with the Visual Basic .NET language even if you
have never programmed anything before. We will start slowly, and build on what we learn. So take a
deep breath, let it out slowly, and tell yourself you can do this. No sweat! No kidding!
Programming a computer is a lot like teaching a child to tie their shoes. Until you find the correct way
of giving the instructions, not much gets accomplished. Visual Basic .NET is a language in which you
can tell your computer how to do things. But like a child, the computer will only understand if you
explain things very clearly. If you have never programmed before, this sounds like an arduous task, and
sometimes it is. However, Visual Basic .NET gives you a simple language to explain some very complex
things. Although it never hurts to have an understanding of what is happening at the lowest levels,
Visual Basic .NET frees the programmer from having to deal with the mundane complexities of writing
Windows programs. You are free to concentrate on solvingproblems.
Visual Basic .NET helps you create solutions that run on the Microsoft Windows operating system.
Chances are, if you are looking at this book, you have already felt the need or the desire to create such
programs. Even if you have never written a computer program before, as you progress through the Try
It Outs in this book, you will become familiar with the various aspects of the Visual Basic .NET
language, as well as its foundation in Microsoft's .NET Framework. You will find that it is not nearly as
difficult as you have been imagining. Before you know it, you will be feeling quite comfortable creating
a variety of different types of programs with Visual Basic .NET. As the name implies, Visual Basic .NET
can be used to create applications for use over the Internet. However, as when learning any new
technology, you have to walk before you can run, so we will start out focusing on Windows applications,
before extending our boundaries.
Windows Versus DOS Programming
A Windows program is quite a bit different from its ancient relative, the MS-DOS program. A DOS
program follows a relatively strict path from beginning to end. Although this does necessarily limit the
functionality of the program, it also limits the road the user has to take to get to it. A DOS program is like
walking down a hallway; to get to the end you have to walk down the hallway, passing any obstacles that
you may encounter. A DOS program would only let you open certain doors along your stroll.
Windows on the other hand, opened up the world of event-driven programming. Events in this context
include, for example, clicking on a button, resizing a window, or changing an entry in a textbox. The
code that you write responds to these events. To go back to the hallway analogy: in a Windows program
to get to the end of the hall, you just click on the end of the hall. The hallway can be ignored. If you get
to the end and realize that is not where you wanted to be, you can just set off for the new destination
without returning to your starting point. The program reacts to your movements and takes the necessary
actions to complete your desired tasks. Visual Basic .NET simplifies the process of writing the code to
handle each event by allowing the programmer to write code only for those events that mean something
in the context of the program. All other events are ignored. For example, Windows distinguishes clicks
from double-clicks, which means that if you only want your program to react to a single click, you need
only write code for this single click; you do not have to write code to handle both single and double-click
events.
You have probably already begun to suspect that the hallway analogy is grossly over-simplified. The
main idea here is that in the DOS world, the user reacts to the program, and in the Windows world, the
program reacts to the user.
Another big advantage in a Windows program is the abstraction of the hardware. What this means is
that Windows takes care of communicating with the hardware for you. You do not need to know the
inner workings of every laser printer on the market, just to create output. You do not need to study the
schematics for graphics cards to write your game. Windows wraps up this functionality by providing
generic routines that communicate with the drivers written by the manufacturers of hardware. This is
probably the main reason why Windows has been so successful. The generic routines are referred to as
the Windows API (Application Programming Interface).
A Brief History of Visual Basic
Before Visual Basic 1.0 was introduced to the world in 1991, developers had to be well versed in C++
programming, as well as the rudimentary building blocks (Windows API) of the Windows system itself.
This complexity meant that only the dedicated and properly trained were capable of turning out
software that could run on Windows. Visual Basic changed all of that, and it has been estimated that
there are now as many lines of production code written in Visual Basic as in any other language.
Visual Basic changed the face of Windows programming by removing the complex burden of writing
code for the user interface (UI). By allowing programmers to draw their own UI, it freed them
to concentrate on the business problems they were trying to solve. Once the UI is drawn, the programmer
can then add code to react to events.
Visual Basic has also been extensible from the very beginning. Third-party vendors quickly saw the
market for reusable modules to aid developers. These modules, or controls, were original referred to as
VBXs (named after their file extension). If you didn't like the way a button behaved you could either
buy or create your own. However, these controls had to be written in C or C++. Database access
utilities were some of the first controls available.
When Microsoft introduced Visual Basic 3.0, the programming world changed again. Now you could
build database applications directly accessible to users (so called front-end applications) completely
with Visual Basic. There was no need to rely on third-party controls. Microsoft accomplished this task
with the introduction of the Data Access Objects (DAO), which allowed programmers to manipulate
data with the same ease as manipulating the user interface.
Versions 4.0 and 5.0 extended the capabilities of version 3.0 in order to allow developers to target the
new Windows 95 platform. Crucially they also made it easier for developers to write code, which could
then be manipulated in order to be used by other language developers. Version 6.0 gave us a new way
to access databases with the integration of ActiveX Data Objects (ADO). ADO was developed by
Microsoft to aid web developers using Active Server Pages to access databases. With all of the
improvements to Visual Basic over the years, it ensured its dominant place in the programming world.
It helps developers write robust and maintainable applications in record time.
With the release of Visual Basic .NET, many of the restrictions that used to exist have been obliterated.
In the past, Visual Basic has been criticized and maligned as a "toy" language, as it did not provide all of
the features of more sophisticated languages such as C++ and Java. Now, Microsoft has removed these
restrictions and made Visual Basic .NET a very powerful development tool. Visual Basic .NET has
become a great choice for programmers of all levels.
Installing Visual Basic .NET
You may own Visual Basic .NET:
As part of Visual Studio .NET, a suite of tools and languages that also includes C#
(pronounced C-sharp) and Visual C++ .NET
Standard Edition, which includes a cut down set of the tools and languages available with
Visual Studio .NET
Both enable you to create your own applications for the Windows platform. The installation procedure
is straightforward and easy to do. In fact, the Visual Basic .NET Install is smart enough to figure out
exactly what your computer requires in order to make it work.
The descriptions that follow are based on installing Visual Studio .NET. However, all of Visual Studio
.NET's languages use the same screens and windows (and hence look very similar), so you won't be
seeing much that you would not see anyway.
Try It Out - Installing Visual Basic .NET
1. The Visual Basic .NET CD has an auto-run feature, but if the Setup screen does not appear
after inserting the CD, then you will have to run setup.exe from the root directory of the
CD. To do this, go to your Windows Start menu (usually found right at the bottom of your
screen) and select Run. Then type d:\setup.exe into the Open box, where d is the drive letter
of your CD drive. After the setup program initializes you will see the following screen:
2. This dialog box shows the order in which the installation takes place. In order to function
properly, Visual Basic .NET requires that several components and updates be installed on
your machine. Step 1 is the Windows component update, so click on the Windows
Component Update link; you will then be prompted to insert the Component Update CD that
came with your Visual Studio .NET disks.
3. The installation program will then examine your system to see exactly which components
have to be installed. Depending on the current state of your machine, this list could include
any of the following items:
Windows NT 4.0 Service Pack 6.0a
Windows 2000 Service Pack 2
Windows Installer 2.0
Windows Management Infrastructure
FrontPage 2000 Web Extensions
FrontPage 2000 Server Extensions
Setup Runtime Files
Internet Explorer 6.0 and Internet Tools
Microsoft Data Access Components 2.7
.NET Framework
If you don't know what some of those things are, don't worry about it. They are just Windows
components that Visual Studio .NET or Visual Basic .NET requires.
4. There may be numerous reboots of your computer as the system components are updated.
However, Microsoft has added a nifty automatic login feature to minimize the time required to
watch the installation if your computer requires a password to login:
As you can see, this feature is optional. If you choose not to use it, you will be prompted to
login after every reboot. This feature could provide a possible security breach, as the person
who is actually logged in does not have to be sat at the computer (so you may want to avoid
using this option in a busy office, for example), but it can reduce the boredom of watching
installation progress screens.
5. Click on Install Now! and the component update will begin. Completed items will have a
check mark and the component currently being installed will be identified by a red arrow:
6. After the Windows Component Update has finished, you will be returned to the Setup screen
once more. You will now be able to install Visual Studio .NET, so click on Visual
Studio.NET:
If at some point you wish to develop Web applications on this same machine, you will need to have
Internet Information Services (IIS) and FrontPage Extensions installed. These are installed by
default on Windows 2000 Server however, when installing on Windows 2000 Professional you may
encounter the following screen:
Selecting Install Components will guide you through the process of getting IIS and FrontPage
2000 Server Extensions installed. You will then be able to continue installing Visual Basic .NET.
7. As with most installations you will be presented with an option list of components to install.
You can just install the features that you need. For example, if your drive space is limited and
you have no immediate need for Visual C++ .NET, you can exclude it from the installation.
You will also be given the chance to select the location of items (although the defaults should
suffice unless your particular machine has special requirements). Any options not chosen at
the initial setup can always be added later as your needs or interests change.
There are three sections of information given for each feature:
The Feature properties section outlines where the required files will be installed and how
much space will be needed to do this
The Feature description box gives you an outline of what each feature is and does
Finally, the Space Allocation section illustrates how the space on your hard drive will be
affected by the installation as a whole
When you are running Visual Basic .NET, a lot of information is swapped from the disk to
memory and back again. Therefore, it is important to have some free space on your disk. There is
no exact rule for determining how much free space you will need, but if you use your machine for
development as well as other tasks, anything less than 100MB free should be considered a full disk.
8. Visual Studio .NET includes a large set of documentation files, the settings for which are
displayed under MSDN Documentation. The documentation can be set to run from the CD,
or installed directly on to your hard disk. You can set the install location as Path (install to
your hard disk) or Run from source (to have the documentation remain on the CD), via the
Feature properties box. Each piece of the documentation can be configured in this manner.
The default is for the documentation to remain on the CD. Only the indexes are copied to
your hard drive to aid in searches. This means that you will have to have the CDs available in
order to access the documentation. You are free to install some or all of MSDN onto your
hard drive. The following table lists the approximate sizes of each of the document sets by
their installed location:
As you can see there is quite an impact on your free disk space if you install all of the
documentation!
9. Once you have chosen all of the features you want, click on Install Now! Installation will
begin and you can sit back and relax for a bit. The setup time varies depending on how many
features you chose to install. As a reference, the installation process took over an hour on a
650 MHz laptop computer with 256MB RAM, a 12GB hard drive, and running Windows 2000
Server. Keep in mind that this included all of Visual Studio .NET and all of the
documentation.
Continues...
Excerpted from Beginning VB.NET
by Richard Blair Jonathan Crossland Matthew Reynolds Thearon Willis
Copyright © 2002 by Richard Blair, Jonathan Crossland, Matthew Reynolds, Thearon Willis.
Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.