Tải bản đầy đủ (.pdf) (38 trang)

Visual Basic 2005 Design and Development - Chapter 1 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (603.92 KB, 38 trang )

Expert One-on-One

Visual Basic
®
2005
Design and Development
01_053416 ffirs.qxd 1/2/07 6:25 PM Page i
01_053416 ffirs.qxd 1/2/07 6:25 PM Page ii
Expert One-on-One

Visual Basic
®
2005
Design and Development
Rod Stephens
01_053416 ffirs.qxd 1/2/07 6:25 PM Page iii
Expert One-on-One

Visual Basic
®
2005
Design and Development
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-05341-6


Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data:
Stephens, Rod, 1961-
Expert one-on-one Visual Basic 2005 design and development / Rod Stephens.
p. cm.
Includes index.
ISBN-13: 978-0-470-05341-6 (paper/website)
ISBN-10: 0-470-05341-0 (paper/website)
1. Microsoft Visual BASIC. 2. BASIC (Computer program language) I. Title.
QA76.73.B3S8335 2007
005.2'768 dc22
2006036646
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 permitted 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 appro-
priate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA01923, (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 />LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND
SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PAR-
TICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE
ADVICE AND STRATEGIES CONTAINED 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 PROFESSIONALASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PRO-
FESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAM-
AGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A
CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE
PUBLISHER ENDORSES THE INFORMATION 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 United
States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, Expert One-on-One, and related trade
dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other coun-
tries, 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.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in elec-
tronic books.
01_053416 ffirs.qxd 1/2/07 6:25 PM Page iv
Credits
Executive Editor
Robert Elliott
Development Editor
Kevin Shafer
Technical Editor
John Mueller
Production Editor
Eric Charbonneau
Copy Editor
Kim Cofer
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert

Graphics and Production Specialists
Carrie A. Foster
Denny Hager
Stephanie D. Jumper
Quality Control Technician
Laura Albert
Proofreading and Indexing
ConText
Techbooks
01_053416 ffirs.qxd 1/2/07 6:25 PM Page v
About the Author
Rod Stephens started out as a mathematician but, while studying at MIT, discovered the joys of pro-
gramming, and has been programming professionally ever since. During his career, he has worked on an
eclectic assortment of applications in such fields as telephone switching, billing, repair dispatching, tax
processing, wastewater treatment, and training for professional football players.
Stephens has written 15 books that have been translated into half a dozen different languages, and more
than 200 magazine articles covering Visual Basic, Visual Basic for Applications, Delphi, and Java. He
writes three weekly newsletters (
www.vb-helper.com/newsletter.html) that contain quick tips,
tricks, and examples for Visual Basic developers.
His popular VB Helper Web site (
www.vb-helper.com) receives several million hits per month, and con-
tains thousands of pages of tips, tricks, and example code for Visual Basic programmers, as well as
example code for this book.
Currently, Stephens is an author, consultant, and instructor at ITT Technical Institute.
Other Books by Rod Stephens
Visual Basic 2005 Programmer’s Reference ISBN: 0-7645-7198-2
Visual Basic .NET and XML ISBN: 0-471-12060-X
Visual Basic Graphics Programming, Second Edition ISBN: 0-471-35599-2
Bug Proofing Visual Basic Graphics ISBN: 0-471-32351-9

Custom Controls Library ISBN: 0-471-24267-5
Ready-to-Run Visual Basic Algorithms, Second Edition ISBN: 0-471-24268-3
Ready-to-Run Visual Basic Code Library ISBN: 0-471-33345-X
01_053416 ffirs.qxd 1/2/07 6:25 PM Page vii
Acknowledgments
Thanks to Bob Elliott, Kevin Shafer, and all of the others who make producing any book possible.
Thanks also to technical editor John Mueller for making sure I wasn’t putting my foot too deeply in my
mouth and for helping to add extra depth to the book. Visit
to learn
about John’s books and to sign up for his free newsletter .NET Tips, Trends & Technology eXTRA.
01_053416 ffirs.qxd 1/2/07 6:25 PM Page ix
Contents
Acknowledgments ix
Introduction xxi
Why Visual Basic? xxi
Who Should Read This Book xxii
How This Book Is Organized xxiii
How to Use This Book xxvi
What You Need to Use This Book xxvi
p2p.wrox.com xxvii
The Book’s Web Site xxviii
Part I: Design 1
Chapter 1: Language Selection 3
Language Addiction 3
Disadvantages 4
Interpreted, Not Compiled 4
Language Features 6
Multiple Inheritance 7
Platform Dependencies 8
Upgrading from Visual Basic 6 8

GUI Building 8
Verbosity 9
Power and Flexibility 9
Advantages 9
Self-Documenting 10
Prototyping and Simple Applications 10
IDE 11
Language Relatives 11
Garbage Collection 11
Large Talent Pool 12
Summary 12
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xi
xii
Contents
Chapter 2: Lifecycle Methodologies 15
Lifecycle Stages 15
Idea Formulation and Refinement 16
Team Building 17
Requirements Gathering 19
Feasibility Analysis 21
High-Level Design 23
Lower-Level Design 24
Implementation 25
Testing 26
Deployment 30
Support 31
Lifecycle Models 33
Throwaway 34
Waterfall 34
Prototyping 40

Mix and Match 46
Summary 47
Chapter 3: Agile Methodologies 49
Agile Programming 49
Crystal Clear 51
Extreme Programming (XP) 52
Agile Versus Traditional Lifecycles 60
Agile Strengths and Weaknesses 61
Design by Contract 62
Microsoft Tools for Agile Development 67
Summary 68
Chapter 4: Object-Oriented Design 69
The Bug Hunter Example 69
Building an Object Model 70
Picking Candidate Classes 70
Converting Candidates into Classes 71
Adding Internal Classes 74
Adding Database Classes 75
Studying Different Design Views 75
Improving the Classes 76
UML 79
Use Case Diagrams 80
Class Diagrams 83
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xii
xiii
Contents
Sequence Diagrams 85
Activity Diagrams 86
State Chart Diagrams 87
Component Diagrams 89

Deployment Diagrams 90
UML Summary 91
UML Tools 92
Summary 95
Chapter 5: User-Interface Design 97
UI Design Philosophy 98
Give the User Control 98
Focus on Tasks 98
Advise, Don’t Act 99
Help Users Do the Right Thing 100
Users 101
Understand the User’s Job 101
Respect the User 101
Understand the User’s Environment 102
Understand User Types 102
Forms 104
MDI versus SDI 104
Resizing Forms 105
Editors 107
Property Sheets 108
Design Principles 109
Prototype in Stages 110
Promote Form Flow 110
Group Related Items 113
Support Resizing 114
Support Different Font Sizes 115
Optimize Menus 119
Use Space Wisely 121
Design Good Headers 122
Preserve State 122

Undo and Redo 123
Accommodate Different Skills Levels 127
Provide Appropriate Feedback 129
Consistency 131
Summary 131
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xiii
xiv
Contents
Chapter 6: Data Storage Design 133
Relational Databases 133
Relational Database Products 139
Compiled-In Data 140
Resource Files 145
Satellite Assemblies 148
System Registry 149
INI Files 154
XML 157
Other Text File Formats 159
Object Databases 160
Summary 162
Chapter 7: Design Patterns 163
Creation Patterns 164
Clone 164
Factory 168
Relation Patterns 170
Adapter 171
Facade 171
Interface 172
Behavior Patterns 172
Abstract Base Class 173

Chain of Responsibility 176
Chain of Events 178
Command 181
Delegation 183
Model, View, Controller 185
Property Procedure 193
Snapshot 195
Strategy 199
For More Information 201
Summary 201
Part II: Meta-Development 203
Chapter 8: Snippets, Macros, and Add-ins 205
Snippets 205
Using Snippets 206
Making Snippets 208
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xiv
xv
Contents
Installing Snippets 211
Sharing Snippets 211
Macros 212
Recording Macros 213
Editing Macros 214
Modifying Macros 214
Using Sample Macros 218
Customizing Visual Studio 218
Sharing Macros 221
Add-ins 222
Making an Add-in 222
Adding Add-in Code 227

Making a Better Add-in 230
Learning About Other Objects 231
Summary 236
Chapter 9: Scripting 237
Scripting Safely 237
Executing SQL Statements 241
Running Queries 241
Generating Queries 244
Running Commands 247
Executing Visual Basic Code 251
Running Code 251
Exposing an Object Model 254
Simplifying Scripting 257
Evaluating Expressions 259
Summary 261
Chapter 10: Custom Controls and Components 263
Building Derived Controls 264
Setting Toolbox Bitmaps 270
Testing Controls 271
Building UserControls 272
Building Controls from Scratch 275
Building Components 282
Building Extender Providers 287
A Simple Example 289
A More Useful Example 291
Summary 294
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xv
xvi
Contents
Chapter 11: Property Support 297

Customizations Overview 298
Displaying and Editing LineWidth 304
LineWidthEditor 304
LineWidthListBox 306
Displaying and Editing LineColor 307
Displaying and Editing PolyPolyline 308
PolyPolylineConverter 309
PolyPolylineEditor 310
Displaying Smart Tags 313
Displaying Property Sheets 319
Displaying Object Properties 322
Summary 328
Chapter 12: Attributes and XML Comments 329
Attributes 330
Helping the Properties Window 332
Helping the Form Designer 339
Helping the Code Editor 340
Helping Serialization 344
XML Comments 347
Automatic Documentation 348
Design-Time Support 350
Recommended Tags 351
Recommendations 355
Summary 357
Part III: Development 359
Chapter 13: Documentation 361
Documentation Timing 361
Documentation Types 364
User Documentation 364
Developer Documentation 369

XML Documentation 373
Custom XML Tags 374
Using XML Documentation 375
Program XmlDocToText 376
Program XmlDocToHtml 381
Summary 388
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xvi
xvii
Contents
Chapter 14: Development Philosophy 391
Program for People 392
Keep the User in Charge 393
Make the Program Find Errors 394
Make it Work First 395
Look Before You Leap 397
Do a Good Job; Then Move On 398
Use Object-Oriented Principles 399
Take Advantage of Visual Studio 400
Beware of the Weakest Link 401
Save Everything 403
Avoid the Bleeding Edge 404
Use the Agile Smorgasbord 405
Summary 406
Chapter 15: Coding Standards 409
Names 410
Hungarian Notation 410
Component Names 411
Constant Names 413
Routine Variable Names 413
Other Names 414

Boolean Names 415
Abbreviations 415
Escaped Names 416
Class Names 416
Property Names 417
Method Names 417
Event Names 418
Declaring Variables 419
Overloading and Optional Parameters 422
ParamArrays 426
If Then and Select Case 427
Block Endings 429
Exit Points 430
Exit For, Exit Do, and Continue 431
End 432
Summary 432
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xvii
xviii
Contents
Chapter 16: Bug Proofing 435
Catching Bugs 435
Waiting for Bugs 436
Global Error Handling 438
Hunting for Bugs 441
Try Catch Blocks 445
Responding to Bugs 447
Summary 451
Chapter 17: Testing 453
Testing Philosophy 453
Test Early and Often 454

Test Everything 455
Save Tests Forever 455
Don’t Shoot the Messenger 456
Kinds of Tests 456
Sub-Unit Tests 456
Unit Tests 457
Integration Tests 458
System Tests 458
Regression Tests 459
Test Techniques 459
Exhaustive Testing 460
Black Box Testing 460
White Box Testing 461
Random Testing 461
Testing Mechanics 461
Testing Inside the Application 461
Testing Outside the Application 464
A Testing Example 465
Testing Tools 467
Visual Studio Testing Tools 470
GUI Testing 471
Summary 475
Chapter 18: Deployment 477
Deployment Models 477
User Types 478
Major and Minor Releases 479
Agile Releases 480
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xviii
xix
Contents

Deployment Methods 480
ClickOnce 480
Windows Installer 486
Other Solutions 490
Xcopy 491
Summary 492
Part IV: Specific Techniques 493
Chapter 19: Splash Screens 495
Determining Splash Screen Contents 496
Displaying Splash Screens 498
Displaying About Dialogs 500
Providing Feedback 500
Controlling Item Placement 501
Shaping Forms 502
Filling Text 504
Rotating Text 505
Displaying Hidden Screens 506
Summary 508
Chapter 20: Printing 511
Event-Driven Printing 512
Printing Forms 514
Wrapping Text 518
Flowing Text 522
Printing Procedurally 528
Summary 532
Chapter 21: Threading 535
Threading Pros and Cons 536
Advantages 536
Disadvantages 537
Using Background Workers 542

UI Threading 542
Providing Feedback 547
Providing Graphical Feedback 550
Dividing Tasks 551
Understanding SyncLock 554
Summary 557
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xix
xx
Contents
Chapter 22: Reflection 559
Exploring Assemblies 559
Exploring Enumerations 567
Loading Classes Dynamically 571
Invoking Methods Dynamically 575
Discovering Resources 577
Retrieving Known Resources 580
Summary 583
Chapter 23: Memory Management 585
Garbage Collection 585
Finalization 587
Disposing Resources 589
Disposing Custom Classes 590
Pre-allocating Objects 591
Weak References 595
Improving Garbage Collection 597
Summary 599
Index 601
02_053416 ftoc.qxd 1/2/07 6:26 PM Page xx
Introduction
Visual Basic 2005, together with the .NET Framework, provides a wonderfully powerful development

environment. With these tools, developers can build amazingly powerful applications relatively quickly
and easily.
With this power, however, comes great complexity. Many books are available that discuss the Visual
Basic language, and if you need to build a relatively simple application, those are generally sufficient.
No books, however, address the complex issues that surround the development of more complicated
Visual Basic applications. None discuss design, modeling, user-interface design, or testing for Visual
Basic applications.
These books also don’t cover some of the important advanced topics that don’t fit well into introductory
books. They generally don’t have the time to cover threading, reflection, advanced memory management,
and printing in the depth necessary to make them useful for building really advanced applications.
Expert One-on-One Visual Basic 2005 Design and Development fills the gaps that these books leave sur-
rounding Visual Basic development. It explains the design activities that are necessary before Visual
Basic development can begin. It covers processes that occur after development (such as deployment,
update management, and testing) that are essential to a successful Visual Basic project, but that are not
part of the language itself. Finally, it covers advanced topics that are useful in real-world applications,
but that are given little attention in other books.
Why Visual Basic?
A Visual Basic programmer’s joke asks, “What’s the difference between Visual Basic .NET and C#?
About three months!” The implication is that Visual Basic .NET syntax is easier to understand, and
building applications with it is faster. Similarly, C# programmers have their jokes about Visual Basic,
implying that C# is more powerful.
In fact, Visual Basic is not a whole lot easier to use than C#, and C# is not significantly more powerful.
The basic form of the two languages is very similar, aside from a few stylistic differences. A good C# pro-
grammer can crank out a small application as quickly as a Visual Basic developer can, and a good Visual
Basic programmer can churn out object hierarchies and graphics code as effectively as a C# developer.
What many developers forget, however, is that building a successful application goes far beyond spitting
out mountains of code. The code must work today, tomorrow, and for the entire life of the application.
Far more important than the programming language is the way developers use the language. I have seen
code written in C, FORTRAN, and even assembly language that was so well structured and documented
that practically anyone could modify the code with a minimal chance of adding obscure bugs. I’ve also

seen a 56,000-line Visual Basic project that was so poorly designed and documented that even develop-
ers who had worked with the project for years could spend a week or more making a simple change,
and have about a 50 percent chance of adding new bugs that would take another week or two to fix.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxi
xxii
Introduction
So, does that mean your choice of language is irrelevant? Almost, but not quite. First, the development
environment plays a very important role in modern application development. Visual Studio provides
unmatched tools for programming, debugging, and refactoring code. IntelliSense makes it easier to
remember what parameters a routine needs. Auto-completion makes it easy to use longer, more descrip-
tive variables. Design-time syntax-checking highlights suspicious code as you type it. XML comments
and attributes let you make classes, methods, and events even more self-documenting than ever before.
All of these tools make writing effective, reliable, maintainable code in the Visual Studio languages
much easier than in many other languages that have less support from their development environments.
Though Visual Studio provides all of these tools, it cannot force you to use them properly, and here
Visual Basic gives you a slight edge. Visual Basic has always been a very descriptive language. Its key-
words are reasonably self-documenting and provide extra context to help you figure out what’s going
on. A piece of C# or C++ code containing several nested loops inside a subroutine may end with a series
of many closing brackets. There’s no extra information to help you remember what the brackets are end-
ing. You can (and should) add comments to make this obvious, but the language doesn’t force you to, so
many programmers don’t bother.
In contrast, Visual Basic ends a block of code with appropriate
End Do, End If, End Sub, and other
statements that make it obvious what type of block is ending. Although you should still add comments
to indicate the purpose of the block of code that is ending, Visual Basic provides some basic annotation
by itself.
Because Visual Basic is more descriptive than the other Visual Studio languages, it encourages develop-
ers to adopt a more descriptive style, too. Every time you write a
For loop, you see keywords that help
confirm the purpose and extent of the loop. Visual Basic is transparent, so developers are more inclined

to make their own code transparent.
(The .NET Framework also sets a good example by providing long, descriptive variable, class, method,
and event names. Class names such as
SortedList and method names such as DrawRectangle are so
easy to understand that it’s easy to adopt similar habits of writing descriptive names.)
No programming language can force you to write good code. But Visual Studio provides powerful tools
that reduce barriers to writing good code. They make it so easy to construct self-descriptive code that
there’s no excuse to write dense, opaque code full of obscure, inconsistent abbreviations. By performing
its own chores in a self-descriptive way, Visual Basic gives you a little extra help in keeping your own
code lucid. Use C# if you like, but I prefer to grab every advantage available.
Who Should Read This Book
This book is intended for advanced Visual Basic programmers. It assumes that you already have experi-
ence with Visual Basic 2005 and are comfortable with the language. The book does not cover any language
fundamentals, and does not include any refresher material. If you feel that you need more experience with
Visual Basic, see my book Visual Basic 2005 Programmer’s Reference (Indianapolis: Wiley, 2005).
The topics I present in this book are as advanced as possible. In some cases, however, I have included
simpler code to give background for a more advanced topic. But the goal of every part of the book is to
explain advanced material that is either complex, hard to find in other places, or both.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxii
xxiii
Introduction
Note that not all advanced material is complicated or difficult to understand. For example, the following
statement summarizes one of the most important concepts in this entire book:
In fact, this statement is so important that if you learn nothing else from this book, you will have
received your money’s worth. However, once you truly understand this idea (which I’ll discuss further
later in the book, notably in Chapter 14, “Development Philosophy”), it’s completely obvious. Still, this
is an advanced concept. I have never seen it written down explicitly in another book, and many develop-
ers only figure it out after years of debugging other people’s code.
How This Book Is Organized
This book’s chapters are grouped into four parts.

Part I: Design
The chapters in this part of the book cover activities that occur before programming begins. They
describe development and design approaches, and some useful design techniques. Many programmers
skimp on these phases, and later pay a huge price when their initial assumptions prove inappropriate
for the application.
Chapter 1, “Language Selection,” describes some of the issues that you should consider when picking a
development language. The book assumes you will use Visual Basic, but this chapter describes some of
the language’s shortcomings. While you may stick with Visual Basic, it’s worth understanding some of
the language’s weak spots, and what you can do about them. If you know where trouble may arise, you
can more easily cope with it. In extreme cases, you may be able to move some of the more troublesome
modules into a library written in another language and call the library from your Visual Basic code.
Chapter 2, “Lifecycle Methodologies,” discusses different lifecycle methodologies such as the waterfall,
iterative prototyping, and staged delivery models. It explains why Visual Basic is a particularly good
tool for building applications iteratively. Most developers become accustomed to a particular methodol-
ogy and never change. Even if you know you will be using iterative prototyping to build your applica-
tion, it’s worth taking the time to understand the best features of alternative approaches so that you can
try to steal the best ideas from them all.
Chapter 3, “Agile Methodologies,” explains different development strategies, such as agile develop-
ment methods like extreme programming (XP), test-first development, and design by contract (DBC).
It includes references to information on Microsoft’s Web sites about using Visual Studio for agile
development.
Chapter 4, “Object-Oriented Design,” discusses issues in object-oriented design. It introduces the
Universal Modeling Language (UML) and gives some tips for designing an object-oriented system. It
explains how Visual Studio Team Suite and Visual Studio Team Architect support UML, and mentions a
few other UML tools that developers can use. It tells how to gain a complete understanding of an appli-
cation by viewing design from different perspectives, such as user interface, document, and data per-
spectives. It explains how to get the greatest benefit from object-oriented ideas, such as encapsulation,
polymorphism, loose coupling, and component-oriented design.
Programs are written for people, not computers.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxiii

xxiv
Introduction
Chapter 5, “User-Interface Design,” explains important user-interface concepts. It explains the rule of
7 +/– 2 and tells how to use containers such as tab strips and scrolling controls to display large amounts
of data. It discusses the merits and drawbacks of SDI and MDI applications, and examines menus, tool-
bars, and keyboard shortcuts. It also explains techniques for customizing the user interface for particular
users and locales.
Chapter 6, “Data Storage Design,” discusses different data storage and retrieval strategies. It explores
options such as relational databases, XML, flat text files, the System Registry, compiled in data, resource
files, and INI files. It explains how a program can serialize and deserialize its data. Note that this chapter
does not cover relational database design in depth. For example, it does not explain normalization and
indexing in great detail, instead referring to other books for more information.
Chapter 7, “Design Patterns,” provides an introduction to design patterns and anti-patterns in Visual
Basic. It explains what design patterns and anti-patterns are, describes some of the most useful, and
refers the reader to other books such as Visual Basic Design Patterns by Mark Grand and Brad Merrill
(Indianapolis: Wiley, 2005) for additional details.
Part II: Meta-Development
The chapters in this part of the book discuss writing tools that are used by application developers. These
include add-ins that give design-time support to developers, custom controls and components that can
be placed on forms, and techniques that let the user execute scripts at run-time. A good library of meta-
development tools makes later development easier and less error-prone.
Chapter 8, “Snippets, Macros, and Add-ins,” explains how to write add-ins for the Visual Basic 2005
development environment. It briefly describes code snippets and explains when it is appropriate to use
them rather than add-ins.
Chapter 9, “Scripting,” explains how a program can allow the end user to write and execute scripts. It
tells how to let the user execute SQL statements, VBA code, and VB .NET scripts. It explains how a pro-
gram can expose its object model so that the user can control it with VB .NET scripting. This chapter also
tells how a developer can use the same scripting techniques to give an application new functionality
after it has been compiled.
Chapter 10, “Custom Controls and Components,” explains how to build custom controls. It describes the

three main approaches for building custom controls:
UserControl, subclassing, and from scratch. It
also explains how to build a special particularly useful type of custom control called extender providers,
and includes examples of some extremely useful data validation extender providers.
Chapter 11, “Property Support,” explains how to build custom property editors for use by custom con-
trols. It also tells how to use type converters to allow editors such as the Properties window at design
time and the
PropertyGrid control at run-time to edit complex properties of an object.
Chapter 12, “Attributes and XML Comments,” explains useful attributes that you can use to “decorate”
code. These attributes make extra information available to other developers (or yourself) who use the
code later so that it is easier to use the code correctly.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxiv
xxv
Introduction
Part III: Development
The chapters in this part of the book explain topics relating to the main body of the application’s devel-
opment work. These chapters focus on the process of writing code, and some approaches that make
writing code more effective. They do not dwell on language features and syntactic issues.
Chapter 13, “Documentation,” explains how a more complex project should be documented. It describes
the special XML documentation comments supported by Visual Basic 2005, and provides advanced
examples that show how to use them to automatically generate documentation and help files.
Chapter 14, “Development Philosophy,” explains advanced coding concepts that make development and
maintenance of complex applications easier. It discusses ideas such as advanced design for testing,
defensive and offensive programming, and writing code for people, not computers. These ideas are par-
ticularly useful in large projects with possibly changing staff because they provide context for new
developers moving onto a project.
Chapter 15, “Coding Standards,” explains my particular set of coding standards that make reading code
easier. It explains the differences between my standards and those used by Microsoft and others. These
standards offer a starting point for developing the rules that you decide to adopt. No set of standards is
perfect, but using any standard is better than not following any standard.

Chapter 16, “Bug Proofing,” explains steps that developers can take to make bugs easier to find and
repair. These methods are essential in large, complex applications. This chapter explains shortcomings of
the
Try Catch Finally blocks and shows how to work around them. It explains how to use applica-
tion-level events to catch all of an application’s errors, and discusses advanced methods that allow the
program to record and recover from errors.
Chapter 17, “Testing,” explains advanced debugging and testing techniques. It explains black box, white
box, and unusual condition testing. It explains regression, routine, module, and integration testing, and
tells how to estimate the quality of a project. It describes the free NUnit testing tool and how it fits into a
test-driven approach. It also describes the unit-test support built into Visual Studio 2005 Team System.
Chapter 18, “Deployment,” explains the deployment strategies available to Visual Basic 2005 applica-
tions. It shows how to provide extensibility by loading new modules and scripts into previously com-
piled applications. It also explains how to use serialization to allow backward and forward
compatibility.
Part IV: Specific Techniques
The chapters in this section explain specific advanced techniques that are useful in many applications.
Whereas the earlier chapters deal with general design and development issues, these chapters explain
approaches to handling particular tasks that arise on many development projects.
Chapter 19, “Splash Screens,” explains how to quickly build impressive splash screens and About
dialogs. It covers such techniques as shaped forms, rotated text, and links to Web pages. While this is
probably the least advanced (but most fun) chapter in the book, you should give the topic its due. Splash
screens and About dialogs are some of the first ways in which users and clients interact with an applica-
tion, so they set the mood. A good splash screen shows your enthusiasm for the application, and tells
your user that you take care of the little details that make a program useful.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxv
xxvi
Introduction
Chapter 20, “Printing,” explains how to print in Visual Basic .NET. It shows how to use the standard
.NET printing techniques to wrap text across pages and flow text around objects. It also describes a more
natural procedural approach to printing that is more intuitive for most developers, and lets you display

items such as page-by-page counts that are difficult when using the standard .NET printing model.
Chapter 21, “Threading,” explains how to use the new threading tools provided by Visual Basic 2005.
With computer hardware speeds leveling off, multi-threading is one of the best ways to improve perfor-
mance of intensive applications, but it comes with its own set of risks and assumptions. This chapter
explains some of the issues involved in multi-threaded applications, such as access to the user interface
thread and concurrency issues.
Chapter 22, “Reflection,” explains one of the most powerful features in Visual Basic .NET: reflection. It
shows how a program can use reflection to learn about modules, objects, properties, and methods in its
own code, and in other assemblies. It also explains how a program can use reflection to load modules
that were written after the application was compiled.
Chapter 23, “Memory Management,” explains memory-management issues. It describes the garbage col-
lector, shows how to implement and invoke
Dispose methods, and how to use weak memory references
to improve memory management.
How to Use This Book
There are two obvious approaches to using this book. First, you can read it cover-to-cover. That presents
the book’s parts in their most natural order: design, meta-development, development, and specific
techniques.
Second, you can jump into the book to read about topics that currently interest you. If you want to build
some custom controls to perform data validation in text boxes, you can read Chapter 10, “Custom
Controls and Components.”
Many of the techniques described in this book are much more useful if you keep them in mind during
development. It is more difficult to go back and retrofit good programming practices onto code that you
have already written. For that reason, you should read relevant chapters before you need them. You may
want to at least skim the entire book so that you have a good idea of what topics are covered. That way,
when you prepare to start a new development phase, you will know if a chapter contains information
that you should look at more closely.
What You Need to Use This Book
To read this book and understand the examples, you will need no special equipment. You don’t really
even need Visual Basic, and many of these techniques apply to other languages as well.

To use Visual Basic .NET and to run the examples found on the book’s Web page, you need any com-
puter that can reasonably run Visual Basic .NET. That means a reasonably modern, fast computer with
a lot of memory. See the Visual Basic .NET documentation for Microsoft’s exact requirements and
recommendations.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxvi
xxvii
Introduction
To build Visual Basic .NET programs, you will also need a copy of Visual Basic .NET. The examples
shown here were written and tested in Visual Basic 2005 under Windows XP, but most will run with few
or no modification in earlier versions of Visual Basic .NET and older operating systems. Some of the pro-
grams may not run in the Vista operating system. Visit the book’s Web site at
www.vb-helper.com/
one_on_one.htm
for updated Vista-compatible examples.
Much of the Visual Basic 2005 release is compatible with Visual Basic .NET 2003 and earlier versions of
Visual Basic .NET, so you can make many of the examples work with earlier versions of Visual Basic
.NET. You will not be able to load the example programs downloaded from the book’s Web site, how-
ever. You will need to copy and paste the significant portions of the code into your version of Visual
Basic .NET, and you may need to make a few changes. If you get stuck, email me at
RodStephens
@vb-helper.com
and I’ll try to point you in the right direction.
Don’t bother trying to run the examples with a pre NET version of Visual Basic such as Visual Basic 6.
The changes between Visual Basic 6 and Visual Basic .NET are huge, and many Visual Basic .NET con-
cepts don’t translate well into Visual Basic 6. With some experience in C#, it would be much easier to
translate programs into that language.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based sys-
tem for you to post messages relating to Wrox books and related technologies and interact with other
readers and technology users. The forums offer a subscription feature to email you topics of interest of

your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts,
and your fellow readers are present on these forums.
At
, you will find a number of different forums that will help you not only as
you read this book, but also as you develop your own applications. To join the forums, just follow these
steps:
1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join, as well as any optional information you wish to pro-
vide, and click Submit.
4. You will receive an email with information describing how to verify your account and complete
the joining process.
You can read messages in the forums without joining P2P, but in order to post your own messages, you
must join.
Once you join, you can post new messages and respond to messages other users post. You can read mes-
sages at any time on the Web. If you would like to have new messages from a particular forum emailed
to you, click the “Subscribe to this Forum” icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to ques-
tions about how the forum software works, as well as many common questions specific to P2P and Wrox
books. To read the FAQs, click the FAQ link on any P2P page.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxvii
xxviii
Introduction
The Book’s Web Site
On the book’s Web site, www.vb-helper.com/one_on_one.htm, you can do the following:
❑ Download the examples in this book
❑ Download other Visual Basic programming examples
❑ View updates and corrections
❑ Connect to the Wrox discussion group for this book
❑ Read other readers’ comments and suggestions

This book was written using Visual Basic 2005 under Windows XP. The book’s Web page will include
any modifications that the examples need to run in the Vista operating system.
If you have corrections or comments of your own, please send them to me at
RodStephens@vb-helper
.com
. I will do my best to keep the Web site as up-to-date as possible.
Subscribe to one of my three newsletters at
www.vb-helper.com/newsletter.html to receive weekly
tips, tricks, and examples for Visual Basic programming. I also announce any updates to my books in the
newsletters, so if you sign up, you’ll learn when there are updates.
03_053416 flast.qxd 1/2/07 6:27 PM Page xxviii

×