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:

Chapter 1, Visual Basic Programming

This chapter introduces you to the three main types of projects you will develop using Visual Basic: desktop applications, console applications, and web (ASP.NET) applications.

Chapter 2, The Development Environment

This chapter shows you how to use some of the newer features of Visual Studio and introduces the new Visual Basic 2005 My namespace feature. It also discusses development issues concerning general system resources such as the registry and the clipboard.

Chapter 3, Application Organization

The recipes in this chapter cover the core programming concepts found in classes, structures, and modules, including method usage and operator overloading.

Chapter 4, Forms, Controls, and Other Useful Objects

As the chapter title implies, the recipes found here deal with Windows Forms (desktop) applications, with a strong emphasis on using the various controls available for building them.

Chapter 5, Strings

This chapter includes dozens of recipes that focus on strings and string manipulation. A few recipes focus on regular expression processing.

Chapter 6, Numbers and Math

The .NET Framework, and by extension Visual Basic, includes several data types that each manipulate different sizes and ranges of numbers. This chapter's recipes show you ways to interact with those data types and values. For those who miss their high school math classes, there are several recipes that deal with more advanced math topics, including geometry and linear algebra.

Chapter 7, Dates and Times

The recipes in this chapter demonstrate how to use and manipulate date and time values and the components from which they are built. Timing and time ranges are also covered.

Chapter 8, Arrays and Collections

Storing individual data values is fine, but sometimes you need to store a whole bunch of similar values as a group. This chapter shows you how to do just that by demonstrating various features of arrays and collections. Generics, new to Visual Basic 2005, play a prominent role in this chapter.

Chapter 9, Graphics

This chapter discusses the graphics features included with .NET, focusing on the many GDI+ graphic objects that let you draw complex shapes and text on almost any display surface.

Chapter 10, Multimedia

Moving pictures and sounds are the core of this chapter. The recipes found here will help you bring action to otherwise static forms and applications.

Chapter 11, Printing

Printing in .NET depends on GDI+ and its drawing engine. While some of the recipes in Chapter 9 will be useful for general printing, you'll find recipes dealing with other printing-specific topics, such as print preview support and the incorporation of user-specified page settings, in this chapter.

Chapter 12, Files and File Systems

This chapter focuses on the interaction between software and the data stored in disk files. Also covered are the different methods you can use to access and manage the file systems and directories where such files reside.

Chapter 13, Databases

Most Visual Basic applications communicate with one or more database systems. This chapter's recipes demonstrate different methods you can use in your applications to join databases and users through the medium of your custom software.

Chapter 14, Special Programming Techniques

If you didn't find it in another chapter, it's here. This somewhat large chapter covers topics that didn't fit neatly into other chapters. But in our opinion, it includes some of the most interesting and tasty recipes in the entire book.

Chapter 15, Exceptions

Error processing is the focus of this chapter. Its recipes deal specifically with exceptions and error management in your Visual Basic applications.

Chapter 16, Cryptography and Compression

Shhsome of the recipes in this chapter are secret. But it's all right for you to read them and use them to protect and ensure the integrity of the data managed by your application.

Chapter 17, Web Development

Most of the recipes in this book can be used in desktop or web-based applications, but there are a few special topics that are unique to ASP.NET applications. They appear in this 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

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

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:

Plain text

Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl).


Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, and directories.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, statements, keywords, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.

Constant width bold

Shows commands or other text that should be typed literally by the user; also used for emphasis within code.

Constant width italic

Shows text that should be replaced with user-supplied values.

This icon signifies a tip, suggestion, or general note.

This icon indicates a warning or caution.

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

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O'Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)

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:

Safari® Enabled

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


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.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: