Most of us learn to cook at an early age, starting with peanut butter and jelly sandwiches and quickly progressing to macaroni and cheese. But very few people make it to the advanced cooking stage, whipping up new culinary creations in the blink of an eye and dazzling all who taste the literal fruits of our labor. And for most of us, that's OK. We don't expect any more than the ordinary, the mundane, on our daily plates.
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 worse: bugs. So how can you take your programming to the "master chef" level?
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 ingredients and keep your whole project from boiling over. Or maybe you are trying to format some content for the printer, and you want to draw a text string at a 27.3-degree angle. If so, you are likely hungry for what's in Recipe 9.18, "Rotating Text to any Angle."
In the world of cooking, regular practice brings noticeable improvements in the quality of your food. It is our belief that regular programming practice using the recipes in this book will bring similar improvements in your software development life. Bon appétit!
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 full-time developer. No matter what your level of expertise, you will benefit from the recipes found in each chapter. But even the simplest recipes assume a minimum understanding of Visual Basic and .NET programming concepts. If you are a first-time programmer still learning the basics of loop constructs and conditions, you might want to spend a little more time with a good tutorial book such as Programming Visual Basic 2005 by Jesse Liberty (O'Reilly) before you dive into the recipes found in these pages.
This book was written with two purposes in mind. The first was a desire to help readers expand their understanding of general and specific programming concepts and algorithms. As you read and study the recipes in this book, you should become more fluent not just in the Visual Basic language, but in the mindset that is required to develop high-quality and stable code. The book's second purpose was to help professional programmers (and also recreational programmers) become more productive by providing a collection of software answers to the questions that may stump them from time to time. If either of these purposes resonates with you and your programming needs, this book is definitely for you.
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 applaud you all the way. For most readers, the skim-and-look-up method will probably work better. The ample index pages should help you find the recipe you need quickly.
The recipes in the book are organized into general programming topic areas, by chapter. The following miniature table of contents quickly summarizes what you'll find in each chapter:
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 chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product's documentation does require permission.
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 falls outside fair use or the permission given above, feel free to contact us at email@example.com.
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 especially thank John Osborn and Ralph Davis for their constant editorial devotion and dedication to this book, from the first glimmer of interest during the "idea phase," to the final push to get every word just where it needed to be. Also essential were Caitrin McCollough and the dozens of other technically adept people who had their fingers in this project.
Jay Schmelzer and Steve Saunders provided regular and valuable feedback on all technical aspects of the book. If you find any problem with any of the code in this book, it was probably something we added in after they had a chance to review each chapter.
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 certainly the best wife anyone could find, and I sometimes feel sad for all of the other husbands who have to settle for less than what I have. And when I also take into account my son Spencer, who is just becoming a fourth grader as I write this, I truly know that I am one blessed man. It is a miracle of God that such joy comes through the two people I get to be with each and every day.
Although I see her much less often, my agent Claudette Moore is also a treasure. She lets me call her and talk about boring paperwork and new book ideas that I should get to work on later today. Thank you again for being part of the fun of writing.
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-notch group producing great technical resources.
From John Clark Craig
This has been a bittersweet year of transition for me, starting with the unexpected death of Jeanie, my wonderful wife of 34 years, soon after the first few chapters were authored. Jeanie was always supportive of my book writing, and she was very excited about this one. I know she still is.
My family and friends have been a steadfast source of joy, inspiration, and support throughout this year. Dakotah and Makayla are the best grandkids a guy could ever hope for, and all my parents, siblings, and in-laws have been there for me when I needed them most.
My fiancée 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 excellent set of programming tools for today's software development needs. In particular, the recent decision to make Visual Basic 2005 Express "free forever" to the public was a smart win/win decision for us all.