Copyright 2006 O'Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( safari.oreilly.com ). For more information, contact our corporate/institutional sales department: (800) 998-9938 or email@example.com.
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. The
Visual Basic 2005 Cookbook
, the image of a bream, and
Microsoft, MSDN, the .NET logo, Visual Basic, Visual C++, Visual Studio, and Windows are registered trademarks of Microsoft Corporation.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
In loving memory of Jeanie Craig (19502005).
Jeanie… Thank you for sharing your life, your spirit, and your love. Our dreams go on, and our love is eternal .
To my parents, Don and Darla, who both know how to cook .
Most of us learn to cook at an early age, starting with
But in the programming world, things are different. Users expect the moon when it comes to software quality, and if you can't deliver something more than just the mundane, you're sure to get an earful. Visual Basic 2005 is a good tool for writing great programs, but it is also very good at letting people write ordinary software. Ordinary cooking usually only results in ordinary food, but ordinary programming can lead to something much
For those of you who have limited creative talent in the kitchen, and even for experts who are just looking to try something new, there are cookbooks galore at your local bookstore and smiling chefs each Saturday morning on your local PBS station. For Visual Basic programmers, locating a similar type of cookbook of "programming recipes" has been somewhat difficultuntil now. The book you are now holding, Visual Basic 2005 Cookbook , is the recipe book you have been looking for. It's chock full of tasty software development tidbits that you can try right now in your kitchenthat is, at your computer.
The recipes in this cookbook will introduce you to a wide range of Visual Basic 2005 programming topics, from simple string and number manipulation to advanced topics involving animations and matrix transformations. Some of the recipes may not be to your taste, but many of them will be just what you need. Perhaps you're in the middle of a meaty project, and you aren't sure how to copy an existing directory from one place to another. A quick look at Recipe 12.4, "Copying Directories," will provide the missing
In the world of cooking, regular practice
Who This Book Is For
Visual Basic 2005 Cookbook
includes a large variety of recipes, and it was written to meet the needs of a wide range of software developers, from the novice programmer trying out new code to the professional
This book was written with two purposes in mind. The first was a
How This Book Is Organized
Visual Basic 2005 Cookbook
is primarily a reference book. Each recipe is organized as a problem/solution pair: you have a problem, you locate a recipe that concerns your problem, and then you obtain the solution through the code and discussion included with the recipe. If you are the adventurous type, you can read through the book from cover to cover, and we will
The recipes in the book are organized into general programming topic areas, by chapter. The following
Most of the book's recipes include source code you can use in your own applications. Some of the code samples are rather long, and typing them in while reading this book would be a chore. That's why we've made the source code for most recipes available as a separate download from the O'Reilly Media web site. To access the code, locate this book's web page at http://www.oreilly.com/catalog/vb2005ckbk/.
What You Need to Use This Book
The recipes included in this book were designed specifically for use with Visual Basic 2005 or later. While some of the more general recipes will work with earlier versions of Visual Basic .NET, many other recipes will generate compile-time or runtime errors if you attempt to use them with earlier versions.
If you do not yet own a copy of Visual Studio 2005, and you aren't sure if you are ready to make the financial investment to obtain it, you can use the free version of the development environment, Microsoft Visual Basic 2005 Express Edition. Although this version does not include all of the features included with the Standard, Professional, and Enterprise editions of the product, you will be able to use most of the recipes in this book with it.
Microsoft Visual Basic 2005 Express Edition can build only desktop applications. If you are looking for a no-cost tool for ASP.NET application development, try the Microsoft Visual Web Developer 2005 Express Edition.
Both Express Edition tools are available from Microsoft's MSDN web site at http://msdn.microsoft.com/express/.
The recipes in this book were all developed using Visual Studio 2005 Professional Edition.
Conventions Used in This Book
The following typographical conventions are used in this book:
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: " Visual Basic 2005 Cookbook by Tim Patrick and John Clark Craig. Copyright 2006 O'Reilly Media, Inc., 978-0-596-10177-0."
If you feel your use of code examples
Comments and Questions
Please address comments and questions concerning this book to the publisher:
We have a web page for this book that lists errata, examples, and any additional information. You can access this page at:
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the O'Reilly Network, see our web site at:
When you see a Safari Enabled icon on the cover of your favorite technology book, that means the book is available online through the O'Reilly Network Safari Bookshelf.
Safari offers a solution that's better than e-books. It's a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.
For those of you who plan on writing a computer book some day, we wholeheartedly recommend O'Reilly Media for your publishing consideration. It produces great booksincluding this oneyear after year, books that regularly meet the computing needs of readers. And here is why: the people who work at O'Reilly aren't just book publishers, they are technology lovers. They actually understand and try out the code included in their books.
The authors wish to
Jay Schmelzer and Steve Saunders provided regular and
From Tim Patrick
Once again my family has been incredibly patient with me as I spent time playing with the computer. You would think that there would be a limit on how many times a person can hear me say "Just one more paragraph," and still love me. But they do.
My beautiful wife Maki is
Although I see her much less often, my agent Claudette Moore is also a
Thanks to John Craig, John Osborn, Ralph Davis, and the team at O'Reilly for trusting me with a portion of this book's content. As everyone in the computer industry already knew, O'Reilly Media is a top-
From John Clark Craig
This has been a
My family and
My fiance EJ Thornton has been an absolute angel, and a bright guiding light in my life for the past few months. Thank you EJ for bringing a renewal of meaning and purpose to my life, and thank you Jeanie for blessing us and for bringing us together!
I owe a huge debt of gratitude to Tim Patrick for jumping in with his tremendous talent to help create this book, to Ralph Davis for his great editorial skills and emotional support (Ralph's wife passed away recently, too), to John Osborn for his nearly infinite patience, understanding, and guidance on this project, and to everyone else involved at O'Reilly.
Finally, I want to thank Microsoft for creating an