ebook img

A Visual Basic 6 Programmer’s Toolkit PDF

324 Pages·2000·11.289 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview A Visual Basic 6 Programmer’s Toolkit

A Visual Basic 6 Programmer's Toolkit Hank Marquis and Eric A. Smith APress Media, LLC A Visual Basic 6 Programmer's Toolkit Hank Marquis and Eric A. Smith Additional material to this book can be downloaded from http://extras.springer.com All rights reserved. No part of this work may be reproduced or transmitred in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without prior written permission of the copyright owner and the publisher. Library of Congress Control Number: 00-13357 4 ISBN 978-3-540-78081-6 ISBN 978-1-4302-5125-5 (eBook) DOI 10.1007/978-1-4302-5125-5 10 9 8 7 6 54 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Copy Editor: TSI Graphics Production: TSI Graphics Indexer: Carla Johnson Cover Design: Derek Yee Design The information in this boak is distributed an an ·~ ls" basis, withaut warranty. Althaugh every precautian has been taken in the preparatian af this wark, neither the authar nar Apress shall have any liability ta any persan ar entity with respect ta any lass ar darnage caused ar alleged ta be caused directly ar indirectly by the infarmatian cantained in this wark. HANK MARQUIS This book is dedicated to my wife Nur and my children Adriana and Rolfe. ERICSMITH To my wife, ]odi Brief Contents Acknowledgments ................................ xiii About the Author ................................. xv Chapter 1: Arrays and Array Manipulation ............ 1 Chapter 2: Strings and String Handling .............. 23 Chapter 3: Fun with Numbers, Numeric Processing and Logic .............................. 41 Chapter 4: Working with Dates and Times ............ 59 Chapter 5: Data Structures: Versatile Veh1des for Data Storage .......................... 69 Chapter 6: Windows Files, Directories and Drives ...... 89 Chapter 7: Persistence: Remernhering What You Can't Afford to Forget ........................ 115 Chapter 8: Localization: The Art of Talking Like a Native .................................... 127 Chapter 9: Working with Networks ................ 141 Chapter 10: Reliable and Flexible Data Validation ..... 161 Chapter 11: Working with the Operating System ...... 183 Chapter 12: Windows Help Systems-From the Inside .. 195 Chapter 13: Multiple Document Interface Forms ...... 201 Chapter 14: Database Programming ................ 229 Chapter 15: Active Data Objects ................... 249 Chapter 16: Objects, Objects and More Objects ....... 269 Chapter 17: WebCiasses: A New Way to Program for the Web ............................ 289 Index .......................................... 313 V Detailed Contents Acknowledgments xiii 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 About the Author xv 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1: Arrays and Array Manipulation 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (REATING AND USING ARRAYS IN VISUAL BASIC o o o o o 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Sorting Arrays 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 WORKING WITH SORTED ARRAYS o o o o o o 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Finding an Element with a Specific Value in a Sorted Array 1 0 0 0 0 0 0 Inserting an Element into a Sorted Array 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Deleting an Element from a Sorted Array 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 WORKING WITH DATA IN AN ARRAY 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Finding Maximum and Minimum Values in an Array Range 14 0 0 0 0 Array Arithmetic 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 More on Array Arithmetic 1 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Copying a Range from One Array to Another 1 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Persisting Array Data 1 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PERFORMANCE HINTS o 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2: Strings and String Handling 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 WHAT ISA STRING? o o o o 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Examining Strings 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ParsingStrings 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Changing Strings 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3: Fun with Numbers, Numeric Processing, and Logic 41 0 0 BINARY ANALYSIS OF NUMBERS o o o o o o o o o o o o o o o o 41 0 0 0 0 0 0 0 0 0 0 0 0 Negative Numbers 42 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Binary Digits in Visual Basic Numbers 43 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 HEXADECIMAL HOCUS POCUS o o o o o o o 45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 MORE ON BIT FLAGS o o o o 4 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Bytes from Words 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Words from Longs 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 BINARY-(ODED DECIMAL o o o o o o o 51 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Next Steps for Warking with BCD Strings 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 METRICS 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 SPELLING ÜUT NUMBERSo 53 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 vii 4: Working with Datesand Times ................... 59 DATE ARITHMETIC .................................... 60 Leap Years ..................................... 60 Calculating Days . . . . . . . . . . . . . . . . . . . . . . ........... 61 FORMATTING DATES ................................... 62 JULIAN DATES ....................................... 64 Next Steps ..................................... 66 WORKING WITH TIME .................................. 66 SUMMARY ......................................... 68 5: Oata Structures: Versatile Vehicles for Oata Storage .................................... 69 DATA STRUCTURES IN VISUAL BASIC ......................... 69 Arrays ........................................ 69 User-Defined TYPes ............................... 69 Enumerated TYPes . . . . . . . . . . . . . . . . . . . . . . . ......... 71 Classes ....................................... 72 Key-Item Pairs .................................. 76 HASHING ......................................... 80 Creating the Data Structures Needed for Hashing ........... 80 Hashing the Data . . . . . . . . . . . . .................... 82 Searching the Hash Table . . . . . . ..................... 84 Testing the Hashing Solution ......................... 86 6: Windows Files, Directories, and Drives ............ 89 FILES, DIRECTORIES, AND DRIVES ........................... 89 WINDOWS APPLICATION PROGRAMMING INTERFACE ............... 90 FILES ......................................... 91 File Existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1 Finding a File ................................... 92 Getting a Unique Temporary Filename .................. 93 Changing a File's Date and Time Stamp .................. 95 Retrieving a Short Filename ......................... 99 Recycling Instead of Deleting ......................... 99 WINDOWS DIREGORIES ................................ 101 Windows System Directory . . . . . . . . . . . . . . . . . . . . . . . . . 1 02 Windows Directory .............................. 1 02 Temporary File Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 03 WINDOWS DISK DRIVES ................................ 10 4 Determining the Number of Drives . . . . . . . . . . . . . . . . . . . . 1 04 Detecting Drive TYPe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 06 DriveSpace ................................... 107 viii Drives Larger Than 2 GB . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 0 Drive Serial Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Reading and Writing the Drive Name . . . . . . . . . . . . . . . . . . 113 7: Persistence: Remembering What You Can't Afford to Forget ................................ 115 PERSISTENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 INI FILES ........................................ 116 Reading a String Value from an INI File . . . . . . . . . . . . . . . . . 116 Reading a Numeric Value from an INI File . . . . . . . . . . . . . . . 118 Writing INI File Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 9 Deleting INI Entries .............................. 120 THE WINDOWS REGISTRY ............................... 120 Reading from and Writing to the Registry . . . . . . . . . . . . . . . 12 1 8: Localization: The Art of Talking Like a Native ..... 127 LOCALIZATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 7 ACTIVEX (OMPONENTS THAT ADAPT ........................ 127 From Visual Basic to Self-Localizing ActiveX Controls ....... 128 Creating Resource Files ........................... 130 Creating Self-Localizing ActiveX DLLs and Documents ...... 131 Implementing Language DLLs . . . . . . . . . . . . . . . . . . . . . . . 133 Setting Default Fonts ............................. 136 Handling Other Languages ......................... 140 9: Working with Networks ........................ 141 ACCESSING A NETWORK ............................... 141 Determining Whether a Network Is Present . . . . . . . . . . . . . . 141 Reading the Username ............................ 142 Reading the Computer Name ........................ 143 Setting the Computer Name . . . . . . . . . . . . . . . . . . . . . . . . 143 Finding the Next Free Network Drive . . . . . . . . . . . . . . . . . . . 144 (ONNECTING TO A NETWORK RESOURCE ..................... 145 Disconnecting from a Network Resource ................ 146 Network Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 7 THE INTERNET EXPLORER API: ACCESS TO THE INTERNET WITHOUT A ßROWSER .......................... 149 Installing the Run-Time Files ........................ ISO Opening an Internet Channel . . . . . . . . . . . . . . . . . . . . . . . . 151 Connecting to the Internet Server . . . . . . . . . . . . . . . . . . . . . 152 Connecting to the Data Sink . . . . . . . . . . . . . . . . . . . . . . . . 152 Posting Form Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Reading the Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Closing All the Handles ............................ 156 Putting the Pieces Together: A Complete Data-Source Program . 156 ix 10: Reliable and Flexible Data Validation ........... 161 VALIDATION OF NUMERIC DATA ........................... 161 IsNumeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 1)'peName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Var1)'pe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 VALIDATION OF DATES ................................. 164 IsDate ....................................... 165 VALIDATION OF TEXT ENTRIES ............................ 166 Text Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 6 Text Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 7 VALIDATING ÜTHER TYPES OF DATA ......................... 167 PUTTING IT ALL TaGETHER .............................. 168 BuHding the Data-Entry Form ....................... 168 Setting Essential Properlies ......................... 169 Enforcing Character Validation . . . . . . . . . . . . . . . . . . . . . . 1 71 Reformatting Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 7 3 Credit Card Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 7 6 Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1 11: Working with the Operating System ............ 183 RETRIEVING SYSTEM INFORMATION ......................... 183 DETERMINING THE AMOUNT OF MEMORY .................... 183 DETERMINING HOW WINDOWS STARTED ..................... 185 GETTING AND SETTING ENVIRONMENT VARIABLES ............... 186 Getting an Environment Variable . . . . . . . . . . . . . . . . . . . . . 18 7 Setting an Environment Variable . . . . . . . . . . . . . . . . . . . . . 18 7 Deleting an Environment Variable .................... 188 SHUTTING DOWN AND RESTARTING WINDOWS ................. 188 Shutting Down Windows .......................... 189 Restarting Windows .............................. 189 RETRIEVING HARDWARE INFORMATION ...................... 189 Determining the CPU 1)'pe ......................... 189 Determining the Number of CPUs . . . . . . . . . . . . . . . . . . . . . 19 1 Determining CPU Performance Capabilities . . . . . . . . . . . . . . 1 91 RETRIEVING THE FLAVOR OF WINDOWS AND THE OPERATING SYSTEM VERSION ............................ 192 Identifying Windows 95/98 ......................... 193 Identifying Windows NT ........................... 193 Identifying the Operating System Version ............... 193 12: Windows Help Systems-From the Inside ......... 195 WINHELP ........................................ 195 DISPLAYING A HELP PAGE ............................... 196 (LOSING A HELP WINDOW .............................. 198 (ONTEXT-SENSITIVE HELP .............................. 198 X SHOWING A SPECIFIC HELP PAGE .......................... 199 DISPLAYING HELP IN APOP-UP WINDOW .................... 200 SEARCHING HELP .................................... 200 MAKING SURE YOUR HELP FILE IS THE ÜNE THATS VISIBLE .......... 202 DISPLAYING THE HELP WINDOW WHERE YOU WANT .............. 202 PUTTING IT ALL TOGETHER .............................. 204 13: Multiple Oocument Interface Forms ............. 207 MOl FORM ßASICS ................................... 207 SHARING SYSTEM RESGURCES ............................ 209 Sharing in the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . 2 12 MANAGING MENUS .................................. 215 Shared Menu Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 16 Semishared Menu Items ........................... 220 Custom Menu Items .............................. 224 14: Oatabase Programming ....................... 229 DATABASE FEATURES IN VISUAL BASIC ....................... 229 OLE DB Support ................................ 229 Active Data Objects (ADO) ......................... 230 The Data Environment ............................ 230 The ADO Data Control ............................ 230 Enhanced Data Binding ........................... 230 The Query and Database Designer .................... 230 The Data Report Designer .......................... 231 Data Sources .................................. 231 The Data View Window ........................... 231 The DataRepeater Control .......................... 231 Data Form Wizard Enhancements .................... 231 The Data Object Wizard ........................... 232 ADO-Aware Data Controls ......................... 232 USING THE DATA ENVIRONMENT DESIGNER ................... 232 Creating the Data Environment ...................... 232 Adding a Query ................................. 234 Binding a Data Grid to the Data Environment ............. 23 7 Binding a Record Viewer to the Data Environment ......... 238 Binding a Data Report to the Data Environment ........... 240 USING THE 0ATALIST AND DATA(OMBO (ONTROLS .............. 242 ßUILDING A DATABASE ANALYZER ......................... 244 15: Active Oata Objects .......................... 249 MICROSOFT'S UNIVERSAL DATA ACCESS STRATEGY ............... 249 More on OLE DB ................................ 249 ODBC ....................................... 250 Remote Data Service ............................. 250 xi

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.