Thông tin tài liệu
Using Your Sybex Electronic Book
T
o realize the full potential of this Sybex electronic book, you must have Adobe Acrobat Reader with
Search installed on your computer. To find out if you have the correct version of Acrobat Reader, click on
the Edit menu—Search should be an option within this menu file. If Search is not an option in the Edit
menu, please exit this application and install Adobe Acrobat Reader with Search from this CD (double-
click on rp500env.exe in the Adobe folder).
Navigation
To search, click the Search Query button on the toolbar
or choose Edit >Search > Query to open the Search window. In
the Adobe Acrobat Search dialog’s text field, type the text you
want to find and click Search.
Use the Search Next button (Control+U) and Search
Previous button (Control+Y) to go to other matches in
the book. The Search command also has powerful tools for
limiting and expanding the definition of the term you are
searching for. Refer to Acrobat's online Help (Help > Plug-In
Help > Using Acrobat Search) for more information.
www.sybex.com
Click here to begin using
your Sybex Electronic Book!
Search
Navigate throught the book by clicking on the headings that appear in the left panel;
the corresponding page from the book displays in the right panel.
Mastering
™
Visual Basic
®
.NET
Evangelos Petroutsos
San Francisco London
2877c00.qxd 11/11/01 4:13 PM Page iii
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
Associate Publisher: Richard Mills
Acquisitions Editor: Denise Santoro Lincoln
Developmental Editor: Tom Cirtin
Editors: Pete Gaughan, Linda Recktenwald
Production Editor: Kylie Johnston
Technical Editors: Jesse Patterson, Greg Guntle
Book Designer: Maureen Forys, Happenstance Type-O-Rama
Graphic Illustrator: Tony Jonick
Electronic Publishing Specialist: Maureen Forys, Happenstance Type-O-Rama
Proofreaders: Nanette Duffy, Amey Garber, Dave Nash, Laurie O’Connell, Yariv Rabinovitch, Nancy Riddiough
Indexer: Ted Laux
CD Coordinator: Christine Detlefs
CD Technician: Keith McNeil
Cover Designer: Design Site
Cover Illustrator/Photographer: Sergie Loobkoff
Copyright © 2002 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. The author created reusable code in
this publication expressly for reuse by readers. Sybex grants readers limited permission to reuse the code found in this publication or its accompa-
nying CD-ROM so long as the author is attributed in any application containing the reusable code and the code itself is never distributed, posted
online by electronic transmission, sold, or commercially exploited as a stand-alone product. Aside from this specific exception concerning reusable
code, no part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photo-
copy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher.
Library of Congress Card Number: 2001094602
ISBN: 0-7821-2877-7
SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries.
Mastering is a trademark of SYBEX Inc.
Screen reproductions produced with FullShot 99. FullShot 99 © 1991–1999 Inbit Incorporated. All rights reserved.
FullShot is a trademark of Inbit Incorporated.
The CD interface was created using Macromedia Director, COPYRIGHT 1994, 1997–2001 Macromedia Inc. For more information on
Macromedia and Macromedia Director, visit www.macromedia.com.
Internet screen shot(s) using Microsoft Internet Explorer reprinted by permission from Microsoft Corporation.
TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the
capitalization style used by the manufacturer.
The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible.
Portions of the manuscript may be based upon pre-release versions supplied by software manufacturer(s). The author and the publisher make no
representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kind
including but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or
alleged to be caused directly or indirectly from this book.
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
2877c00.qxd 11/12/01 2:55 PM Page iv
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
The media and/or any online materials accompanying this book that
are available now or in the future contain programs and/or text files
(the “Software”) to be used in connection with the book. SYBEX
hereby grants to you a license to use the Software, subject to the terms
that follow. Your purchase, acceptance, or use of the Software will
constitute your acceptance of such terms.
The Software compilation is the property of SYBEX unless otherwise
indicated and is protected by copyright to SYBEX or other copyright
owner(s) as indicated in the media files (the “Owner(s)”). You are
hereby granted a single-user license to use the Software for your per-
sonal, noncommercial use only. You may not reproduce, sell, distribute,
publish, circulate, or commercially exploit the Software, or any portion
thereof, without the written consent of SYBEX and the specific copy-
right owner(s) of any component software included on this media.
In the event that the Software or components include specific license
requirements or end-user agreements, statements of condition, dis-
claimers, limitations or warranties (“End-User License”), those End-
User Licenses supersede the terms and conditions herein as to that
particular Software component. Your purchase, acceptance, or use of the
Software will constitute your acceptance of such End-User Licenses.
By purchase, use or acceptance of the Software you further agree to
comply with all export laws and regulations of the United States as
such laws and regulations may exist from time to time.
REUSABLE CODE IN THIS BOOK
The author created reusable code in this publication expressly for reuse
for readers. Sybex grants readers permission to reuse for any purpose
the code found in this publication or its accompanying CD-ROM so
long as the author is attributed in any application containing the
reusable code, and the code itself is never sold or commercially
exploited as a stand-alone product.
SOFTWARE SUPPORT
Components of the supplemental Software and any offers associated
with them may be supported by the specific Owner(s) of that material,
but they are not supported by SYBEX. Information regarding any
available support may be obtained from the Owner(s) using the
information provided in the appropriate read.me files or listed else-
where on the media.
Should the manufacturer(s) or other Owner(s) cease to offer support
or decline to honor any offer, SYBEX bears no responsibility. This
notice concerning support for the Software is provided for your
information only. SYBEX is not the agent or principal of the
Owner(s), and SYBEX is in no way responsible for providing any sup-
port for the Software, nor is it liable or responsible for any support
provided, or not provided, by the Owner(s).
WARRANTY
SYBEX warrants the enclosed media to be free of physical defects for a
period of ninety (90) days after purchase. The Software is not available
from SYBEX in any other form or media than that enclosed herein or
posted to www.sybex.com. If you discover a defect in the media during
this warranty period, you may obtain a replacement of identical format
at no charge by sending the defective media, postage prepaid, with
proof of purchase to:
SYBEX Inc.
Product Support Department
1151 Marina Village Parkway
Alameda, CA 94501
Web: www.sybex.com
After the 90-day period, you can obtain replacement media of identi-
cal format by sending us the defective disk, proof of purchase, and a
check or money order for $10, payable to SYBEX.
DISCLAIMER
SYBEX makes no warranty or representation, either expressed or
implied, with respect to the Software or its contents, quality, per-
formance, merchantability, or fitness for a particular purpose. In no
event will SYBEX, its distributors, or dealers be liable to you or any
other party for direct, indirect, special, incidental, consequential,
or other damages arising out of the use of or inability to use the Soft-
ware or its contents even if advised of the possibility of such damage.
In the event that the Software includes an online update feature,
SYBEX further disclaims any obligation to provide this feature for any
specific duration other than the initial posting.
The exclusion of implied warranties is not permitted by some states.
Therefore, the above exclusion may not apply to you. This warranty
provides you with specific legal rights; there may be other rights that
you may have that vary from state to state. The pricing of the book with
the Software by SYBEX reflects the allocation of risk and limitations
on liability contained in this agreement of Terms and Conditions.
SHAREWARE DISTRIBUTION
This Software may contain various programs that are distributed as
shareware. Copyright laws apply to both shareware and ordinary
commercial software, and the copyright Owner(s) retains all rights. If
you try a shareware program and continue using it, you are expected to
register it. Individual programs differ on details of trial periods, regis-
tration, and payment. Please observe the requirements stated in
appropriate files.
COPY PROTECTION
The Software in whole or in part may or may not be copy-protected
or encrypted. However, in all cases, reselling or redistributing these
files without authorization is expressly forbidden except as specifically
provided for by the Owner(s) therein.
Software License Agreement: Terms and Conditions
2877c00.qxd 11/11/01 4:13 PM Page v
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
To my family
2877c00.qxd 11/11/01 4:13 PM Page vii
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
Acknowledgments
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 produc-
tion 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.
2877c00.qxd 11/12/01 7:39 PM Page viii
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
Contents at a Glance
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Part I • The Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1 • Getting Started with VB.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2 • Visual Basic Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Chapter 3 • Visual Basic: The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Chapter 4 • Writing and Using Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Chapter 5 • Working with Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Chapter 6 • Basic Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Chapter 7 • More Windows Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Part II • Rolling Your Own Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Chapter 8 • Building Custom Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Chapter 9 • Building Custom Windows Controls . . . . . . . . . . . . . . . . . . . . . . . 391
Chapter 10 • Automating Microsoft Office Applications . . . . . . . . . . . . . . . . . . 433
Part III • Basic Framework Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Chapter 11 • Storing Data in Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Chapter 12 • Handling Strings, Characters, and Dates . . . . . . . . . . . . . . . . . . . . 529
Chapter 13 • Working with Folders and Files . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Part IV • Intermediate Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Chapter 14 • Drawing and Painting with Visual Basic . . . . . . . . . . . . . . . . . . . . 619
Chapter 15 • Printing with VB.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Chapter 16 • The TreeView and ListView Controls . . . . . . . . . . . . . . . . . . . . . 741
2877c00.qxd 11/11/01 4:13 PM Page ix
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
Chapter 17 • Error Handling and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Chapter 18 • Recursive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Chapter 19 • The Multiple Document Interface . . . . . . . . . . . . . . . . . . . . . . . . 837
Part V • Database Programming with VB.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867
Chapter 20 • Databases: Architecture and Basic Concepts . . . . . . . . . . . . . . . . . 869
Chapter 21 • Building Database Applications with ADO.NET . . . . . . . . . . . . . 925
Chapter 22 • Programming the ADO.NET Objects . . . . . . . . . . . . . . . . . . . . . 963
Part VI • VB.NET on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
Chapter 23 • Introduction to Web Programming . . . . . . . . . . . . . . . . . . . . . . . 999
Chapter 24 • Accessing Data on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
Chapter 25 • XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1099
CONTENTS AT A GLANCE
x
2877c00.qxd 11/11/01 4:13 PM Page x
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
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, how-
ever, 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 Win-
dows 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 pic-
ture (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 argu-
ments. 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
2877c00.qxd 11/11/01 4:13 PM Page xxiii
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
INTRODUCTION
xxiv
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 key-
words 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. Fol-
lowing 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 chap-
ters. 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 tech-
niques 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.
2877c00.qxd 11/11/01 4:13 PM Page xxiv
Copyright ©2002 SYBEX, Inc., Alameda, CA
www.sybex.com
[...]... provides tools for designing, executing, and debugging your applications It’s your second desktop, and you’ll be spending most of your productive hours in this environment The Start Page When you run Visual Studio for the first time, you will see the window shown in Figure 1.1 On the My Profile tab, you will set your personal preferences by specifying your language Select Visual Basic Developer in the... 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 Copyright ©2002 SYBEX, Inc., Alameda, CA www .sybex. com xxvii 2877c01.qxd 11/11/01 4:14 PM Page 1 Part I The Fundamentals In this section: N N N N N N... and Using Procedures Working with Forms Basic Windows Controls More Windows Controls Copyright ©2002 SYBEX, Inc., Alameda, CA www .sybex. com 2877c01.qxd 11/11/01 4:14 PM Page 3 Chapter 1 Getting Started with VB.NET Welcome to the Enterprise Edition of Visual Basic NET I’m assuming you have installed Visual Studio NET, Enterprise Edition You may have even already explored the new environment on your. .. 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, ... form The controls on these tabs are examined in Part VI of the book Copyright ©2002 SYBEX, Inc., Alameda, CA www .sybex. com 23 2877c01.qxd 24 11/11/01 4:14 PM Page 24 Chapter 1 GETTING STARTED WITH VB.NET The Data tab contains the icons of the objects you will use to build data-driven applications, and they’re explored in Part V of the book The items on the Data tab are objects with no visible interface... 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,... 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... hope to cover The first thing you must learn is the environment you’ll be working in from now on In the first chapter of this book, you’ll familiarize yourself with the integrated development environment (IDE) and how its tools allow you to quickly design the user interface of your application, as well as how to program the application It will be a while before you explore all the items of the IDE Visual... the Copyright ©2002 SYBEX, Inc., Alameda, CA www .sybex. com 2877c00.qxd 11/11/01 4:13 PM Page xxvii INTRODUCTION 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... window of your application—what users will see on their desktop when they run your application Open the File menu and select New ➢ Project In the New Project dialog box (Figure 1.2), you will see a list of project types you can create with Visual Studio Select the Windows Application Copyright ©2002 SYBEX, Inc., Alameda, CA www .sybex. com 5 2877c01.qxd 6 11/11/01 4:14 PM Page 6 Chapter 1 GETTING STARTED . Using Your Sybex Electronic Book T o realize the full potential of this Sybex electronic book, you must have Adobe Acrobat Reader with Search installed on your computer. To. (Help > Plug-In Help > Using Acrobat Search) for more information. www .sybex. com Click here to begin using your Sybex Electronic Book! Search Navigate throught the book by clicking on the. 2001094602 ISBN: 0-7821-2877-7 SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries. Mastering is a trademark of SYBEX Inc. Screen
Ngày đăng: 31/03/2014, 22:20
Xem thêm: Using Your Sybex Electronic Book pot, Using Your Sybex Electronic Book pot, Chapter 1: Getting Started with VB.NET, VB.NET at Work: Creating a Windows Installer, Chapter 21: Building Database Applications with ADO. NET, Chapter 22: Programming the ADO. NET Objects, ASP. NET Web Service Projects