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 2008 Recipes: A Problem-Solution Approach

Book Description

Sometimes you just need to solve a problem and get on with your work. To that end, Visual Basic 2008 Recipes is a compendium of practical solutions for busy VB .NET programmers. Create time for the more interesting aspects of your VB .NET project by solving common problems with the practical solutions and dozens of code examples in this book. Important .NET 3.5 technologies, such as Windows Presentation Framework (WPF) and Language Integrated Query (LINQ), are covered, and each chapter addresses a specific problem domain, including database access, multimedia, XML manipulation, networking, and security. The code is free to download, so you can use the recipes immediately.

  • Take advantage of quality, ready-made solutions to common Visual Basic programming problems

  • Avoid the tedium of having to reinvent the wheel-focus instead on the more interesting problems specific to your application

  • Learn from and be inspired by what the authors have done. Use their work as stepping stones to solve even more complex problems

Table of Contents

  1. Copyright
  2. Dedication
  3. About the Author
  4. About the Technical Reviewer
  5. Acknowledgments
  6. Introduction
  7. Application Development
    1. Create a Console Application from the Command Line
    2. Create a Windows-Based Application from the Command Line
    3. Create and Use a Code Module from the Command Line
    4. Create and Use a Code Library from the Command Line
    5. Embed a Resource File in an Assembly
    6. Build Projects from the Command Line Using MSBuild.exe
    7. Access Command-Line Arguments
    8. Include Code Selectively at Build Time
    9. Manipulate the Appearance of the Console
    10. Access a Program Element That Has the Same Name As a Keyword
    11. Create and Manage Strong-Named Key Pairs
    12. Give an Assembly a Strong Name
    13. Verify That a Strong-Named Assembly Has Not Been Modified
    14. Delay Sign an Assembly
    15. Sign an Assembly with an Authenticode Digital Signature
    16. Create and Trust a Test Software Publisher Certificate
    17. Manage the Global Assembly Cache
    18. Make Your Assembly More Difficult to Decompile
    19. Use Implicitly Typed Variables
    20. Use Object Initializers
    21. Use Anonymous Types
    22. Create and Use Extension Methods
    23. Create and Use Lambda Expressions
  8. Data Manipulation
    1. Manipulate the Contents of a String Efficiently
    2. Encode a String Using Alternate Character Encoding
    3. Convert Basic Value Types to Byte Arrays
    4. Base64 Encode Binary Data
    5. Validate Input Using Regular Expressions
    6. Use Compiled Regular Expressions
    7. Create Dates and Times from Strings
    8. Add, Subtract, and Compare Dates and Times
    9. Convert Dates and Times Across Time Zones
    10. Sort an Array or an ArrayList
    11. Copy a Collection to an Array
    12. Manipulate or Evaluate the Contents of an Array
    13. Use a Strongly Typed Collection
    14. Create a Generic Type
    15. Store a Serializable Object to a File
    16. Read User Input from the Console
  9. Application Domains, Reflection, and Metadata
    1. Load an Assembly into the Current Application Domain
    2. Create an Application Domain
    3. Execute an Assembly in a Different Application Domain
    4. Avoid Loading Unnecessary Assemblies into Application Domains
    5. Create a Type That Cannot Cross Application Domain Boundaries
    6. Create a Type That Can Be Passed Across Application Domain Boundaries
    7. Instantiate a Type in a Different Application Domain
    8. Pass Data Between Application Domains
    9. Unload Assemblies and Application Domains
    10. Retrieve Type Information
    11. Test an Object's Type
    12. Instantiate an Object Using Reflection
    13. Create a Custom Attribute
    14. Inspect the Attributes of a Program Element Using Reflection
  10. Threads, Processes, and Synchronization
    1. Execute a Method Using the Thread Pool
    2. Execute a Method Asynchronously
    3. Creating an Asynchronous Method to Update the User Interface
    4. Execute a Method Periodically
    5. Execute a Method at a Specific Time
    6. Execute a Method by Signaling a WaitHandle Object
    7. Execute a Method Using a New Thread
    8. Synchronize the Execution of Multiple Threads Using a Monitor
    9. Synchronize the Execution of Multiple Threads Using an Event
    10. Synchronize the Execution of Multiple Threads Using a Mutex
    11. Synchronize the Execution of Multiple Threads Using a Semaphore
    12. Synchronize Access to a Shared Data Value
    13. Know When a Thread Finishes
    14. Terminate the Execution of a Thread
    15. Create a Thread-Safe Collection Instance
    16. Start a New Process
    17. Terminate a Process
    18. Ensure That Only One Instance of an Application Can Execute Concurrently
  11. Files, Directories, and I/O
    1. Retrieve Information About a File, Directory, or Drive
    2. Set File and Directory Attributes
    3. Copy, Move, or Delete a File or a Directory
    4. Calculate the Size of a Directory
    5. Retrieve Version Information for a File
    6. Show a Just-in-Time Directory Tree in the TreeView Control
    7. Read and Write a Text File
    8. Read and Write a Binary File
    9. Parse a Delimited Text File
    10. Read a File Asynchronously
    11. Find Files That Match a Wildcard Expression
    12. Test Two Files for Equality
    13. Manipulate Strings Representing File Names
    14. Determine Whether a Path Is a Directory or a File
    15. Work with Relative Paths
    16. Create a Temporary File
    17. Get the Total Free Space on a Drive
    18. Show the Common File Dialog Boxes
    19. Use an Isolated Store
    20. Monitor the File System for Changes
    21. Access a COM Port
    22. Get a Random File Name
    23. Manipulate the Access Control Lists of a File or Directory
  12. Language Integrated Query (LINQ)
    1. Query a Generic Collection
    2. Query a Nongeneric Collection
    3. Control Query Results
    4. Sort Data Using LINQ
    5. Filter Data Using LINQ
    6. Perform General Aggregate Operations
    7. Perform Average and Sum Calculations
    8. Perform Count Operations
    9. Perform Min and Max Calculations
    10. Group Query Results
    11. Query Data from Multiple Collections
    12. Returning Specific Elements of a Collection
    13. Display Collection Data Using Paging
    14. Compare and Combine Collections
    15. Cast a Collection to a Specific Type
  13. LINQ to XML and XML Processing
    1. Create an XML Document
    2. Load an XML File into Memory
    3. Insert Elements into an XML Document
    4. Change the Value of an Element or Attribute
    5. Remove or Replace Elements or Attributes
    6. Query an XML Document Using LINQ
    7. Query for Elements in a Specific XML Namespace
    8. Query an XML Document Using XPath
    9. Join and Query Multiple XML Documents
    10. Convert an XML File to a Delimited File (and Vice Versa)
    11. Validate an XML Document Against a Schema
    12. Use XML Serialization with Custom Objects
    13. Create a Schema for a .NET Class
    14. Generate a Class from a Schema
    15. Perform an XSL Transform
  14. Database Access
    1. Connect to a Database
    2. Use Connection Pooling
    3. Create a Database Connection String Programmatically
    4. Store a Database Connection String Securely
    5. Execute a SQL Command or Stored Procedure
    6. Use Parameters in a SQL Command or Stored Procedure
    7. Process the Results of a SQL Query Using a Data Reader
    8. Obtain an XML Document from a SQL Server Query
    9. Perform Asynchronous Database Operations Against SQL Server
    10. Write Database-Independent Code
    11. Create a Database Object Model
    12. Generate Data Object Classes from the Command Line
    13. Discover All Instances of SQL Server on Your Network
  15. Windows Forms
    1. Add a Control Programmatically
    2. Link Data to a Control
    3. Process All the Controls on a Form
    4. Track the Visible Forms in an Application
    5. Find All MDI Child Forms
    6. Save Configuration Settings for a Form
    7. Force a List Box to Scroll to the Most Recently Added Item
    8. Restrict a Text Box to Accepting Only Specific Input
    9. Use an Autocomplete Combo Box
    10. Sort a List View by Any Column
    11. Lay Out Controls Automatically
    12. Make a Multilingual Form
    13. Create a Form That Cannot Be Moved
    14. Make a Borderless Form Movable
    15. Create an Animated System Tray Icon
    16. Validate an Input Control
    17. Use a Drag-and-Drop Operation
    18. Use Context-Sensitive Help
    19. Display a Web Page in a Windows-Based Application
    20. Create a Windows Presentation Foundation Application
    21. Run a Windows Vista Application with Elevated Rights
  16. Multimedia
    1. Find All Installed Fonts
    2. Perform Hit Testing with Shapes
    3. Create an Irregularly Shaped Control
    4. Create a Movable Sprite
    5. Create a Scrollable Image
    6. Perform a Screen Capture
    7. Use Double Buffering to Increase Redraw Speed
    8. Show a Thumbnail for an Image
    9. Play a Simple Beep or System Sound
    10. Play a WAV File
    11. Play a Sound File
    12. Show a Video with DirectShow
    13. Retrieve Information About Installed Printers
    14. Print a Simple Document
    15. Print a Multipage Document
    16. Print Wrapped Text
    17. Show a Dynamic Print Preview
    18. Manage Print Jobs
  17. Networking and Remoting
    1. Obtain Information About the Local Network Interface
    2. Detect Changes in Network Connectivity
    3. Download Data over HTTP or FTP
    4. Download a File and Process It Using a Stream
    5. Respond to HTTP Requests from Your Application
    6. Get an HTML Page from a Site That Requires Authentication
    7. Send E-mail Using SMTP
    8. Resolve a Host Name to an IP Address
    9. Ping an IP Address
    10. Communicate Using TCP
    11. Create a Multithreaded TCP Server That Supports Asynchronous Communications
    12. Communicate Using UDP
    13. Communicate Using Named Pipes
    14. Make an Object Remotable
    15. Register All the Remotable Classes in an Assembly
    16. Host a Remote Object in IIS
    17. Control the Lifetime of a Remote Object
    18. Control Versioning for Remote Objects
    19. Consume an RSS Feed
  18. Security and Cryptography
    1. Allow Partially Trusted Code to Use Your Strong-Named Assembly
    2. Disable Execution Permission Checks
    3. Ensure the Runtime Grants Specific Permissions to Your Assembly
    4. Limit the Permissions Granted to Your Assembly
    5. View the Permissions Required by an Assembly
    6. Determine at Runtime Whether Your Code Has a Specific Permission
    7. Restrict Who Can Extend Your Classes and Override Class Members
    8. Inspect an Assembly's Evidence
    9. Determine Whether the Current User Is a Member of a Specific Windows Group
    10. Restrict Which Users Can Execute Your Code
    11. Impersonate a Windows User
    12. Create a Cryptographically Random Number
    13. Calculate the Hash Code of a Password
    14. Calculate the Hash Code of a File
    15. Verify a Hash Code
    16. Ensure Data Integrity Using a Keyed Hash Code
    17. Work with Security-Sensitive Strings in Memory
    18. Encrypt and Decrypt Data Using the Data Protection API
  19. Code Interoperability
    1. Call a Function in an Unmanaged DLL
    2. Get the Handle for a Control, Window, or File
    3. Call an Unmanaged Function That Uses a Structure
    4. Call an Unmanaged Function That Uses a Callback
    5. Retrieve Unmanaged Error Information
    6. Use a COM Component in a .NET Client
    7. Release a COM Component Quickly
    8. Use Optional Parameters
    9. Use an ActiveX Control in a .NET Client
    10. Expose a .NET Component to COM
    11. Use a Windows Presentation Foundation Control from a Windows Form
  20. Commonly Used Interfaces and Patterns
    1. Implement a Serializable Type
    2. Implement a Cloneable Type
    3. Implement a Comparable Type
    4. Implement an Enumerable Type Using a Custom Iterator
    5. Implement a Disposable Class
    6. Implement a Type That Can Be Formatted
    7. Implement a Custom Exception Class
    8. Implement a Custom Event Argument
    9. Implement the Singleton Pattern
    10. Implement the Observer Pattern
  21. Windows Integration
    1. Access Runtime Environment Information
    2. Retrieve the Value of an Environment Variable
    3. Write an Event to the Windows Event Log
    4. Read and Write to the Windows Registry
    5. Search the Windows Registry
    6. Create a Windows Service
    7. Create a Windows Service Installer
    8. Create a Shortcut on the Desktop or Start Menu