ebook img

Visual Basic 2005 express edition for dummies PDF

318 Pages·2006·3.169 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 Visual Basic 2005 express edition for dummies

01_597051 ffirs.qxd 10/20/05 1:17 PM Page iii Visual Basic® 2005 Express Edition FOR DUMmIES ‰ by Richard Mansfield 01_597051 ffirs.qxd 10/20/05 1:17 PM Page iv Visual Basic®2005 Express Edition For Dummies® Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit- ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. Trademarks:Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. Visual Basic is a registered trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON- TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2005927625 ISBN-13: 978-0-7645-9705-3 ISBN-10: 0-7645-9705-1 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/QZ/RR/QV/IN 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xi Contents at a Glance Introduction ................................................................1 Part I: The Basics of Visual Basic Express ....................11 Chapter 1: What It’s All About: Visual Basic Express Takes a Bow ...........................13 Chapter 2: Up and Running ............................................................................................23 Chapter 3: At Your Service: Loads of Built-In Helpers ................................................35 Chapter 4: Tackling Essential Tools ..............................................................................51 Part II: Programming the Practical Way ......................75 Chapter 5: Common Tasks .............................................................................................77 Chapter 6: It’s All about My ............................................................................................97 Chapter 7: Whose Type Are You: Managing Variable Types ....................................105 Chapter 8: Superstrings: Managing Arrays ................................................................129 Chapter 9: Pretty Printing ............................................................................................143 Chapter 10: Testing and Deployment ..........................................................................161 Part III: Dealing with Databases ...............................179 Chapter 11: The Basics of Databases ..........................................................................181 Chapter 12: Quick Database User-Interface Techniques ..........................................191 Chapter 13: Managing DataSets ...................................................................................209 Part IV: Programming for the Web ............................225 Chapter 14: Painless Internet Programming ..............................................................227 Chapter 15: Everything’s Eventual: Web Page Management ....................................245 Part V: The Part of Tens ...........................................263 Chapter 16: Ten Great Visual Basic Express Tips and Tricks ..................................265 Chapter 17: Ten Important VB Resources ..................................................................277 Appendix A: About the CDs .......................................283 Bonus Appendix B: A Dictionary of VB.NET ....On the Web Index.......................................................................287 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xiii Table of Contents Introduction ..................................................................1 Understanding Visual Basic ............................................................................2 Tapping into the Power ...................................................................................2 About This Book ...............................................................................................3 How to Use This Book ......................................................................................4 Foolish Assumptions ........................................................................................5 How This Book Is Organized ...........................................................................6 Part I: The Basics of Visual Basic Express ...........................................6 Part II: Programming the Practical Way ...............................................6 Part III: Dealing with Databases ............................................................6 Part IV: Programming for the Web ........................................................7 Part V: The Part of Tens .........................................................................7 Appendixes ..............................................................................................7 Conventions Used in This Book .....................................................................8 What You Need to Get Started ........................................................................8 Icons Used in This Book ..................................................................................9 Where to Go from Here ....................................................................................9 Part I: The Basics of Visual Basic Express .....................11 Chapter 1:What It’s All About: Visual Basic Express Takes a Bow . . .13 Something Strange Happened in Orlando ...................................................14 Visual Basic through the years ...........................................................14 Visual Basic today ................................................................................15 What Does Visual Basic Express Have to Offer? ........................................15 Defaults that serve a purpose .............................................................16 The Express initiatives .........................................................................16 Finding help ...........................................................................................18 Taking a First Look .........................................................................................19 Creating a user interface helps organize your project ....................20 Programming for the Web ....................................................................21 Chapter 2:Up and Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 Finding Resources from Microsoft ...............................................................23 Building a Basic VB Program ........................................................................24 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xiv xiv Visual Basic 2005 Express Edition For Dummies Expanding Your Program ...............................................................................28 Frightening Yourself with the “Starter” Kits ...............................................32 Checking Out Additional Resources ............................................................33 Chapter 3:At Your Service: Loads of Built-In Helpers . . . . . . . . . . . . .35 IntelliSense Is Available .................................................................................36 Reusing Code Snippets ..................................................................................38 Automatic math: Snippets in action ...................................................38 Improving the code in code snippets ................................................39 Customizing code snippets .................................................................40 Customizing the Way You Work ....................................................................41 Automatic windows ..............................................................................42 Controlling your keyboard ..................................................................42 Changing toolbars ................................................................................44 Your IDE, your way ...............................................................................44 Aligning and Sizing Controls .........................................................................45 Using Help .......................................................................................................46 Error or “Exception” Helpers ........................................................................48 Chapter 4:Tackling Essential Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Introducing the Toolbox and Its Controls ...................................................51 Adjusting a Control’s Properties ..................................................................53 Changing a property in the Properties window ................................54 Some important properties (and many that aren’t) ........................55 Enabling Users to Change Properties ..........................................................65 Working Around Application Settings ................................................66 A more complex but flexible Application Settings workaround .....67 Storing persistent data: Its various hideouts ....................................68 Changing a property with the Application Settings feature ............70 Understanding the Solution Explorer ..........................................................72 Adding other files .................................................................................72 Finding your solution ...........................................................................73 Part II: Programming the Practical Way .......................75 Chapter 5:Common Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Mastering Events ............................................................................................77 Using Subroutines ..........................................................................................78 Writing a simple sub .............................................................................79 Passing parameters ..............................................................................79 Using Functions ..............................................................................................80 Understanding Scope .....................................................................................81 When variables are local .....................................................................82 Public: The greatest scope of all .........................................................84 Scoping procedures ..............................................................................85 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xv xv Table of Contents Going Round and Round in Loops ...............................................................85 Using a For...Next loop .........................................................................85 Working with Do loops .........................................................................89 Exploring While...Wend: A simple loop ..............................................90 For...Each: Looping in object collections ...........................................91 Making Decisions via Branching ..................................................................91 Understanding If...Then .......................................................................92 Multiple choice: The Select Case command .....................................94 Chapter 6:It’s All about My . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Comparing My to Classic VB and .NET ........................................................97 Classic VB ..............................................................................................98 A .NET version ......................................................................................98 The new My version .............................................................................98 Getting Familiar with My ...............................................................................99 Browsing through My help ..................................................................99 The major My categories ...................................................................100 Using My While Programming ....................................................................101 Chapter 7:Whose Type Are You: Managing Variable Types . . . . . . .105 Two Main Kinds of Data ...............................................................................106 Strings are like words .........................................................................106 You can do math with numbers ........................................................108 Understanding Variables .............................................................................109 Assigning a value to a variable .........................................................109 Storing string or numeric variables .................................................110 Naming Variables ..........................................................................................111 Creating a Variable .......................................................................................111 Declaring variables explicitly ............................................................112 Declaring variables implicitly ...........................................................113 Manipulating Variables ................................................................................114 Some variable efficiencies .................................................................115 Saving time with += .............................................................................116 Understanding Data Types for Numeric Variables ...................................117 Converting Data Types ................................................................................118 Creating Expressions with Operators ........................................................120 Comparing values ...............................................................................121 Using arithmetic operators ...............................................................124 The logical operators .........................................................................125 Setting operator precedence .............................................................127 Chapter 8:Superstrings: Managing Arrays . . . . . . . . . . . . . . . . . . . . . .129 Working in a Zero-Based World ..................................................................130 Initializing Arrays .........................................................................................131 Creating Arrays of Objects ..........................................................................131 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xvi xvi Visual Basic 2005 Express Edition For Dummies Searching and Sorting Arrays .....................................................................133 Customizing the Sorting Rules ....................................................................135 Using Many Members ..................................................................................136 The ArrayList Powerhouse ..........................................................................137 Why use an ArrayList? .......................................................................137 Working with ranges ...........................................................................139 Data Binding ..................................................................................................139 Enumerators .................................................................................................140 Using Hashtables ..........................................................................................141 Chapter 9:Pretty Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 Quick and Easy Printing Using My .............................................................144 Printing Just the Way You Want with the Printer Objects .......................146 Parsing text for the printer ................................................................147 Using the PrintPage event .................................................................149 Determining printable page size .......................................................150 Looping through the text ...................................................................152 Triggering PrintPage with the Button control .................................153 Letting Users Set Print Options ..................................................................155 Using the PrintPreview Control ..................................................................155 Printing Graphics .........................................................................................156 Understanding With...End With ........................................................157 Fine-tuning your graphics print options ..........................................159 Chapter 10:Testing and Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Finding and Fixing Syntax Errors ................................................................162 Tracking Down Logic Errors .......................................................................164 The voyeur technique ........................................................................164 Using Debug.WriteLine .......................................................................167 The Immediate window responds ....................................................167 The watch technique .........................................................................167 Setting breakpoints ............................................................................168 Fixing Errors with the Minor Debugging Tools .........................................169 Step Over (Shift+F8) ...........................................................................169 Step Out (Ctrl+Shift+F8) .....................................................................170 Run to Cursor (right-click in Break mode) ......................................170 Set Next Statement (right-click in Break mode) .............................170 Show Next Statement (right-click in Break mode) .........................171 The Call Stack ......................................................................................171 Adding Structured Error Handling .............................................................171 How runtime errors occur .................................................................172 Understanding Try .............................................................................173 The official syntax for Try...Catch...Finally ......................................175 Understanding Finally ........................................................................176 Throwing exceptions ..........................................................................177 Tips for using Try...End Try ...............................................................178 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xvii xvii Table of Contents Part III: Dealing with Databases ................................179 Chapter 11:The Basics of Databases . . . . . . . . . . . . . . . . . . . . . . . . . .181 Processing Data ............................................................................................181 Understanding Tables, Columns, Rows, and All the Rest .......................182 Tables, fields, and rows .....................................................................183 Joining and querying tables ..............................................................185 Why use multiple tables? ...................................................................187 Tangled relationships: Using unique data to tie tables together ....188 Let the database do it for you: AutoNumber fields ........................188 Indexes — a Key to Success ........................................................................189 Imagine nonalphabetic yellow pages ...............................................189 Hey, let’s index every field! ................................................................190 Chapter 12:Quick Database User-Interface Techniques . . . . . . . . . .191 Organizing the Entry Fields .........................................................................191 Navigating through Fields with the Tab Key .............................................192 Binding to Data .............................................................................................193 Loading a sample database ...............................................................193 Connecting to a database ..................................................................194 Binding controls to a dataset ............................................................197 Using the DataGridView ...............................................................................199 Saving Data to a DataSet ..............................................................................203 Saving Data to a Database ...........................................................................204 Everything is stored together ...........................................................204 Saving edited records .........................................................................206 Chapter 13:Managing DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209 Delving into DataSets ...................................................................................209 Building a DataSet Programmatically ........................................................211 Importing namespaces .......................................................................211 Declaring the global variables ..........................................................213 Building a DataSet in code .................................................................214 Analyzing the code .............................................................................216 Playing around ....................................................................................219 Understanding Collections ..........................................................................219 Opening an Existing DataSet .......................................................................220 Adding and Removing Data .........................................................................221 Adding data to a DataSet ...................................................................222 Removing data from a DataSet ..........................................................223 Moving through the DataSet .......................................................................223 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xviii xviii Visual Basic 2005 Express Edition For Dummies Part IV: Programming for the Web ..............................225 Chapter 14:Painless Internet Programming . . . . . . . . . . . . . . . . . . . . .227 Creating the Simplest Web Program ..........................................................228 Setting up the program ......................................................................228 Testing your program .........................................................................230 Positioning objects with the Style Builder ......................................233 Coming to Grips with ASP.NET ...................................................................234 The purpose of ASP ............................................................................234 HTML’s limitations ..............................................................................234 Firewalls and other necessary evils .................................................235 Getting to Know WebControls ....................................................................235 Displaying images ...............................................................................236 Containing with the Panel container ................................................236 The Table control ...............................................................................236 The rich Calendar ...............................................................................237 The AdRotator ....................................................................................238 Using Style Objects with WebControls ............................................240 Attaching a Database to Your Web Page ...................................................241 Chapter 15:Everything’s Eventual: Web Page Management . . . . . . .245 Understanding Server-Side Controls in ASP.NET .....................................246 The problem of persistence ..............................................................247 Fleshing out your ASP.NET project ...................................................247 Viewing the code ................................................................................249 Adding Simple Validation ............................................................................250 Managing State with Server-Side Controls ................................................252 Identifying a user’s first visit .............................................................253 Preserving values within a single page ............................................255 Preserving values across pages ........................................................256 Storing data with the Session property ...........................................258 Exploring the Application object (an alternative to Session) .......260 Why Not Use Cookies? .................................................................................260 Part V: The Part of Tens .............................................263 Chapter 16:Ten Great Visual Basic Express Tips and Tricks . . . . . .265 Using the ConversionWizard to Master VB Express ...............................265 Moving from Classic VB or VB6 to VB Express ........................................267 Managing Directories ...................................................................................267 Talking to the Clipboard ..............................................................................268 Randomizing .................................................................................................269 Detecting Keystrokes ...................................................................................271 CStr versus .ToString ...................................................................................271 02_597051 ftoc.qxd 10/20/05 1:18 PM Page xix xix Table of Contents Simplifying Source Code Two Ways ...........................................................272 Combining the declaration and the assignment .............................272 Avoiding repetition .............................................................................272 Understanding How the Registry Works with VB Express ......................273 Reading from the Registry .................................................................274 Writing to the Registry .......................................................................275 Drawing Directly on a Control ....................................................................276 Chapter 17:Ten Important VB Resources . . . . . . . . . . . . . . . . . . . . . . .277 Reading the Latest Info ................................................................................277 Getting Answers to VB Express Questions ...............................................278 Keeping Visual Basic Healthy .....................................................................278 Visiting Other Web Sites of Interest ...........................................................278 Discovering Microsoft’s Plans for the Future of Database Technology ...........................................................................279 Importing Favorite Settings .........................................................................279 Using the Application Test Center .............................................................279 Creating Menus via the MenuStrip .............................................................280 Protecting Your Intellectual Property ........................................................281 Graphics Transformations: Kitten with a Whip ........................................282 Appendix A: About the CDs ........................................283 System Requirements ..................................................................................283 Using the CD with Microsoft Windows ......................................................283 What You’ll Find ............................................................................................284 If You’ve Got Problems (Of the CD Kind) ..................................................285 Appendix B: A Dictionary of VB.NET ................On the Web Index........................................................................287

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.