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

Beginning Microsoft Visual Basic 2008 phần 1 pdf

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 (1.08 MB, 92 trang )

ffirs.indd viffirs.indd vi 4/2/08 5:13:18 PM4/2/08 5:13:18 PM
Beginning
Microsoft
®
Visual Basic
®
2008
Acknowledgments xi
Introduction xxvii
Chapter 1: Welcome to Visual Basic 2008 1
Chapter 2: The Microsoft .NET Framework 25
Chapter 3: Writing Software 37
Chapter 4: Controlling the Flow 87
Chapter 5: Working with Data Structures 133
Chapter 6: Extensible Application Markup Language (XAML) 185
Chapter 7: Building Windows Applications 211
Chapter 8: Displaying Dialog Boxes 257
Chapter 9: Creating Menus 301
Chapter 10: Debugging and Error Handling 325
Chapter 11: Building Objects 363
Chapter 12: Advanced Object-Oriented Techniques 403
Chapter 13: Building Class Libraries 439
Chapter 14: Creating Windows Forms User Controls 457
Chapter 15: Programming Custom Graphics 485
Chapter 16: Accessing Databases 535
Chapter 17: Database Programming with SQL Server and ADO.NET 555
Chapter 18: ASP.NET 617
Chapter 19: Web Projects 653
Chapter 20: Visual Basic 2008 and XML 677
Chapter 21: Distributed Computing with Windows


Communication Foundation 717
Chapter 22: Building a Sequential Workflow Using the Windows
Workflow Foundation 737
Chapter 23: Building Mobile Applications 755
Chapter 24: Deploying Your Application 775
Chapter 25: Where to Now? 793
(Continued)
ffirs.indd iffirs.indd i 4/2/08 5:13:16 PM4/2/08 5:13:16 PM
Appendix A: Exercise Solutions 797
Appendix B: Using the Microsoft Solutions Framework 827
Appendix C: An Introduction to Code Security and SSL 835
Appendix D: An Introduction to Windows CardSpace 841
Appendix E: .NET Framework Differences 849
Index 853
ffirs.indd iiffirs.indd ii 4/2/08 5:13:17 PM4/2/08 5:13:17 PM
Beginning
Microsoft
®
Visual Basic
®
2008
Thearon Willis
and
Bryan Newsome
Wiley Publishing, Inc.
ffirs.indd iiiffirs.indd iii 4/2/08 5:13:17 PM4/2/08 5:13:17 PM
Beginning Microsoft
®
Visual Basic
®

2008
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-19134-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data
Willis, Thearon.
Beginning Microsoft Visual basic 2008 / Thearon Willis and Bryan Newsome.
p. cm.
Includes index.
ISBN 978-0-470-19134-7 (paper/website)
1. Microsoft Visual BASIC 2. BASIC (Computer program language)
I. Newsome, Bryan, 1971- II. Title.
QA76.73.B3W45556 2008
005.2'762—dc22
2008004982
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
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 war-
ranties, 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 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, Wrox 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. Microsoft and Visual
Basic are registered trademarks 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 electronic books.
ffirs.indd ivffirs.indd iv 4/2/08 5:13:17 PM4/2/08 5:13:17 PM
For my daughter, Stephanie, my most precious gift from God.
For Wendy, my love and friend in Christ.
— Thearon
To Jennifer and Katelyn.
— Love, Bryan
ffirs.indd vffirs.indd v 4/2/08 5:13:18 PM4/2/08 5:13:18 PM
ffirs.indd viffirs.indd vi 4/2/08 5:13:18 PM4/2/08 5:13:18 PM

About the Authors
Thearon Willis currently works as a senior developer and builds Windows applications and add - ins for
Microsoft Office products using Microsoft Visual Basic 2008. Over the years, Thearon has worked on a
variety of systems from mainframe to client - server development.
Bryan Newsome works as a director for a custom software solutions company specializing in Microsoft
applications. Since starting his career building Visual Basic 5 solutions, he has embraced each new
version Visual Basic and now creates all new solutions leveraging the .NET platform and VB.NET.
He provides clients with solutions and mentoring on leading - edge Microsoft technologies. For VB.NET,
Bryan is a Microsoft Certified Application Developer.
ffirs.indd viiffirs.indd vii 4/2/08 5:13:18 PM4/2/08 5:13:18 PM
ffirs.indd viiiffirs.indd viii 4/2/08 5:13:18 PM4/2/08 5:13:18 PM
Acquisitions Editor
Katie Mohr
Development Editors
Sara Shlaer
Sydney Jones
Technical Editor
Mark Lavoie
Production Editor
Kathryn Duggan
Copy Editor
Travis Henderson
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

Project Coordinator, Cover
Lynsey Stanford
Proofreader
Jeremy Bagai, Word One New York Editing &
Proofreading
Indexer
Johnna VanHoose Dinse
Credits
ffirs.indd ixffirs.indd ix 4/2/08 5:13:18 PM4/2/08 5:13:18 PM
ffirs.indd xffirs.indd x 4/2/08 5:13:19 PM4/2/08 5:13:19 PM
Acknowledgments
First and foremost I want to thank God for giving me the wisdom and knowledge to share with others
and for the many talents that he has blessed me with. I would also like to thank Katie Mohr for giving
me the opportunity to write this book and Sara Shlaer and Sydney Jones for their hard work in editing.
Also thanks to all the other people at Wiley who work so hard to bring this book to market. I ’ d be
remiss if I didn ’ t thank my good friend and co - author Bryan Newsome; thanks for your hard work
and dedication.
— Thearon
So many people put so much effort into publishing this work. Thank you all; this would not be possible
without your hard work and dedication. Special thanks to Katie Mohr, Sara Shlaer, Sydney Jones, and
Mark Lavoie for making my work readable and technically sound. It was a pleasure to work with you all
over the past months. Thank you Thearon; you were a terrific mentor for me and the reason I ever had
the opportunity to write.
— Bryan
ffirs.indd xiffirs.indd xi 4/2/08 5:13:19 PM4/2/08 5:13:19 PM
ffirs.indd xiiffirs.indd xii 4/2/08 5:13:19 PM4/2/08 5:13:19 PM
Contents
Acknowledgments xi
Introduction xxvii
Chapter 1: Welcome to Visual Basic 2008 1

Event-Driven Programming 2
Installing Visual Basic 2008 3
The Visual Basic 2008 IDE 7
The Profile Setup Page 7
The Menu 8
The Toolbars 9
Creating a Simple Application 10
The Toolbox 14
Modified Hungarian Notation 18
The Code Editor 19
Using the Help System 23
Summary 24
Exercise 24
Chapter 2: The Microsoft .NET Framework 25
Microsoft’s Reliance on Windows 25
MSN 1.0 26
The .NET Vision 27
This Sounds like Java 28
Where Now? 29
Writing Software for Windows 29
The .NET Framework Classes 30
Executing Code 31
Common Language Runtime 32
Code Loading and Execution 33
Application Isolation 33
Security 33
Interoperation 34
Exception Handling 34
The Common Type System and Common Language Specification 35
Summary 35

ftoc.indd xiiiftoc.indd xiii 4/2/08 5:13:54 PM4/2/08 5:13:54 PM
Contents
xiv
Chapter 3: Writing Software 37
Information and Data 37
Algorithms 38
What Is a Programming Language? 39
Working with Variables 39
Comments and Whitespace 42
Comments 42
Whitespace 44
Data Types 44
Working with Numbers 45
Common Integer Math Operations 45
Integer Math Shorthand 48
Working with Strings 52
Using Dates 61
Boolean 68
Storing Variables 69
Binary 69
Bits and Bytes 70
Representing Values 70
Converting Values 72
Methods 73
Why Use Methods? 74
Methods You’ve Already Seen 75
Building a Method 78
Choosing Method Names 81
Scope 82
Summary 84

Exercises 85
Chapter 4: Controlling the Flow 87
Making Decisions 87
The If Statement 88
The Else Statement 90
Allowing Multiple Alternatives with ElseIf 91
Nested If Statements 92
Single-Line If Statement 92
Comparison Operators 93
String Comparison 103
ftoc.indd xivftoc.indd xiv 4/2/08 5:13:55 PM4/2/08 5:13:55 PM
Contents
xv
Select Case 105
Case-Insensitive Select Case 108
Multiple Selections 112
The Case Else Statement 113
Different Data Types with Select Case 114
Loops 114
The For . . . Next Loop 115
The For Each . . . Next Loop 120
The Do . . . Loop Loops 121
Nested Loops 127
Quitting Early 128
Infinite Loops 131
Summary 132
Exercises 132
Chapter 5: Working with Data Structures 133
Understanding Arrays 133
Defining and Using Arrays 134

Using For Each . . . Next 137
Passing Arrays as Parameters 139
Sorting Arrays 142
Going Backwards 143
Initializing Arrays with Values 144
Understanding Enumerations 145
Using Enumerations 145
Determining the State 150
Setting Invalid Values 152
Understanding Constants 153
Using Constants 153
Different Constant Types 155
Structures 155
Building Structures 156
Adding Properties to Structures 159
Working with ArrayLists 160
Using an ArrayList 160
Deleting from an ArrayList 164
Showing Items in the ArrayList 167
Working with Collections 168
Creating CustomerCollection 169
Adding an Item Property 170
ftoc.indd xvftoc.indd xv 4/2/08 5:13:55 PM4/2/08 5:13:55 PM
Contents
xvi
Building Lookup Tables with Hashtable 172
Using Hashtables 172
Cleaning Up: Remove, RemoveAt, and Clear 176
Case Sensitivity 178
Advanced Array Manipulation 180

Dynamic Arrays 180
Using Preserve 182
Summary 183
Exercises 184
Chapter 6: Extensible Application Markup Language (XAML) 185
What Is XAML? 185
XAML Syntax 187
Windows Presentation Foundation 190
Creating a Rich WPF User Interface 191
Using WPF Common Controls 197
Wiring Up Events 205
Summary 209
Exercise 209
Chapter 7: Building Windows Applications 211
Responding to Events 211
Setting Up a Button Event 212
Building a Simple Application 219
Building the Form 219
Counting Characters 223
Counting Words 226
Creating More Complex Applications 232
The Text Editor Project 232
Creating the Toolbar 233
Creating the Status Bar 238
Creating an Edit Box 240
Clearing the Edit Box 242
Responding to Toolbar Buttons 244
Using Multiple Forms 251
The About Dialog Box 251
Summary 255

Exercises 255
ftoc.indd xviftoc.indd xvi 4/2/08 5:13:55 PM4/2/08 5:13:55 PM
Contents
xvii
Chapter 8: Displaying Dialog Boxes 257
The MessageBox Dialog Box 257
Available Icons for MessageBox 258
Available Buttons for MessageBox 259
Setting the Default Button 259
Miscellaneous Options 260
The Show Method Syntax 260
Example Message Boxes 262
The OpenDialog Control 265
The OpenFileDialog Control 265
The Properties of OpenFileDialog 266
The Methods of OpenFileDialog 267
Using the OpenFileDialog Control 268
The SaveDialog Control 273
The Properties of SaveFileDialog 273
The Methods of SaveFileDialog 274
Using the SaveFileDialog Control 274
The FontDialog Control 278
The Properties of FontDialog 278
The Methods of FontDialog 279
Using the FontDialog Control 279
The ColorDialog Control 282
The Properties of ColorDialog 283
Using the ColorDialog Control 284
The PrintDialog Control 285
The Properties of PrintDialog 286

Using the PrintDialog Control 286
The PrintDocument Class 287
Printing a Document 287
The FolderBrowserDialog Control 294
The Properties of FolderBrowserDialog 295
Using the FolderBrowserDialog Control 295
Summary 298
Exercises 299
Chapter 9: Creating Menus 301
Understanding Menu Features 301
Images 302
Access Keys 302
Shortcut Keys 302
ftoc.indd xviiftoc.indd xvii 4/2/08 5:13:56 PM4/2/08 5:13:56 PM
Contents
xviii
Check Marks 302
The Properties Window 303
Creating Menus 304
Designing the Menus 304
Adding Toolbars and Controls 306
Coding Menus 308
Coding the View Menu and Toolbars 312
Testing Your Code 314
Context Menus 316
Creating Context Menus 317
Enabling and Disabling Menu Items and Toolbar Buttons 320
Summary 324
Exercise 324
Chapter 10: Debugging and Error Handling 325

Major Error Types 326
Syntax Errors 326
Execution Errors 329
Logic Errors 329
Debugging 331
Creating a Sample Project 331
Setting Breakpoints 347
Debugging Using the Watch Window 354
Debugging with the Locals Window 356
Error Handling 358
Using Structured Error Handling 359
Summary 361
Exercises 362
Chapter 11: Building Objects 363
Understanding Objects 363
Encapsulation 365
Methods and Properties 365
Events 365
Visibility 366
What Is a Class? 367
Building Classes 367
Reusability 368
ftoc.indd xviiiftoc.indd xviii 4/2/08 5:13:56 PM4/2/08 5:13:56 PM
Contents
xix
Designing an Object 369
State 370
Behavior 370
Storing State 371
Real Properties 374

Read/Write Properties 377
The IsMoving Method 380
Constructors 382
Inheritance 384
Adding New Methods and Properties 385
Adding a GetPowerToWeightRatio Method 387
Changing Defaults 389
Polymorphism: Scary Word, Simple Concept 391
Overriding More Methods 392
Inheriting from the Object Class 394
Objects and Structures 395
The Framework Classes 396
Namespaces 396
The Imports Statement 398
Creating Your Own Namespace 399
Inheritance in the .NET Framework 401
Summary 402
Exercises 402
Chapter 12: Advanced Object-Oriented Techniques 403
Building a Favorites Viewer 403
Internet Shortcuts and Favorites 404
Using Classes 407
Scanning Favorites 413
Viewing Favorites 420
An Alternative Favorite Viewer 422
Building a Favorites Tray 422
Displaying Favorites 424
Using Shared Properties and Methods 428
Using Shared Procedures 428
Using Shared Methods 433

Understanding Object-Oriented Programming and Memory Management 434
Garbage Collection 435
Releasing Resources 436
Defragmentation and Compaction 437
Summary 438
Exercise 438
ftoc.indd xixftoc.indd xix 4/2/08 5:13:56 PM4/2/08 5:13:56 PM
Contents
xx
Chapter 13: Building Class Libraries 439
Understanding Class Libraries 440
Creating a Class Library 440
Building a Class Library for Favorites Viewer 442
A Multitiered Application 445
Using Strong Names 446
Signing Assemblies 447
Assembly Versions 449
Registering Assemblies 449
Gacutil Utility 450
Why Is My Assembly Not Visible in the References Dialog Box? 450
Designing Class Libraries 452
Using Third-Party Class Libraries 453
Viewing Classes with the Object Browser 454
Summary 455
Exercise 455
Chapter 14: Creating Windows Forms User Controls 457
Windows Forms Controls 458
Creating and Testing a User Control 458
Exposing Properties from User Controls 462
Adding Properties 462

Exposing Methods from User Controls 464
Exposing Events from User Controls 465
Design Time or RunTime 470
Creating a Command Link Control 472
Building the Command Link Control 473
Using the Command Link Control 481
Summary 484
Exercise 484
Chapter 15: Programming Custom Graphics 485
Building a Simple Paint Program 485
Creating a Project with User Controls 486
How Drawing Programs Work 486
The GraphicsItem Class 488
Screen and Client Coordinates 490
Listening to the Mouse and Drawing GraphicsCircle Objects 491
Invalidation 496
ftoc.indd xxftoc.indd xx 4/2/08 5:13:57 PM4/2/08 5:13:57 PM
Contents
xxi
Optimized Drawing 497
Choosing Colors 498
Responding to Clicks 504
Dealing with Two Colors 507
Indicating the Assigned Buttons 509
Using Advanced Colors 516
Using Different Tools 520
Implementing Hollow Circle 521
Working with Images 525
Drawing Images 526
Scaling Images 528

Preserving the Aspect Ratio 530
More Graphics Methods 533
Summary 533
Chapter 16: Accessing Databases 535
What Is a Database? 535
Microsoft Access Objects 536
Tables 536
Queries 536
The SQL SELECT Statement 537
Queries in Access 539
Creating a Customer Query 539
Data Access Components 543
DataSet 544
DataGridView 544
BindingSource 545
BindingNavigator 545
TableAdapter 545
Data Binding 546
Summary 552
Exercises 553
Chapter 17: Database Programming with SQL Server and ADO.NET 555
ADO.NET 556
ADO.NET Data Namespaces 557
The SqlConnection Class 558
SqlCommand 560
SqlDataAdapter 562
The DataSet Class 566
DataView 567
ftoc.indd xxiftoc.indd xxi 4/2/08 5:13:57 PM4/2/08 5:13:57 PM
Contents

xxii
The ADO.NET Classes in Action 570
Examining a DataSet Example 570
Data Binding 578
BindingContext and CurrencyManager 579
Binding Controls 580
LINQ to SQL 610
Summary 614
Exercises 615
Chapter 18: ASP.NET 617
Thin-Client Architecture 618
Web Forms versus Windows Forms 619
Windows Forms Advantages 619
Web Forms Advantages 619
Web Applications: The Basic Pieces 620
Web Servers 620
Browsers 620
HyperText Markup Language 620
VBScript and JavaScript 621
Cascading Style Sheets 621
Active Server Pages 621
Benefits of ASP.NET Web Pages 622
Special Web Site Files 622
Development 622
Controls: The Toolbox 623
Building Web Applications 623
Creating a Web Form for Client- and Server-Side Processing 623
Web Site Locations with VS 2008 628
Performing Data Entry and Validation 630
Designing the Site’s Look and Feel 635

Using the GridView to Build a Data-Driven Web Form 645
Summary 651
Exercises 651
Chapter 19: Web Projects 653
Web Site Authentication 653
Windows Authentication 654
Forms Authentication 654
ftoc.indd xxiiftoc.indd xxii 4/2/08 5:13:57 PM4/2/08 5:13:57 PM
Contents
xxiii
Web Site Administration Tool (WAT) 654
Login Controls 662
Summary 675
Exercises 675
Chapter 20: Visual Basic 2008 and XML 677
Understanding XML 677
What Does XML Look Like? 678
XML for Visual Basic Newcomers 680
The Address Book Project 681
Creating the Project 681
The SerializableData Class 682
Loading the XML File 688
Changing the Data 691
Sending E-mail 692
Creating a List of Addresses 694
Ignoring Members 698
Loading Addresses 701
Adding New Addresses 702
Navigating Addresses 704
Deleting Addresses 705

Integrating with the Address Book Application 707
Demonstrating the Principle of Integration 708
Reading the Address Book from Another Application 709
Summary 714
Exercises 715
Chapter 21: Distributed Computing with Windows
Communication Foundation 717
What Is a Web Service? 717
How Does a Web Service Work? 718
SOAP 719
Building a Web Service 721
A Web Services Demonstration 721
Adding More Methods 724
Understanding WCF Services 726
WCF services 726
Summary 735
Exercises 735
ftoc.indd xxiiiftoc.indd xxiii 4/2/08 5:13:58 PM4/2/08 5:13:58 PM

×