Evangelos Petroutsos
SYBEX Inc.
Product Support Department
1151 Marina Village Parkway
Alameda, CA 94501
Web: www.sybex.com
Product Support Department
1151 Marina Village Parkway
Alameda, CA 94501
Web: www.sybex.com
Many people contributed to this book, and I would like to thank them all. I guess I should
start with the programmers at Microsoft, for their commitment to Visual Basic. Visual Basic has
evolved from a small, limited programming environment to a first-class development tool.
Special thanks to the talented people at Sybex—to all of them and to each one individually. I’ll
start with editor Pete Gaughan, who has taken this book personally and improved it in numerous
ways. Thanks, Pete. Thank you to developmental editor Tom Cirtin, who has followed the progress
of the book, its ups and downs, and managed to coordinate the entire team. To technical editors
Jesse Patterson and Greg Guntle for scrutinizing every paragraph and every line of code. To production
editor Kylie Johnston, who has done more than I can guess to keep this project in order and on
schedule. To designer and compositor Maureen Forys, and everyone else who added their expertise
and talent. Thank you all!
I’d like to thank and recognize Matt Tagliaferri for contributing Chapter 17, on exception handling.
I would also like to thank Alvaro Antunes and Harry Heijkoop for their helpful remarks while they
were translating earlier versions of Mastering Visual Basic into Portuguese and Dutch, respectively.
Introduction
Welcome to .NET and Visual Basic .NET. As you already know, .NET is a name for
a new strategy: a blueprint for building applications for the next decade. It’s actually even more
than that. It’s Microsoft’s commitment to remain at the top of a rapidly changing world and give
us the tools to address the needs of tomorrow’s computing. Visual Basic .NET is a language for
creating .NET applications, like many others. It also happens that Visual Basic is the easiest to
learn, most productive language (but you already know that).
Visual Basic .NET is released shortly after the tenth anniversary of the first version of VB.
The original language that changed the landscape of computing has lasted for 10 years and has
enabled more programmers to write Windows application than any other language. Programmers
who invested in Visual Basic 10 years ago are in demand today. In the world of computing, however,
things change very fast, including languages. At some point, they either die, or they evolve
into something new. Visual Basic was a language designed primarily for developing Windows
applications. It was a simple language, because it managed to hide many of the low-level details
of the operating system. Those who wanted to do more with Visual Basic had to resort to Windows
API. In a way, earlier versions of Visual Basic were ‘sandboxed’
to protect developers from scary details.
Microsoft had to redesign Visual Basic. The old language just didn’t belong in the .NET picture
(at least, it wouldn’t integrate very well into the picture). Visual Basic .NET is not VB7; it’s
a drastic departure from VB6, but a necessary departure. Visual Basic .NET was designed to take
us through the next decade of computing, and if you want to stay ahead, you will have to invest
the time and effort to learn it.
The most fundamental component of the .NET initiative is the .NET Framework, or simply
the Framework. You can think of the Framework as an enormous collection of functions for just
about any programming task. All drawing methods, for example, are part of the System.Drawing
class. To draw a rectangle, you call the DrawRectangle method, passing the appropriate arguments.
To create a new folder, you call the CreateDirectory method of the Directory class; to
retrieve the files in a folder, you call the GetFiles method of the same object. The Framework
contains all the functionality of the operating system and makes it available to your application
through numerous methods.
VB was such a success because it was a very simple language. You didn’t have to learn a lot
before you could start using the language. Being able to access the Framework’s objects means
that you’re no longer limited by the language. The new version of the language unlocks the full
potential of .NET; now there’s hardly anything you can do with another language but can’t do
with Visual Basic. This makes the language as powerful as any other language, but it also makes the
learning curve steeper. The good news is that, if you get started today, you’ll get a head start, which
may well last for another decade.
Who Should Read This Book?
You don’t need to know Visual Basic to read Mastering Visual Basic .NET, but you do need a basic
understanding of programming. You need to know the meaning of variables and functions and how
an If…Then structure works. This book is addressed to the typical programmer who wants to get the
most out of Visual Basic. It covers the topics I feel are of use to most VB programmers, and it does
so in depth. Visual Basic .NET is an extremely rich programming environment, and I’ve had to
choose between superficial coverage of many topics and in-depth coverage of fewer topics. To make
room for more topics, I have avoided including a lot of reference material and lengthy listings. For
example, you won’t find complete project listings or Form descriptions. I assume you can draw a few
controls on a Form and set their properties, and you don’t need long descriptions of the properties
of the control. I’m also assuming that you don’t want to read the trivial segments of each application.
Instead, the listings concentrate on the “meaty” part of the code: the procedures that explain the
topic at hand. If you want to see the complete listing, it’s all on the CD.
The topics covered in this book were chosen to provide a solid understanding of the principles
and techniques for developing applications with Visual Basic. Programming isn’t about new keywords
and functions. I chose the topics I felt every programmer should learn in order to master the
language. I was also motivated by my desire to present useful, practical examples. You will not find
all topics equally interesting or important. My hope is that everyone will find something interesting
and something of value to their daily work—whether it’s an application that maps the folders and
files of a drive to a TreeView control, an application that prints tabular data, or an application that
saves a collection of objects to a file.
Many books offer their readers long, numbered sequences of steps to accomplish something. Following
instructions simplifies certain tasks, but programming isn’t about following instructions. It’s
about being creative; it’s about understanding principles and being able to apply the same techniques
in several practical situations. And the way to creatively exploit the power of a language such as
Visual Basic .NET is to understand its principles and its programming model.
In many cases, I provide a detailed, step-by-step procedure that will help you accomplish a task,
such as designing a menu. But not all tasks are as simple as designing menus. I explain why things
must be done in a certain way, and I present alternatives and try to connect new topics to those
explained earlier in the book. In several chapters, I expand on applications developed in earlier chapters.
Associating new knowledge to something you have already mastered provides positive feedback
and a deeper understanding of the language.
This book isn’t about the hottest features of the language; it’s about solid programming techniques
and practical examples. For example, I’m not going to show you how to write multithreaded
applications. The real challenge with multithreaded applications is their debugging, which requires
substantial experience. Once you master the basics of programming Windows applications with
Visual Basic .NET and you feel comfortable with the more advanced examples of the book, you will
find it easy to catch up with the topics that aren’t discussed.
How About the Advanced Topics?
Some of the topics discussed in this book are non-trivial, and quite a few topics can be considered
advanced. The TreeView control, for example, is not a trivial control, like the Button or TextBox
control, but it’s ideal for displaying hierarchical information (this is the control that displays the
hierarchy of folders in Windows Explorer). If you want to build an elaborate user interface, you
should be able to program controls like the TreeView control, which is discussed in Chapter 16.
(But you need not read that chapter before you decide to use this control in a project.)
You may also find some examples to be more difficult than you expected. I have tried to make
the text and the examples easy to read and understand, but not unrealistically simple. In Chapter 13,
you’ll find information about the File and Directory objects. You can use these objects to access and
manipulate the file system from within your application, but this chapter wouldn’t be nearly as useful
without an application that shows you how to scan a folder recursively (scan the folder’s files
and then its subfolders, to any depth). To make each chapter as useful as I could, I’ve included complex
examples, which will provide a better understanding of the topics. In addition, many of these
examples can be easily incorporated into your applications.
You can do a lot with the TreeView control with very little programming, but in order to make
the most out of this control, you must be ready for some advanced programming. Nothing terribly
complicated, but some things just aren’t simple. Programming most of the operations of the Tree-
View control, for instance, is straightforward, but if your application calls for populating a TreeView
with an arbitrary number of branches (such as mapping a directory structure to a TreeView),
the code can get involved.
The reason I’ve included the more advanced examples is that the corresponding chapters would
be incomplete without them. If you find some material to be over your head at first reading, you can
skip it and come back to it after you have mastered other aspects of the language. But don’t let a few
advanced examples intimidate you. Most of the techniques are well within the reach of an average
VB programmer. The few advanced topics were included for the readers who are willing to take that
extra step and build elaborate interfaces using the latest tools and techniques.
There’s another good reason for including advanced topics. Explaining a simple topic, like how
to populate a collection with items, is very simple. But what good is it to populate a collection if you
don’t know how to save it to disk and read back its items in a later session? Likewise, what good is it
to learn how to print simple text files? In a business environment, you will most likely be asked to
print a tabular report, which is substantially more complicated than printing text. In Chapter 15 you
will learn how to print business reports with headers, footers, and page numbers, and even how to
draw grids around the rows and columns of the report.
One of my goals in writing this book was to
exhaust the topics I’ve chosen to discuss and to
present all the information you need to do something practical.
The Structure of the Book
Mastering Visual Basic .NET isn’t meant to be read from cover to cover, and I know that most people
don’t read computer books this way. Each chapter is independent of the others, although all chapters
contain references to other chapters. Each topic is covered in depth; however, I make no assumptions
about the reader’s knowledge on the topic.
As a result, you may find the introductory sections of a chapter too simple.
The topics become progressively more advanced, and even experienced programmers
will find some new information in each chapter. Even if you are familiar with the topics in a
chapter, take a look at the examples. I have tried to simplify many of the advanced topics and
demonstrate them with clear, practical examples.
VB6 ➠VB.NET
Experienced Visual Basic programmers should pay attention to these special sidebars with the “VB6 to VB.NET” icon, which calls your attention to changes in the language. These sections usually describe new features in VB.NET or enhancements of VB6 features, but also VB6 features that are no longer supported by VB.NET
This book tries to teach through examples. Isolated topics are demonstrated with short examples,
and at the end of many chapters, you’ll build a large, practical, real-world app that “puts together”
the topics and techniques discussed throughout the chapter. You may find some of the more
advanced applications a bit more difficult to understand, but you shouldn’t give up. Simpler applications
would have made my job easier, but the book wouldn’t deserve the Mastering title and your
knowledge of Visual Basic wouldn’t be as complete.
In the first part of the book, we’ll go through the fundamentals of Visual Basic .NET. You’ll
learn how to design visual interfaces with point-and-click operations, and how to program a few
simple events, like the click of the mouse on a button. After reading the first two chapters, you’ll
understand the structure of a Windows application. Then we’ll explore the elements of the visual
interface (the basic Windows controls) and how to program them.
The second part of the book is about building and using objects. Visual Basic .NET is a truly
object-oriented language, and objects are the recurring theme in every chapter. Part II is a formal
and more systematic treatment of objects. You will learn how to build custom classes and controls,
which will help you understand object-oriented programming a little better.
In the third part of the book, we’ll discuss some of the most common classes of the Framework.
The Framework is the core of .NET. It’s your gateway to the functionality of the operating system
itself, and it’s going to be incorporated into the next version of Windows. In Part III we’ll examine
collections (like ArrayLists and HashTables), the objects for manipulating files and folders, the
StringBuilder object that manipulates text, and a few more.
The fourth part of the book is a collection of intermediate to advanced topics. It includes chapters on
graphics and printing, an overview of the debugging tools, and a chapter on recursive programming—
a very powerful programming technique. You will also find a chapter on building Multiple Document
Interfaces—an interface that hosts multiple windows, each one displaying a different document.
The fifth part of the book is an overview of the data access tools. The emphasis is on the visual
tools, and you will learn how to query databases and present data to the user. You will also find
information on programming the basic objects of ADO.NET.
Part VI is about Web applications. Here you will learn the basics of ASP .NET, how to develop
Web applications, and how to write Web services. Web applications are written Visual Basic .NET,
but they deploy a user interface that consists of HTML pages and interact with the user through the
browser. Web services are functions that can be called from anywhere, and they’re one of the most
promising features of the .NET Platform.
Mastering Visual Basic .NET does not cover all the topics you can think of. I hope I’ve chosen the
topics you’ll encounter most often in your daily tasks and I’ve covered them in enough detail,
to help you understand the basics and be able to look up more specific topics in the product documentation.
How to Reach the Author
Despite our best efforts, a book this size is bound to contain errors. Although a printed medium
isn’t as easy to update as a Web site,
I will spare no effort to fix every problem you report (or I discover).
The revised applications, along with any other material I think will be of use to the readers of
this book, will be posted on the Sybex Web site.
If you have any problems with the text or the applications
in this book, you can contact me directly at pevangelos@yahoo.com.
Although I can’t promise a response to every question, I will fix any problems in the examples
and provide updated versions. I would also like to hear any comments you may have on the book,
about the topics you liked or did not like, and how useful the examples are.
Your comments will be taken into consideration in future editions.
Product details
Price
|
|
---|---|
File Size
| 18,457 KB |
Pages
|
1210 p |
File Type
|
PDF format |
ISBN
| 0-7821-2877-7 |
Copyright
| 2002 SYBEX Inc |
Contents at a Glance
Introduction
Part I • The Fundamentals
Chapter 1 • Getting Started with VB.NET
Chapter 2 • Visual Basic Projects
Chapter 3 • Visual Basic: The Language
Chapter 4 • Writing and Using Procedures
Chapter 5 • Working with Forms
Chapter 6 • Basic Windows Controls
Chapter 7 • More Windows Controls
Part II • Rolling Your Own Objects
Chapter 8 • Building Custom Classes
Chapter 9 • Building Custom Windows Controls
Chapter 10 • Automating Microsoft Office Applications
Part III • Basic Framework Classes
Chapter 11 • Storing Data in Collections
Chapter 12 • Handling Strings, Characters, and Dates
Chapter 13 • Working with Folders and Files
Part IV • Intermediate Programming
Chapter 14 • Drawing and Painting with Visual Basic
Chapter 15 • Printing with VB.NET
Chapter 16 • The TreeView and ListView Controls
Chapter 17 • Error Handling and Debugging
Chapter 18 • Recursive Programming
Chapter 19 • The Multiple Document Interface
Part V • Database Programming with VB.NET
Chapter 20 • Databases: Architecture and Basic Concepts
Chapter 21 • Building Database Applications with ADO.NET
Chapter 22 • Programming the ADO.NET Objects
Part VI • VB.NET on the Web
Chapter 23 • Introduction to Web Programming
Chapter 24 • Accessing Data on the Web
Chapter 25 • XML Web Services
Index
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●
●❂●