Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Wrox's Visual Basic 2005 express edition starter kit
Nội dung xem thử
Mô tả chi tiết
Wrox’s Visual Basic®2005
Express Edition Starter Kit
Andrew Parsons
01_595733 ffirs.qxd 12/1/05 1:31 PM Page i
Wrox’s Visual Basic®2005 Express Edition Starter Kit
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-10: 0-7645-9573-3
ISBN-13: 978-0-7645-9573-8
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1MA/SR/RS/QV/IN
Library of Congress Cataloging-in-Publication Data:
Parsons, Andrew, 1970–
Wrox’s Visual Basic 2005 express edition starter kit / Andrew Parsons.
p. cm.
Includes index.
ISBN 0-7645-9573-3 (paper/cd-rom)
1. Microsoft Visual BASIC. 2. BASIC (Computer program language) I.
Title.
QA76.73B3P2542 2005
005.2_768—dc22
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 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.
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 PARTICULAR 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 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 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, 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.
"Microsoft" is a registered trademark of Microsoft Corporation in the United States and/or other countries and is used by
Wiley Publishing, Inc. under license from owner. Wrox’s Visual Basic® 2005 Express Edition Starter Kit is an independent publication not affiliated with Microsoft Corporation.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
01_595733 ffirs.qxd 12/7/05 2:24 PM Page ii
Credits
Acquisitions Editor
Katie Mohr
Senior Development Editor
Kevin Kent
Technical Editor
Todd Meister
Production Editor
William A. Barton
Copy Editor
Luann Rouff
Editorial Manager
Mary Beth Wakefield
Vice President & Executive Group Publisher
Richard Swadley
Vice President and Publisher
Joseph B. Wikert
Permissions Editor
Laura Moss
Media Development Specialist
Angela Denny
Kit Malone
Travis Silvers
Project Coordinator
Michael Kruzil
Graphics and Production Specialists
Carrie A. Foster
Lauren Goddard
Denny Hager
Joyce Haughey
Jennifer Heleine
Barbara Moore
Quality Control Technicians
Leeann Harney
Susan Moritz
Joe Niesen
Proofreading and Indexing
TECHBOOKS Production Services
01_595733 ffirs.qxd 12/1/05 1:31 PM Page iii
01_595733 ffirs.qxd 12/1/05 1:31 PM Page iv
About the Author
Andrew Parsons has been programming with the Basic language for more than 20 years and with Visual
Basic for the last eight years. He has experience with more than a dozen programming languages but
keeps coming back to the Basic language because of its easy-to-understand syntax and the powerful features found in the modern versions, and he believes that Visual Basic is the best language to program in
no matter what your level of experience.
Andrew has written several books and technical articles about Visual Basic for magazines in Australia
and New Zealand and is constantly improving his own skills in Visual Basic with ongoing programming
work with companies such as Quicken Software (associated with Intuit Inc.) and APS. When he’s not
writing code for other people, or books to help people learn how to program effectively, he serves as editor of MSDN Magazine, Australia and New Zealand, and still finds time to write add-ins for Microsoft
Office at GrayMatter Software (www.graymatter.com.au).
You can contact Andrew at [email protected].
01_595733 ffirs.qxd 12/1/05 1:31 PM Page v
01_595733 ffirs.qxd 12/1/05 1:31 PM Page vi
Contents
Acknowledgments xiii
Introduction xv
Part I: Getting Familiar 1
Chapter 1: Basic Installation 3
Where Did Basic Come From? 3
And Then Came Visual Basic 4
The Old and the New 5
Let’s Get Started 6
What It Looks Like 7
The Major Components 9
Your First Program 11
Try It Out: Creating Your First Program 11
That Was Too Easy 12
Try It Out: Your Very Own Web Browser 13
Summary 15
Exercises 15
Chapter 2: Why Do All That Work? 17
Object-Oriented Programming 101 17
Starting Out Right 19
Try It Out: Using Starter Kits 20
Try It Out: Modifying Starter Kit Projects 23
Wizards, Too 25
Try It Out: Using a Wizard 26
Everything Is Optional 28
Try It Out: Customizing the Options 30
It’s All There in the Documentation 30
Summary 31
Exercises 32
02_595733 ftoc.qxd 12/1/05 1:32 PM Page vii
viii
Contents
Chapter 3: Using Databases 33
SQL Server Express 33
Data to Database 34
Try It Out: Creating the Database 41
Connecting Database to a Project 45
Try It Out: Connecting a Database and Project 47
Alternatives to SQL Server Express 48
Summary 49
Exercise 49
Chapter 4: What the User Sees 51
User Interface Basics 51
User Interface Fundamentals 52
Adding and Customizing Controls 53
Try It Out: Adding a Control to a Form 54
The Controls 55
Basic Controls 55
Layout Controls 58
Menu and Status Controls 59
Dialog Controls 61
Graphic Controls 61
Other Controls 62
Anchoring and Docking 63
Anchoring 63
Docking 64
Building the User Interface for the Personal Organizer 64
Try It Out: Creating the Main User Interface 64
Summary 67
Exercises 67
Chapter 5: How Do You Make That Happen? 69
Writing Code 69
The Basics of Basic 70
Try It Out: Writing Code #1 74
Want Something More? 76
Try It Out: Adding Conditional Code 77
Try It Out: Writing Event Handlers 82
Objects: A Special Case 83
Applying the Knowledge 83
Try It Out: Connecting User Interface Elements 84
02_595733 ftoc.qxd 12/1/05 1:32 PM Page viii
ix
Contents
Summary 88
Exercises 89
Part II: Extending Yourself Is Good 91
Chapter 6: Take Control of Your Program 93
Adding Some Class to Your Program 93
Creating Custom Classes 94
Special Method Actions 101
Try It Out: Creating a Class 103
Control Freaks Are Cool 104
Design-time Properties 105
Try It Out: Modifying the Menu and Toolbar 108
Custom Controls — Empower Yourself 111
Try It Out: Adding Properties to Persons 112
Go That Extra Mile 115
Try It Out: Creating Dynamic Buttons 116
Summary 119
Exercises 119
Chapter 7: Who Do You Call? 121
Using the Database Connection 121
An Alternate Method 124
What about Existing Controls? 125
Try It Out: Adding a Database to Personal Organizer 126
Database Programming 127
Actions You Can Perform 128
Try It Out: Accessing the Database through Code 129
Summary 141
Exercise 141
Chapter 8: It’s My World — Isn’t It? 143
They’re My Classes 143
It’s All about the Computer 144
Try It Out: Using the Clipboard 145
Try It Out: Accessing System Information 147
Try It Out: Sending Keystrokes with SendKeys 149
Getting to the App 153
Try It Out: Using My Project and My.Application 154
02_595733 ftoc.qxd 12/1/05 1:32 PM Page ix
x
Contents
You Can Use It Again and Again . . . and Again 156
Try It Out: Using Code Snippets 156
Reusing Code Properly 158
Partial Classes 158
Generics 160
Try It Out: Adding the Login Form 162
Summary 167
Exercises 167
Chapter 9: Getting into the World 169
Creating a Web Browser 169
WebBrowser Properties 170
WebBrowser Methods 171
WebBrowser Events 172
Try It Out: Creating a Custom Web Browser Control 174
Web Services 179
Try It Out: Consuming a Web Service 181
Commercial Web Services 183
Try It Out: Web Service Registration 183
Amazon’s ItemSearch 184
Try It Out: Adding “Suggested Gift Ideas” 185
Visual Web Developer 2005 Express 196
Try It Out: Using Web Developer Express 196
Summary 198
Exercise 198
Chapter 10: When Things Go Wrong 199
Protecting Your Code 199
Try, Try, and Try Again 200
Try It Out: Using Try and Catch 201
Let the Others Know! 203
Try It Out: Throwing Exceptions Around 204
Troubleshooting Your Code 205
Telling the Program to Stop 205
Keeping Track of Variables 207
Try It Out: Using the Debug Object 210
Gone Too Far and Don’t Want to Stop? 211
Try It Out: Using Edit and Continue 212
Summary 213
Exercise 213
02_595733 ftoc.qxd 12/1/05 1:32 PM Page x
xi
Contents
Part III: Making It Hum 215
Chapter 11: It’s Printing Time! 217
Timing Is Everything — Well, Almost 217
A Use for Timers 218
Try It Out: Using the Timer Effectively 220
Printing 224
Try It Out: Printing 226
System Components 231
Try It Out: Using System Components 232
Summary 239
Exercises 240
Chapter 12: Using XML 241
So What Is XML? 241
Extensible Means Just That 243
XML Attributes 244
Validating Data 244
Databases and XML 245
Try It Out: Exporting and Importing XML 246
The System.Xml Namespace 253
Try It Out: Creating a Wizard Form 256
Summary 277
Exercises 278
Chapter 13: Securing Your Program 279
Program Security 279
Role-Based Security 280
A Closer Look at Identity and Principal 282
Try It Out: Using Role-Based Security 282
Code-Based Security 283
Cryptography and Encryption 284
Secret Key Cryptography 285
Public Key Cryptography 285
Try It Out: Encrypting a Password 286
Summary 291
Exercise 291
02_595733 ftoc.qxd 12/1/05 1:32 PM Page xi
xii
Contents
Chapter 14: Getting It Out There 293
Installing the “Hard” Way 293
Just ClickOnce 294
Try It Out: Using ClickOnce 295
ClickOnce Options 299
ClickOnce Has Security and Signing, Too 302
Try It Out: Advanced Settings in ClickOnce 304
Summary 306
Exercise 306
Appendix A: Need More? What’s on the CD and Website 307
Appendix B: .NET — The Foundation 309
Appendix C: Answers to Exercises 317
Index 341
02_595733 ftoc.qxd 12/1/05 1:32 PM Page xii
Acknowledgments
While I would love to claim that this book is the result of only my own work, it just wouldn’t be true.
Without the help of a number of colleagues, I would not have been able to complete this book at all,
let alone with the high quality of examples and the accuracy of code listings that you’ll find throughout
the chapters.
In particular, I would like to thank the following people from Microsoft who have been continuously
available to help out when I was stuck with various beta builds of Visual Basic Express and who gave
me excellent feedback that made the book better — Charles Sterling, Frank Arrigo, Ari Bixhorn, and Jay
Roxe. There are a heck of a lot of other Microsoft guys in Australia and the United States who have
helped out in various ways, too — to all of you, a big THANK YOU!
It also helped that I had an awesome set of fellow developers out there who are as committed to helping
people learn how to program as I am, and the following names are just some of the guys who have
encouraged me in a myriad of ways while I was writing this book. So, to Tony Gray, Nick Wienholt,
Nick Randolph, Greg Low, Mitch Denny, Carl “GoatBoy” Belle, Kevin Johnson, and “uber-boss” Pierre
Le Grange: You all know what you did and it was all worth it — thanks for sharing the passion I have to
help other people get into programming.
Saving the best for last — I want to thank my family. Without the support of my wife, Glenda, and her
understanding and acceptance of the many late nights and absences while I slaved away at this book, it
just wouldn’t have been possible at all. And to my kids, Jacob and Ashleigh, I love you, and thanks for
loving me back!
One last note — in a pretty special way, I’ve written this book for my son, Jacob. He’s convinced that he
wants to follow in my footsteps as a programmer, and I feel privileged to be able to write a book that
will help him learn how to program, too. It’s not often that a father has an opportunity to help his children in this unique way, and I’m very thankful that I can do it for him.
Jake, you rock, little buddy!
03_595733_flast.qxd 12/1/05 1:33 PM Page xiii
03_595733_flast.qxd 12/1/05 1:33 PM Page xiv