Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 1.431 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
1.431
Dung lượng
11,79 MB
Nội dung
Visual Basic 2005 Cookbook By John Clark Craig, Tim Patrick Publisher: O'Reilly Pub Date: September 2006 Print ISBN-10: 0-596-10177-5 Print ISBN-13: 978-0-59-610177-0 Pages: 740 Table of Contents | Index 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 Visual Basic 2005 Cookbook By John Clark Craig, Tim Patrick Publisher: O'Reilly Pub Date: September 2006 Print ISBN-10: 0-596-10177-5 Print ISBN-13: 978-0-59-610177-0 Pages: 740 Table of Contents | Index Copyright Dedication Preface Chapter 1 Visual Basic Programming Introduction Recipe 1.1 Creating a Windows Forms Application Recipe 1.2 Creating a Console Application Recipe 1.3 Creating an ASP.NET Web Forms Application Chapter 2 The Development Environment Introduction Recipe 2.1 Discovering and Using a Code Snippet Recipe 2.2 Creating a New Snippet Recipe 2.3 Sharing Snippets Recipe 2.4 Adding Snippet Files to Visual Studio Recipe 2.5 Getting an Application's Version Number Recipe 2.6 Letting Visual Studio Automatically Update an Application's Version Number Recipe 2.7 Setting the Startup Form for an Application Recipe 2.8 Setting the Startup to a Sub Main Procedure Recipe 2.9 Getting an Application's Command Line Recipe 2.10 Testing an Application's Command Line Recipe 2.11 Obfuscating an Application Recipe 2.12 Determining if an Application Is Running in the Visual Studio Environment Recipe 2.13 Accessing Environment Variables Recipe 2.14 Accessing the Registry Recipe 2.15 Getting System Information Recipe 2.16 Getting the User's Name Chapter 3 Application Organization Introduction Recipe 3.1 Creating a Code Module Recipe 3.2 Creating a Class Recipe 3.3 Creating a Structure Recipe 3.4 Creating Other Item Types Recipe 3.5 Creating Object Instances Recipe 3.6 Initializing a Class Instance with Data Recipe 3.7 Releasing an Instance's Resources Recipe 3.8 Using Namespaces Recipe 3.9 Splitting a Class Across Multiple Files Recipe 3.10 Creating a Form Based on Another Form Recipe 3.11 Passing and Returning Structures and Other Objects Recipe 3.12 Creating and Using an Enumeration Recipe 3.13 Converting Between Numeric and String Enumeration Values Recipe 3.14 Creating a Method That Accepts Different Sets of Arguments Recipe 3.15 Using Standard Operators for Nonstandard Purposes Recipe 3.16 Enforcing Strong Data Typing in an Otherwise Weakly Typed Collection Chapter 4 Forms, Controls, and Other Useful Objects Introduction Recipe 4.1 Creating and Adding Controls at Runtime Recipe 4.2 Iterating Through All Controls on a Form Recipe 4.3 Sharing Event-Handler Logic Among Many Controls Recipe 4.4 Working with Timers Recipe 4.5 Determining If a Control Can Take the Focus Recipe 4.6 Programmatically Clicking a Button Recipe 4.7 Drawing a Control Recipe 4.8 Making a Form the Top-Most Form Recipe 4.9 Indicating the Accept and Cancel Buttons on a Form Recipe 4.10 Remembering a Form's Position Between Uses Recipe 4.11 Attaching a Control to the Edge of a Form Recipe 4.12 Moving or Resizing Controls as a Form Resizes Recipe 4.13 Limiting the Sizing of a Form Recipe 4.14 Centering a Form Recipe 4.15 Creating and Moving a Borderless Form Recipe 4.16 Creating a Fading Form Recipe 4.17 Creating a Nonrectangular Form Recipe 4.18 Changing Menus at Runtime Recipe 4.19 Creating Shortcut Menus Chapter 5 Strings Introduction Recipe 5.1 Using a StringBuilder Recipe 5.2 Creating a String of N Identical Characters Recipe 5.3 Creating a String by Repeating a String N Times Recipe 5.4 Obfuscating a String Recipe 5.5 Converting Binary Data to a Hexadecimal String Recipe 5.6 Extracting Substrings from Larger Strings Recipe 5.7 Converting a String's Case Recipe 5.8 Comparing Strings with Case Sensitivity Recipe 5.9 Comparing Strings Without Case Sensitivity Recipe 5.10 Converting Strings to and from Character Arrays Recipe 5.11 Converting Strings to and from Byte Arrays Recipe 5.12 Tallying Characters Recipe 5.13 Counting Words Recipe 5.14 Removing Extra Whitespace Recipe 5.15 Using the Correct End-of-Line Characters Recipe 5.16 Replacing Substrings Recipe 5.17 Inserting a Character or String Recipe 5.18 Inserting a Line Recipe 5.19 Double-Spacing a String Recipe 5.20 Formatting Numbers into Strings Recipe 5.21 Trimming Sets of Characters from a String Recipe 5.22 Identifying and Validating Types of Data in a String Recipe 5.23 Converting Strings Between Encoding Systems Recipe 5.24 Determining a Character's Type Recipe 5.25 Parsing Strings Recipe 5.26 Concatenating Strings Recipe 5.27 Speeding Up String Manipulation Recipe 5.28 Counting Occurrences of a Substring Recipe 5.29 Padding a String for Exact Length and Alignment Recipe 5.30 Converting Tabs to Spaces Recipe 5.31 Reversing a String Recipe 5.32 Shuffling a String Recipe 5.33 Using a Simple String Encryption Recipe 5.34 Converting a String to Morse Code Recipe 5.35 Adding Strings to an Application's Resources Recipe 5.36 Converting Any Data to a String Recipe 5.37 Using Regular Expressions to Extract All Numbers Recipe 5.38 Getting a Count of Regular Expression Matches Recipe 5.39 Getting the Nth Regular Expression Match Recipe 5.40 Compiling Regular Expressions for Speed Recipe 5.41 Using Regular Expressions to Validate Data Recipe 5.42 Using Regular Expressions to Count Characters, Words, or Lines Recipe 5.43 Converting a String to and from Base64 Recipe 5.44 Splitting a String Recipe 5.45 Creating a String of Space Characters Chapter 6 Numbers and Math Introduction Recipe 6.1 Using Compact Operator Notation Recipe 6.2 Choosing Integers of the Right Size and Type for the Job Recipe 6.3 Using Unsigned Integers Recipe 6.4 Swapping Two Integers Without Using a Third Recipe 6.5 Using Single- and Double-Precision Variables Recipe 6.6 Using Decimal Variables for Maximum Precision Recipe 6.7 Converting Between Number Types Recipe 6.8 Rounding Numbers Accurately Recipe 6.9 Declaring Loop Counters Within Loops Recipe 6.10 Converting Between Radians and Degrees Recipe 6.11 Limiting Angles to a Range Recipe 6.12 Creating Double-Precision Point Variables Recipe 6.13 Converting Between Rectangular and Polar Coordinates Recipe 6.14 Creating Three-Dimensional Variables Recipe 6.15 Converting Between Rectangular, Spherical, and Cylindrical Coordinates Recipe 6.16 Working with Complex Numbers Recipe 6.17 Solving Right Triangles Recipe 6.18 Solving Any Triangle Recipe 6.19 Determining if a String Contains a Valid Number Recipe 6.20 Converting Numbers to Integers Recipe 6.21 Calculating π to Thousands of Digits Recipe 6.22 Getting a Number's Prime Factors Recipe 6.23 Using Recursion to Calculate Factorials Recipe 6.24 Manipulating Bits with Bitwise Operators Recipe 6.25 Storing and Retrieving Bits in a BitArray Recipe 6.26 Enhancing the Random Number Generator Recipe 6.27 Generating Random Integers in a Range Recipe 6.28 Generating Random Real Numbers in a Range Recipe 6.29 Generating Normal-Distribution Random Numbers Recipe 6.30 Generating Exponential-Distribution Random Numbers Recipe 6.31 Creating a Matrix Recipe 6.32 Inverting a Matrix Recipe 6.33 Calculating the Determinant of a Matrix Recipe 6.34 Solving Simultaneous Equations Recipe 6.35 Listing of the MatrixHelper Class Chapter 7 Dates and Times Introduction Recipe 7.1 Getting the System Date and Time Recipe 7.2 Accessing the System's Time Zone Recipe 7.3 Using System Ticks Recipe 7.4 Timing Application Activities Recipe 7.5 Calculating Elapsed Time Using Ticks Recipe 7.6 Calculating Elapsed Time with the Stopwatch Recipe 7.7 Extracting Year, Month, and Day Numbers from a Date Value Recipe 7.8 Extracting Hour, Minute, and Second Numbers from a Date Value Recipe 7.9 Creating a Date or Time Value from Its Parts Recipe 7.10 Formatting Dates and Times Recipe 7.11 Parsing and Validating Dates and Times Recipe 7.12 Adding to Dates and Times Recipe 7.13 Subtracting from Dates and Times Recipe 7.14 Determining the Number of Days Between Two Dates Recipe 7.15 Determining the Day of the Week for a Date Recipe 7.16 Determining the Day of the Year for a Date Recipe 7.17 Determining the Number of Days in a Month Recipe 7.18 Using Controls to Enter or Select a Date Recipe 7.19 Calculating the Phase of the Moon Recipe 7.20 Creating a Calendar Recipe 7.21 Checking for Leap Years Recipe 7.22 Dates and Times in ISO 8601 Formats Chapter 8 Arrays and Collections Introduction Recipe 8.1 Filling an Array While Declaring It Recipe 8.2 Sorting Array Elements Recipe 8.3 Reversing an Array Recipe 8.4 Inserting into an Array Recipe 8.5 Shuffling an Array Recipe 8.6 Swapping Two Array Values Recipe 8.7 Resizing Arrays Without Losing Existing Values Recipe 8.8 Quickly Copying Part of an Array into Another Recipe 8.9 Writing a Comma-Separated-Values File from a String Array Recipe 8.10 Reading a Comma-Separated-Values File into a String Array Recipe 8.11 Using a Multivalue Array Instead of a Two-Dimensional Array Recipe 8.12 Converting Between Delimited Strings and Arrays Recipe 8.13 Formatting an Array as a Single String Recipe 8.14 Iterating Through Array Elements Recipe 8.15 Passing Arrays to Methods Recipe 8.16 Returning Arrays from Functions Recipe 8.17 Creating a Collection Recipe 8.18 Inserting an Item into a Collection Recipe 8.19 Deleting a Collection Item Recipe 8.20 Iterating Through a Collection Chapter 9 Graphics Introduction Recipe 9.1 Creating Graphics Objects Recipe 9.2 Drawing on Controls for Special Effects Recipe 9.3 Letting the User Select a Color Recipe 9.4 Working with Coordinate Systems (Pixels, Inches, Centimeters) Recipe 9.5 Creating a Bitmap Recipe 9.6 Setting a Background Color Recipe 9.7 Drawing Lines, Ellipses, and Rectangles Recipe 9.8 Drawing Lines One Pixel Wide Regardless of Scaling Recipe 9.9 Forcing a Form or Control to Redraw Recipe 9.10 Using Transparency Recipe 9.11 Scaling with Transforms Recipe 9.12 Using an Outline Path Recipe 9.13 Using Gradients for Smooth Color Changes Recipe 9.14 Drawing Bezier Splines Recipe 9.15 Drawing Cardinal Splines Recipe 9.16 Limiting Display Updates to Specific Regions Recipe 9.17 Drawing Text Recipe 9.18 Rotating Text to Any Angle Recipe 9.19 Mirroring Text on the Canvas Recipe 9.20 Getting the Height and Width of a Graphic String Recipe 9.21 Drawing Text with Outlines and Drop Shadows Recipe 9.22 Calculating a Nice Axis Recipe 9.23 Drawing a Simple Chart Recipe 9.24 Creating Odd-Shaped Forms and Controls Recipe 9.25 Using the RGB, HSB (HSV), and HSL Color Schemes Recipe 9.26 Creating a Rubber-Band Rectangular Selection Recipe 9.27 Animating with Transparency Recipe 9.28 Substitutions for Obsolete Visual Basic 6.0 Features Chapter 10 Multimedia Introduction Recipe 10.1 Playing an Audio File Recipe 10.2 Displaying Image Files Recipe 10.3 Playing a Video File Recipe 10.4 Making Your Computer Beep Recipe 10.5 Creating an Animation Using Multiple Images Recipe 10.6 Creating an Animation by Generating Multiple Bitmaps Recipe 10.7 Creating an Animation by Drawing at Runtime Recipe 10.8 Creating Animated Sprites Recipe 10.9 Resizing and Compressing JPEG Files Recipe 10.10 Getting JPEG Extended Information Recipe 10.11 Creating Thumbnails Recipe 10.12 Displaying Images While Controlling Stretching and Sizing Recipe 10.13 Scrolling Images Recipe 10.14 Merging Two or More Images Recipe 10.15 Using Resource Images Recipe 10.16 Capturing an Image of the Screen Recipe 10.17 Getting Display Dimensions Recipe 10.18 Speeding Up Image Processing Recipe 10.19 Converting an Image to Grayscale Recipe 10.20 Performing Edge Detection on an Image Recipe 10.21 Full Listing of the LockImage Class Chapter 11 Printing Introduction Recipe 11.1 Enumerating Printers Recipe 11.2 Sending "Raw" Data to a Printer Recipe 11.3 Get Details About the Default Printer Recipe 11.4 Creating a Print Preview Recipe 11.5 Prompting for Printed Page Settings Recipe 11.6 Drawing Text and Graphics to a Printer Recipe 11.7 Determining the Print Destination Recipe 11.8 Creating Graph Paper Chapter 12 Files and File Systems Introduction Recipe 12.1 Enumerating Drives Recipe 12.2 Determining if a Directory Exists Recipe 12.3 Creating a New Directory Recipe 12.4 Copying Directories Recipe 12.5 Moving Directories Recipe 12.6 Renaming Directories Recipe 12.7 Parsing File and Directory Paths Recipe 12.8 Searching Iteratively Through Directories and Subdirectories Recipe 12.9 Finding Directories and Files Using Wildcards Recipe 12.10 Determining If a File Exists Recipe 12.11 Getting and Setting File Attributes Recipe 12.12 Accessing Special User and Windows Directories Recipe 12.13 Determining the Space on a Drive Recipe 12.14 Browsing for a Directory Recipe 12.15 Getting File Information Recipe 12.16 Using File-Access Methods Recipe 12.17 Reading and Writing Files as Strings Recipe 12.18 Reading and Writing Binary Files Recipe 12.19 Copying or Moving a File Recipe 12.20 Sending a File to the Recycle Bin Recipe 12.21 Creating a Temporary File Recipe 12.22 Calculating a Checksum for a File Recipe 12.23 Comparing Two Files for Equality Recipe 12.24 Locking a File During Access Recipe 12.25 Reading from a File at a Specific Position Recipe 12.26 Reading and Writing Objects in a File Recipe 12.27 Creating a Comma-Separated-Values File Chapter 13 Databases Introduction Recipe 13.1 Connecting to a Data Provider Recipe 13.2 Issuing SQL Commands Regex.Matches( ) method Region object registry 2nd 3rd 4th accessing password hashes Registry object RegistryKey class RegistryKey objects and members RegistryKey.SetValue( ) method regular expression object regular expressions compiling for speed counting characters counting matches of data validation using extracting numbers from strings matches matching the Nth regular expression Remove( ) method 2nd RemoveMemoryPressure( ) method Rename( ) function RenameDirectory( ) method Replace( ) method 2nd 3rd tabs Report template ReRegisterForFinalize( ) method Resize( ) method resource images resources file template resx file extension RGB color scheme 2nd 3rd 4th 5th right triangles 2nd 3rd 4th RijndaelManaged object Rnd( ) function Rollback( ) method RotateTransform( ) method 2nd Round( ) function rounding numbers rpt file extension rubber-band selection 2nd 3rd 4th 5th 6th RunWorkerAsync( ) method Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] SByte integer type ScaleTransform( ) method 2nd 3rd 4th 5th screen captures 2nd 3rd 4th 5th screen dimensions Screen object screen savers 2nd 3rd 4th 5th 6th command-line options searching iterating through directories and subdirectories wildcards in file and directory searches security passwords Seek( ) method Send( ) method SendKeys( ) method 2nd 3rd key sequences serial I/O serialization SerializationInfo object SetPixel( ) method settings file template SetValue( ) method Shared keyword Shell( ) function Short integer type ShowDialog( ) method shuffling algorithm for string shuffling Sieve of Eratosthenes signed variable types size 2nd simultaneous equations single precision variables SMTP snippet picker Solution Explorer Sort( ) method 2nd 3rd 4th 5th SoundPlayer class Space( ) function SpecialDirectories object SpecialFolder enumeration values and members spherical 2nd 3rd 4th Splash Screen forms Split( ) function 2nd 3rd 4th 5th sprites 2nd 3rd 4th 5th SQL commands SQL database template SQL parameters SQL Server SqlCommand class SqlTransaction object standard operators 2nd 3rd 4th Start( ) method startup code StartupNextInstance Stopwatch object 2nd stored procedures StrConv( ) function StrDup( ) function streams StreamWriter object String( ) function VB 6 version String.Split( ) method 2nd StringBuilder object 2nd 3rd buffer concatenation using methods string manipulation StringFormat object strings application resources arrays 2nd 3rd Base64 binary data byte arrays case character arrays characters characters or strings Chars( ) property comparison incorporating case sensitivity comparison without case sensitivity compression and decompression concatenating creating of N identical characters creating through N repetitions of a string data data type identification and validation double spacing encoding systems encrypting with a key encryption and decryption 2nd 3rd 4th files Morse code numbers obfuscating padding for length and alignment parsing reversing sets of characters shuffling space character strings of N quantity speeding up manipulation splitting substrings 2nd tabs valid numbers in whitespace words in strong data typing in weakly typed collections 2nd 3rd StrReverse( ) function structures 2nd 3rd passing and returning Substring( ) method Subtract( ) method 2nd SuppressFinalize( ) method surface SyncLock statements system date and time 2nd 3rd system information 2nd 3rd 4th system reboots 2nd 3rd system registry system ticks system time zone System Tray icons System.Collections.Generic namespace System.Data namespace System.Data.SqlClient class SqlConnectionStringBuilder System.Diagnostics class Debugger.IsAttached flag Process class System.Drawing class Color structure Printing.PrinterSettings.InstalledPrinters collection System.Environment.SpecialFolder enumeration values and members System.Exception class System.GC object System.GC.MaxGeneration property System.IO class Compression namespace GZipStream class DriveInfo object FileInfo object Attributes property FileStream object System.Net class Dns namespace 2nd Mail.SmtpClient class Sockets namespace System.Reflection namespace System.Security.Cryptography namespace System.Text class Encoding functions Encoding object RegularExpressions.Regex object System.Threading.Thread.Sleep( ) method System.Windows.Forms class Cursor object SystemInformation object System.Xml.XmlDocument object Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] TcpClient class temperature conversion application Console version 2nd 3rd 4th Windows Forms version 2nd 3rd 4th 5th 6th temporary files terminating running processes text 2nd 3rd 4th 5th 6th mirroring on the canvas outlines and drop shadows rotating text file template TextBox control TextChanged event threads 2nd 3rd three-dimensional variables Throw statement thumbnails Ticks property 2nd TimeChanged public event TimeSpan object 2nd TimeZone object Toolbox ToolTip control tooltips ToShortDateString andToShortTimeString properties ToString( ) method 2nd 3rd 4th 5th 6th 7th 8th displaying exception information using Triangle class ToUniversalTime( ) method ToUpper( ) and ToLower( ) methods TrackBar control transactional component template transactions TranslateTransform( ) method 2nd transparency animation TransparencyKey property TreeView control triangles Trim( ) Try…Catch…Finally block statement two-dimensional arrays versus multivalue arrays Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] UInt16 integer type UInteger integer type ULong integer type UnhandledException event handler Unicode encoding conversions unmanaged memory unsigned integers unsigned variable types size 2nd user controls 2nd 3rd 4th 5th Users field Using statement UTF7 Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] validation of numbers in strings value types values vbNewLine constant 2nd version numbers automatic updating video files Visual Basic 2005 compiler Visual Basic 6.0 obsolete graphics features 2nd 3rd string insertion Visual Studio 2005 Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] WaitForExit( ) method WaitForInputIdle( ) method WaitForPendingFinalizers( ) method Web custom control template web development 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th email FTP downloads host name or IP address resolution for remote computers hyperlinks IP addresses links local computer IP address web pages 2nd 3rd 4th 5th XML Web Services WebBrowser control whitespace wildcards Windows file system security Windows Forms basic forms controls drawing on controls 2nd 3rd 4th 5th localizing controls form properties host names or IP addresses of remote computers links project creation Properties Window Solution Explorer startup form tab order text 2nd Toolbox Windows Management Instrumentation (WMI) Windows service template winres.exe application winspool.drv library WMI (Windows Management Instrumentation) words WriteAllBytes( ) method WriteAllText( ) method 2nd WriteXML( ) method Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] XML 2nd 3rd 4th XML documents 2nd 3rd 4th 5th 6th validating XML file template XML Schema template XML Web Service XmlDocument.CreateElement( ) method XmlReaderSettings class Xor operators xsd file extension XSLT file template ... 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 Visual Basic 2005 Cookbook By John Clark Craig, Tim Patrick... The Cookbook series designations, Visual Basic 2005 Cookbook, the image of a bream, and related trade dress are trademarks of O'Reilly Media, Inc Microsoft, MSDN, the NET logo, Visual Basic, Visual C++,... 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