O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Visual Basic 2005 Cookbook

Book Description

This book will help you solve more than 300 of the most common and not-so-common tasks that working Visual Basic 2005 programmers face every day. If you're a seasoned .NET developer, beginning Visual Basic programmer, or a developer seeking a simple and clear migration path from VB6 to Visual Basic 2005, the Visual Basic 2005 Cookbook delivers a practical collection of problem-solving recipes for a broad range of Visual Basic programming tasks.

The concise solutions and examples in the Visual Basic 2005 Cookbook range from simple tasks to the more complex, organized by the types of problems you need to solve. Nearly every recipe contains a complete, documented code sample showing you how to solve the specific problem, as well as a discussion of how the underlying technology works and that outlines alternatives, limitations, and other considerations. As with all O'Reilly Cookbooks, each recipe helps you quickly understand a problem, learn how to solve it, and anticipate potential tradeoffs or ramifications.

Useful features of the book include:

  • Over 300 recipes written in the familiar O'Reilly Problem-Solution-Discussion format
  • Hundreds of code snippets, examples, and complete solutions available for download
  • VB6 updates to alert VB6 programmers to code-breaking changes in Visual Basic 2005
  • Recipes that target Visual Basic 2005 features not included in previous releases
  • Code examples covering everyday data manipulation techniques and language fundamentals
  • Advanced projects focusing on multimedia and mathematical transformations using linear algebraic methods
  • Specialized topics covering files and file systems, printing, and databases

In addition, you'll find chapters on cryptography and compression, graphics, and special programming techniques. Whether you're a beginner or an expert, the Visual Basic 2005 Cookbook is sure to save you time, serving up the code you need, when you need it.

Table of Contents

  1. Dedication
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
    3. What You Need to Use This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Comments and Questions
    7. Safari® Enabled
    8. Acknowledgments
    9. From Tim Patrick
    10. From John Clark Craig
  4. 1. Visual Basic Programming
    1. Introduction
    2. 1.1. Creating a Windows Forms Application
    3. 1.2. Creating a Console Application
    4. 1.3. Creating an ASP.NET Web Forms Application
  5. 2. The Development Environment
    1. Introduction
    2. 2.1. Discovering and Using a Code Snippet
    3. 2.2. Creating a New Snippet
    4. 2.3. Sharing Snippets
    5. 2.4. Adding Snippet Files to Visual Studio
    6. 2.5. Getting an Application’s Version Number
    7. 2.6. Letting Visual Studio Automatically Update an Application’s Version Number
    8. 2.7. Setting the Startup Form for an Application
    9. 2.8. Setting the Startup to a Sub Main Procedure
    10. 2.9. Getting an Application’s Command Line
    11. 2.10. Testing an Application’s Command Line
    12. 2.11. Obfuscating an Application
    13. 2.12. Determining if an Application Is Running in the Visual Studio Environment
    14. 2.13. Accessing Environment Variables
    15. 2.14. Accessing the Registry
    16. 2.15. Getting System Information
    17. 2.16. Getting the User’s Name
  6. 3. Application Organization
    1. Introduction
    2. 3.1. Creating a Code Module
    3. 3.2. Creating a Class
    4. 3.3. Creating a Structure
    5. 3.4. Creating Other Item Types
    6. 3.5. Creating Object Instances
    7. 3.6. Initializing a Class Instance with Data
    8. 3.7. Releasing an Instance’s Resources
    9. 3.8. Using Namespaces
    10. 3.9. Splitting a Class Across Multiple Files
    11. 3.10. Creating a Form Based on Another Form
    12. 3.11. Passing and Returning Structures and Other Objects
    13. 3.12. Creating and Using an Enumeration
    14. 3.13. Converting Between Numeric and String Enumeration Values
    15. 3.14. Creating a Method That Accepts Different Sets of Arguments
    16. 3.15. Using Standard Operators for Nonstandard Purposes
    17. 3.16. Enforcing Strong Data Typing in an Otherwise Weakly Typed Collection
  7. 4. Forms, Controls, and Other Useful Objects
    1. Introduction
    2. 4.1. Creating and Adding Controls at Runtime
    3. 4.2. Iterating Through All Controls on a Form
    4. 4.3. Sharing Event-Handler Logic Among Many Controls
    5. 4.4. Working with Timers
    6. 4.5. Determining If a Control Can Take the Focus
    7. 4.6. Programmatically Clicking a Button
    8. 4.7. Drawing a Control
    9. 4.8. Making a Form the Top-Most Form
    10. 4.9. Indicating the Accept and Cancel Buttons on a Form
    11. 4.10. Remembering a Form’s Position Between Uses
    12. 4.11. Attaching a Control to the Edge of a Form
    13. 4.12. Moving or Resizing Controls as a Form Resizes
    14. 4.13. Limiting the Sizing of a Form
    15. 4.14. Centering a Form
    16. 4.15. Creating and Moving a Borderless Form
    17. 4.16. Creating a Fading Form
    18. 4.17. Creating a Nonrectangular Form
    19. 4.18. Changing Menus at Runtime
    20. 4.19. Creating Shortcut Menus
  8. 5. Strings
    1. Introduction
    2. 5.1. Using a StringBuilder
    3. 5.2. Creating a String of N Identical Characters
    4. 5.3. Creating a String by Repeating a String N Times
    5. 5.4. Obfuscating a String
    6. 5.5. Converting Binary Data to a Hexadecimal String
    7. 5.6. Extracting Substrings from Larger Strings
    8. 5.7. Converting a String’s Case
    9. 5.8. Comparing Strings with Case Sensitivity
    10. 5.9. Comparing Strings Without Case Sensitivity
    11. 5.10. Converting Strings to and from Character Arrays
    12. 5.11. Converting Strings to and from Byte Arrays
    13. 5.12. Tallying Characters
    14. 5.13. Counting Words
    15. 5.14. Removing Extra Whitespace
    16. 5.15. Using the Correct End-of-Line Characters
    17. 5.16. Replacing Substrings
    18. 5.17. Inserting a Character or String
    19. 5.18. Inserting a Line
    20. 5.19. Double-Spacing a String
    21. 5.20. Formatting Numbers into Strings
    22. 5.21. Trimming Sets of Characters from a String
    23. 5.22. Identifying and Validating Types of Data in a String
    24. 5.23. Converting Strings Between Encoding Systems
    25. 5.24. Determining a Character’s Type
    26. 5.25. Parsing Strings
    27. 5.26. Concatenating Strings
    28. 5.27. Speeding Up String Manipulation
    29. 5.28. Counting Occurrences of a Substring
    30. 5.29. Padding a String for Exact Length and Alignment
    31. 5.30. Converting Tabs to Spaces
    32. 5.31. Reversing a String
    33. 5.32. Shuffling a String
    34. 5.33. Using a Simple String Encryption
    35. 5.34. Converting a String to Morse Code
    36. 5.35. Adding Strings to an Application’s Resources
    37. 5.36. Converting Any Data to a String
    38. 5.37. Using Regular Expressions to Extract All Numbers
    39. 5.38. Getting a Count of Regular Expression Matches
    40. 5.39. Getting the Nth Regular Expression Match
    41. 5.40. Compiling Regular Expressions for Speed
    42. 5.41. Using Regular Expressions to Validate Data
    43. 5.42. Using Regular Expressions to Count Characters, Words, or Lines
    44. 5.43. Converting a String to and from Base64
    45. 5.44. Splitting a String
    46. 5.45. Creating a String of Space Characters
  9. 6. Numbers and Math
    1. Introduction
    2. 6.1. Using Compact Operator Notation
    3. 6.2. Choosing Integers of the Right Size and Type for the Job
    4. 6.3. Using Unsigned Integers
    5. 6.4. Swapping Two Integers Without Using a Third
    6. 6.5. Using Single- and Double-Precision Variables
    7. 6.6. Using Decimal Variables for Maximum Precision
    8. 6.7. Converting Between Number Types
    9. 6.8. Rounding Numbers Accurately
    10. 6.9. Declaring Loop Counters Within Loops
    11. 6.10. Converting Between Radians and Degrees
    12. 6.11. Limiting Angles to a Range
    13. 6.12. Creating Double-Precision Point Variables
    14. 6.13. Converting Between Rectangular and Polar Coordinates
    15. 6.14. Creating Three-Dimensional Variables
    16. 6.15. Converting Between Rectangular, Spherical, and Cylindrical Coordinates
    17. 6.16. Working with Complex Numbers
    18. 6.17. Solving Right Triangles
    19. 6.18. Solving Any Triangle
    20. 6.19. Determining if a String Contains a Valid Number
    21. 6.20. Converting Numbers to Integers
    22. 6.21. Calculating π to Thousands of Digits
    23. 6.22. Getting a Number’s Prime Factors
    24. 6.23. Using Recursion to Calculate Factorials
    25. 6.24. Manipulating Bits with Bitwise Operators
    26. 6.25. Storing and Retrieving Bits in a BitArray
    27. 6.26. Enhancing the Random Number Generator
    28. 6.27. Generating Random Integers in a Range
    29. 6.28. Generating Random Real Numbers in a Range
    30. 6.29. Generating Normal-Distribution Random Numbers
    31. 6.30. Generating Exponential-Distribution Random Numbers
    32. 6.31. Creating a Matrix
    33. 6.32. Inverting a Matrix
    34. 6.33. Calculating the Determinant of a Matrix
    35. 6.34. Solving Simultaneous Equations
    36. 6.35. Listing of the MatrixHelper Class
  10. 7. Dates and Times
    1. Introduction
    2. 7.1. Getting the System Date and Time
    3. 7.2. Accessing the System’s Time Zone
    4. 7.3. Using System Ticks
    5. 7.4. Timing Application Activities
    6. 7.5. Calculating Elapsed Time Using Ticks
    7. 7.6. Calculating Elapsed Time with the Stopwatch
    8. 7.7. Extracting Year, Month, and Day Numbers from a Date Value
    9. 7.8. Extracting Hour, Minute, and Second Numbers from a Date Value
    10. 7.9. Creating a Date or Time Value from Its Parts
    11. 7.10. Formatting Dates and Times
    12. 7.11. Parsing and Validating Dates and Times
    13. 7.12. Adding to Dates and Times
    14. 7.13. Subtracting from Dates and Times
    15. 7.14. Determining the Number of Days Between Two Dates
    16. 7.15. Determining the Day of the Week for a Date
    17. 7.16. Determining the Day of the Year for a Date
    18. 7.17. Determining the Number of Days in a Month
    19. 7.18. Using Controls to Enter or Select a Date
    20. 7.19. Calculating the Phase of the Moon
    21. 7.20. Creating a Calendar
    22. 7.21. Checking for Leap Years
    23. 7.22. Dates and Times in ISO 8601 Formats
  11. 8. Arrays and Collections
    1. Introduction
    2. 8.1. Filling an Array While Declaring It
    3. 8.2. Sorting Array Elements
    4. 8.3. Reversing an Array
    5. 8.4. Inserting into an Array
    6. 8.5. Shuffling an Array
    7. 8.6. Swapping Two Array Values
    8. 8.7. Resizing Arrays Without Losing Existing Values
    9. 8.8. Quickly Copying Part of an Array into Another
    10. 8.9. Writing a Comma-Separated-Values File from a String Array
    11. 8.10. Reading a Comma-Separated-Values File into a String Array
    12. 8.11. Using a Multivalue Array Instead of a Two-Dimensional Array
    13. 8.12. Converting Between Delimited Strings and Arrays
    14. 8.13. Formatting an Array as a Single String
    15. 8.14. Iterating Through Array Elements
    16. 8.15. Passing Arrays to Methods
    17. 8.16. Returning Arrays from Functions
    18. 8.17. Creating a Collection
    19. 8.18. Inserting an Item into a Collection
    20. 8.19. Deleting a Collection Item
    21. 8.20. Iterating Through a Collection
  12. 9. Graphics
    1. Introduction
    2. 9.1. Creating Graphics Objects
    3. 9.2. Drawing on Controls for Special Effects
    4. 9.3. Letting the User Select a Color
    5. 9.4. Working with Coordinate Systems (Pixels, Inches, Centimeters)
    6. 9.5. Creating a Bitmap
    7. 9.6. Setting a Background Color
    8. 9.7. Drawing Lines, Ellipses, and Rectangles
    9. 9.8. Drawing Lines One Pixel Wide Regardless of Scaling
    10. 9.9. Forcing a Form or Control to Redraw
    11. 9.10. Using Transparency
    12. 9.11. Scaling with Transforms
    13. 9.12. Using an Outline Path
    14. 9.13. Using Gradients for Smooth Color Changes
    15. 9.14. Drawing Bezier Splines
    16. 9.15. Drawing Cardinal Splines
    17. 9.16. Limiting Display Updates to Specific Regions
    18. 9.17. Drawing Text
    19. 9.18. Rotating Text to Any Angle
    20. 9.19. Mirroring Text on the Canvas
    21. 9.20. Getting the Height and Width of a Graphic String
    22. 9.21. Drawing Text with Outlines and Drop Shadows
    23. 9.22. Calculating a Nice Axis
    24. 9.23. Drawing a Simple Chart
    25. 9.24. Creating Odd-Shaped Forms and Controls
    26. 9.25. Using the RGB, HSB (HSV), and HSL Color Schemes
    27. 9.26. Creating a Rubber-Band Rectangular Selection
    28. 9.27. Animating with Transparency
    29. 9.28. Substitutions for Obsolete Visual Basic 6.0 Features
  13. 10. Multimedia
    1. Introduction
    2. 10.1. Playing an Audio File
    3. 10.2. Displaying Image Files
    4. 10.3. Playing a Video File
    5. 10.4. Making Your Computer Beep
    6. 10.5. Creating an Animation Using Multiple Images
    7. 10.6. Creating an Animation by Generating Multiple Bitmaps
    8. 10.7. Creating an Animation by Drawing at Runtime
    9. 10.8. Creating Animated Sprites
    10. 10.9. Resizing and Compressing JPEG Files
    11. 10.10. Getting JPEG Extended Information
    12. 10.11. Creating Thumbnails
    13. 10.12. Displaying Images While Controlling Stretching and Sizing
    14. 10.13. Scrolling Images
    15. 10.14. Merging Two or More Images
    16. 10.15. Using Resource Images
    17. 10.16. Capturing an Image of the Screen
    18. 10.17. Getting Display Dimensions
    19. 10.18. Speeding Up Image Processing
    20. 10.19. Converting an Image to Grayscale
    21. 10.20. Performing Edge Detection on an Image
    22. 10.21. Full Listing of the LockImage Class
  14. 11. Printing
    1. Introduction
    2. 11.1. Enumerating Printers
    3. 11.2. Sending “Raw” Data to a Printer
    4. 11.3. Get Details About the Default Printer
    5. 11.4. Creating a Print Preview
    6. 11.5. Prompting for Printed Page Settings
    7. 11.6. Drawing Text and Graphics to a Printer
    8. 11.7. Determining the Print Destination
    9. 11.8. Creating Graph Paper
  15. 12. Files and File Systems
    1. Introduction
    2. 12.1. Enumerating Drives
    3. 12.2. Determining if a Directory Exists
    4. 12.3. Creating a New Directory
    5. 12.4. Copying Directories
    6. 12.5. Moving Directories
    7. 12.6. Renaming Directories
    8. 12.7. Parsing File and Directory Paths
    9. 12.8. Searching Iteratively Through Directories and Subdirectories
    10. 12.9. Finding Directories and Files Using Wildcards
    11. 12.10. Determining If a File Exists
    12. 12.11. Getting and Setting File Attributes
    13. 12.12. Accessing Special User and Windows Directories
    14. 12.13. Determining the Space on a Drive
    15. 12.14. Browsing for a Directory
    16. 12.15. Getting File Information
    17. 12.16. Using File-Access Methods
    18. 12.17. Reading and Writing Files as Strings
    19. 12.18. Reading and Writing Binary Files
    20. 12.19. Copying or Moving a File
    21. 12.20. Sending a File to the Recycle Bin
    22. 12.21. Creating a Temporary File
    23. 12.22. Calculating a Checksum for a File
    24. 12.23. Comparing Two Files for Equality
    25. 12.24. Locking a File During Access
    26. 12.25. Reading from a File at a Specific Position
    27. 12.26. Reading and Writing Objects in a File
    28. 12.27. Creating a Comma-Separated-Values File
  16. 13. Databases
    1. Introduction
    2. 13.1. Connecting to a Data Provider
    3. 13.2. Issuing SQL Commands
    4. 13.3. Retrieving Results from a Database Query
    5. 13.4. Using SQL Parameters
    6. 13.5. Using Stored Procedures
    7. 13.6. Using Transactions
    8. 13.7. Storing the Results of a Query in Memory
    9. 13.8. Creating In-Memory Data Tables Manually
    10. 13.9. Writing In-Memory Data Tables to an XML File
    11. 13.10. Reading an XML File into In-Memory Data Tables
  17. 14. Special Programming Techniques
    1. Introduction
    2. 14.1. Preventing Multiple Instances of a Running Application
    3. 14.2. Creating a Simple User Control
    4. 14.3. Describing User Control Properties
    5. 14.4. Starting Other Applications by EXE, Document, or URL
    6. 14.5. Waiting for Applications to Finish
    7. 14.6. List All Running Processes
    8. 14.7. Terminating a Running Process
    9. 14.8. Pausing Execution of a Program
    10. 14.9. Control Applications by Simulating Keystrokes
    11. 14.10. Watching for File and Directory Changes
    12. 14.11. Creating an Icon in the System Tray
    13. 14.12. Accessing the Clipboard
    14. 14.13. Adding Tooltips to Controls
    15. 14.14. Dragging and Dropping Files to a ListBox
    16. 14.15. Dragging and Dropping Between ListBox Controls
    17. 14.16. Disposing of Objects Appropriately
    18. 14.17. Fine-Tuning Garbage Collection
    19. 14.18. Moving the (Mouse) Cursor
    20. 14.19. Intercepting All Key Presses on a Form
    21. 14.20. Accessing the Registry
    22. 14.21. Running Procedures in Threads
    23. 14.22. Reading XML into a TreeView
    24. 14.23. Creating an XML Document
    25. 14.24. Validating an XML Document
    26. 14.25. Using Generic Collections
    27. 14.26. Creating a Screensaver
    28. 14.27. Localizing the Controls on a Form
    29. 14.28. Adding Pop-up Help to Controls
    30. 14.29. Maintaining User-Specific Settings Between Uses of an Application
    31. 14.30. Verifying a Credit Card Number
    32. 14.31. Capturing a Console Application’s Output
    33. 14.32. Reading an Assembly’s Details
    34. 14.33. Performing Serial I/O
    35. 14.34. Rebooting the System
  18. 15. Exceptions
    1. Introduction
    2. 15.1. Catching an Exception
    3. 15.2. Throwing an Exception
    4. 15.3. Catching Unhandled Exceptions
    5. 15.4. Displaying Exception Information
    6. 15.5. Creating New Exception Types
    7. 15.6. Ignoring Exceptions in a Block of Code
  19. 16. Cryptography and Compression
    1. Introduction
    2. 16.1. Generating a Hash
    3. 16.2. Encrypting and Decrypting a String
    4. 16.3. Encrypting and Decrypting a File
    5. 16.4. Prompting for a Username and Password
    6. 16.5. Handling Passwords Securely
    7. 16.6. Compressing and Decompressing a String
    8. 16.7. Compressing and Decompressing a File
    9. 16.8. Generating Cryptographically Secure Random Numbers
    10. 16.9. Complete Listing of the Crypto.vb Module
    11. 16.10. Complete Listing of the Compress.vb Module
  20. 17. Web Development
    1. Introduction
    2. 17.1. Displaying Web Pages on a Form
    3. 17.2. Accessing Content Within an HTML Document
    4. 17.3. Getting All Links from a Web Page
    5. 17.4. Get the Local Computer’s IP Address
    6. 17.5. Resolving a Host Name or IP Address for Another Computer
    7. 17.6. Pinging an IP Address
    8. 17.7. Using FTP to Download Files
    9. 17.8. Calling a Web Service
    10. 17.9. Sending Email Using SMTP
    11. 17.10. Getting POP3 Emails
    12. 17.11. Sending a Message to Another Computer
    13. 17.12. Adding Hyperlinks to a (Desktop) Form
  21. Index
  22. About the Authors
  23. Colophon
  24. Copyright