The comprehensive guide to Visual Basic 2012
Microsoft Visual Basic (VB) is the most popular programming language in the world, with millions of lines of code used in businesses and applications of all types and sizes. In this edition of the bestselling Wrox guide, Visual Basic expert Rod Stephens offers novice and experienced developers a comprehensive tutorial and reference to Visual Basic 2012. This latest edition introduces major changes to the Visual Studio development platform, including support for developing mobile applications that can take advantage of the Windows 8 operating system.
- This new edition includes information on developing Win8-compatible Metro applications using pre-loaded templates
- Explores the new design features and support for WPF designers
- Explains how to develop Windows smartphone apps
- Covers new VB language features such as Asynch and Await
Visual Basic 2012 Programmer’s Reference is the programmer’s go-to reference for the 2012 edition of Visual Basic.
表中的内容
INTRODUCTION xxvii
PART I: IDE
CHAPTER 1: INTRODUCTION TO THE IDE 3
Introducing the IDE 3
Different IDE Appearances 4
IDE Configurations 5
Projects and Solutions 6
Starting the IDE 6
Creating a Project 8
Saving a Project 11
Summary 13
CHAPTER 2: MENUS, TOOLBARS, AND WINDOWS 15
IDE Tools 15
Menus 16
File 16
Edit 18
View 19
Project 20
Build 24
Debug 24
Data 24
Format 25
Tools 25
Test 28
Window 28
Help 29
Toolbars 30
Secondary Windows 30
Toolbox 32
Properties Window 33
Summary 33
CHAPTER 3: WINDOWS FORMS DESIGNER 35
Introducing Windows Forms Designer 35
Setting Designer Options 35
Adding Controls 37
Selecting Controls 38
Copying Controls 39
Moving and Sizing Controls 40
Arranging Controls 40
Setting Properties 40
Setting Group Properties 41
Using Smart Tags 41
Adding Code to Controls 42
Summary 43
CHAPTER 4: WPF DESIGNER 45
Introducing WPF Designer 45
Editor Weaknesses 46
Recognizing Designer Windows 47
Adding Controls 48
Selecting Controls 49
Moving and Sizing Controls 50
Setting Properties 51
Setting Group Properties 51
Adding Code to Controls 52
Summary 53
CHAPTER 5: VISUAL BASIC CODE EDITOR 55
Editing Code 55
Margin Icons 56
Outlining 58
Tooltips 59
Intelli Sense 60
Code Coloring and Highlighting 61
Code Snippets 63
Architectural Tools 64
Rename 64
Go To Definition 64
Go To Type Definition 64
Highlight References 65
Find All References 65
Generate From Usage 65
The Code Editor at Run Time 66
Summary 68
CHAPTER 6: DEBUGGING 69
Debugging and Testing 69
The Debug Menu 70
The Debug ➪ Windows Submenu 72
The Breakpoints Window 74
The Command and Immediate Windows 75
Summary 77
PART II: GETTING STARTED
CHAPTER 7: SELECTING WINDOWS FORMS CONTROLS 81
Controls 81
Controls Overview 82
Choosing Controls 86
Containing and Arranging Controls 87
Making Selections 89
Entering Data 90
Displaying Data 90
Providing Feedback 91
Initiating Action 92
Displaying Graphics 94
Displaying Dialog Boxes 94
Third-Party Controls 95
Summary 96
CHAPTER 8: USING WINDOWS FORMS CONTROLS 97
Using Controls and Components 97
Controls and Components 98
Creating Controls 99
Properties 101
Properties at Design Time 101
Properties at Run Time 104
Useful Control Properties 106
Position and Size Properties 109
Methods 110
Events 110
Creating Event Handlers at Design Time 111
Validation Events 112
Summary 114
CHAPTER 9: WINDOWS FORMS 115
Using Forms 115
Transparency 116
About, Splash, and Login Forms 117
Mouse Cursors 118
Icons 120
Application Icons 121
Notification Icons 121
Properties Adopted by Child Controls 122
Property Reset Methods 123
Overriding Wnd Proc 123
MRU Lists 125
Dialog Boxes 126
Wizards 128
Summary 129
CHAPTER 10: SELECTING WPF CONTROLS 131
WPF Controls and Code 131
Controls Overview 132
Containing and Arranging Controls 133
Making Selections 136
Entering Data 137
Displaying Data 137
Providing Feedback 138
Initiating Action 138
Presenting Graphics and Media 139
Providing Navigation 140
Managing Documents 140
Digital Ink 141
Summary 142
CHAPTER 11: USING WPF CONTROLS 143
WPF Controls 143
WPF Concepts 144
Separation of User Interface and Code 144
WPF Control Hierarchies 145
WPF in the IDE 145
Editing XAML 146
Editing Visual Basic Code 147
XAML Features 148
Objects 148
Resources 151
Styles 152
Templates 153
Transformations 156
Animations 156
Drawing Objects 159
Procedural WPF 162
Documents 166
Flow Documents 166
Fixed Documents 168
XPS Documents 169
Summary 169
CHAPTER 12: WPF WINDOWS 171
Using WPF Windows 171
Window Applications 172
Page Applications 174
Browser Applications 174
Frame Applications 176
Summary 177
CHAPTER 13: PROGRAM AND MODULE STRUCTURE 179
Solutions and Projects 179
Hidden Files 180
Code File Structure 184
Code Regions 185
Conditional Compilation 186
Namespaces 193
Typographic Code Elements 195
Comments 195
XML Comments 195
Line Continuation 198
Implicit Line Continuation 199
Line Joining 200
Summary 200
CHAPTER 14: DATA TYPES, VARIABLES, AND CONSTANTS 203
Variables 203
Data Types 204
Type Characters 207
Data Type Conversion 210
Narrowing Conversions 210
Data Type Parsing Methods 212
Widening Conversions 213
The Convert Class 213
To String 213
Variable Declarations 214
Attribute_List 214
Accessibility 215
Shared 216
Shadows 216
Read Only 219
Dim 219
With Events 220
Name 221
Bounds_List 222
New 223
As Type and Inferred Types 224
Initialization_Expression 225
Initializing Collections 228
Multiple Variable Declarations 229
Option Explicit and Option Strict 230
Scope 233
Block Scope 233
Procedure Scope 234
Module Scope 234
Namespace Scope 235
Restricting Scope 235
Parameter Declarations 236
Property Procedures 238
Enumerated Data Types 240
Anonymous Types 243
Nullable Types 244
Constants 244
Accessibility 245
As Type 245
Initialization_Expression 246
Delegates 246
Naming Conventions 248
Summary 249
CHAPTER 15: OPERATORS 251
Understanding Operators 251
Arithmetic Operators 252
Concatenation Operators 253
Comparison Operators 253
Logical Operators 255
Bitwise Operators 257
Operator Precedence 257
Assignment Operators 259
The String Builder Class 260
Date and Time Span Operations 261
Operator Overloading 262
Summary 266
CHAPTER 16: SUBROUTINES AND FUNCTIONS 267
Managing Code 267
Subroutines 268
Attribute_List 268
Inheritance_Mode 272
Accessibility 273
Subroutine_Name 274
Parameters 274
Implements interface.subroutine 279
Statements 281
Functions 281
Property Procedures 283
Extension Methods 284
Lambda Functions 285
Relaxed Delegates 287
Asynchronous Methods 290
Calling End Invoke Directly 291
Handling a Callback 293
Using Async and Await 295
Summary 297
CHAPTER 17: PROGRAM CONTROL STATEMENTS 299
Controlling Programs 299
Decision Statements 299
Single-Line If Then 300
Multiline If Then 300
Select Case 301
Enumerated Values 304
IIf 304
If 306
Choose 306
Looping Statements 308
For Next 308
Non-Integer For Next Loops 311
For Each 311
Enumerators 314
Iterators 316
Do Loop Statements 316
While End 318
Summary 318
CHAPTER 18: ERROR HANDLING 321
The Struggle for Perfection 321
Bugs versus Unplanned Conditions 322
Catching Bugs 323
Catching Unplanned Conditions 324
Global Exception Handling 326
Structured Error Handling 328
Exception Objects 330
Throwing Exceptions 331
Re-throwing Exceptions 333
Custom Exceptions 334
Debugging 335
Summary 336
CHAPTER 19: DATABASE CONTROLS AND OBJECTS 337
Data Sources 337
Automatically Connecting to Data 338
Connecting to the Data Source 338
Adding Data Controls to the Form 341
Automatically Created Objects 344
Other Data Objects 345
Data Overview 346
Connection Objects 347
Transaction Objects 350
Data Adapters 352
Command Objects 356
Data Set 358
Data Table 360
Data Row 363
Data Column 365
Data Relation 366
Constraints 368
Data View 370
Data Row View 373
Simple Data Binding 373
Currency Manager 374
Complex Data Binding 377
Summary 379
CHAPTER 20: LINQ 381
The Many Faces of LINQ 381
Introduction to LINQ 383
Basic LINQ Query Syntax 384
From 385
Where 386
Order By 386
Select 387
Using LINQ Results 389
Advanced LINQ Query Syntax 390
Join 390
Group By 391
Aggregate Functions 393
Set Operations 394
Limiting Results 394
LINQ Functions 395
LINQ Extension Methods 397
Method-Based Queries 397
Method-Based Queries with Lambda Functions 399
Extending LINQ 401
LINQ to Objects 403
LINQ to XML 404
XML Literals 404
LINQ into XML 405
LINQ out of XML 406
LINQ to ADO.NET 409
LINQ to SQL and LINQ to Entities 409
LINQ to Data Set 410
PLINQ 413
Summary 414
CHAPTER 21: METRO-STYLE APPLICATIONS 417
Building Metro-Style Applications 417
Starting a New Project 418
Special Image Files 419
Building Metro Bones 420
Control Layout 421
XAML Code 421
Zooming in on the Controls 424
Visual Basic Code 424
Testing 428
Summary 429
PART III: OBJECT-ORIENTED PROGRAMMING
CHAPTER 22: OOP CONCEPTS 433
Introducing OOP 433
Classes 434
Encapsulation 436
Inheritance 437
Inheritance Hierarchies 438
Refi nement and Abstraction 438
“Has-a” and “Is-a” Relationships 441
Adding and Modifying Class Features 441
Interface Inheritance 443
Polymorphism 444
Method Overloading 445
Extension Methods 446
Summary 447
CHAPTER 23: CLASSES AND STRUCTURES 449
Packaging Data 449
Classes 450
Attribute_list 450
Partial 451
Accessibility 452
Shadows 453
Inheritance 454
Implements interface 456
Structures 456
Structures Cannot Inherit 457
Structures Are Value Types 457
Memory Required 457
Heap and Stack Performance 459
Object Assignment 459
Parameter Passing 460
Boxing and Unboxing 461
Class Instantiation Details 461
Structure Instantiation Details 464
Garbage Collection 466
Finalize 467
Dispose 469
Constants, Properties, and Methods 471
Events 473
Declaring Events 473
Raising Events 474
Catching Events 475
Shared Variables 477
Shared Methods 477
Summary 479
CHAPTER 24: NAMESPACES 481
Handling Name Confl icts 481
The Imports Statement 482
Automatic Imports 484
Namespace Aliases 486
Namespace Elements 486
The Root Namespace 487
Making Namespaces 487
Classes, Structures, and Modules 488
Resolving Namespaces 489
Summary 492
CHAPTER 25: COLLECTION CLASSES 493
Grouping Data 493
What Is a Collection? 494
Arrays 494
Array Dimensions 496
Lower Bounds 497
Resizing 497
Speed 498
Other Array Class Features 498
Collections 499
Array List 499
String Collection 501
Name Value Collection 501
Dictionaries 503
List Dictionary 503
Hashtable 504
Hybrid Dictionary 505
String Dictionary 505
Sorted List 505
Collections Util 505
Stacks and Queues 506
Stack 506
Queue 508
Generics 509
Collection Initializers 511
Iterators 512
Summary 513
CHAPTER 26: GENERICS 515
Class Creators 515
Advantages of Generics 516
Defining Generics 516
Generic Constructors 517
Multiple Types 518
Constrained Types 520
Instantiating Generic Classes 521
Imports Aliases 522
Derived Classes 523
Generic Collection Classes 523
Generic Methods 524
Generics and Extension Methods 524
Summary 526
PART IV: INTERACTING WITH THE ENVIRONMENT
CHAPTER 27: PRINTING 529
Printing Concepts 529
Basic Printing 530
Drawing Basics 534
Graphics Objects 534
Pens 536
Brushes 538
A Booklet Example 540
Summary 545
CHAPTER 28: CONFIGURATION AND RESOURCES 547
The Need for Configuration 547
My 548
Me and My 549
My Sections 549
Environment 550
Setting Environment Variables 550
Using Environ 551
Using System.Environment 551
Registry 553
Native Visual Basic Registry Methods 554
My.Computer.Registry 556
Configuration Files 559
Resource Files 562
Application Resources 562
Using Application Resources 563
Embedded Resources 564
Localization Resources 564
Application 566
Application Properties 566
Application Methods 567
Application Events 568
Summary 569
CHAPTER 29: STREAMS 571
Stream Concepts 571
Stream 572
File Stream 574
Memory Stream 575
Binary Reader and Binary Writer 576
Text Reader and Text Writer 578
String Reader and String Writer 579
Stream Reader and Stream Writer 580
Open Text, Create Text, and Append Text 581
Custom Stream Classes 582
Summary 583
CHAPTER 30: FILESYSTEM OBJECTS 585
Programming Approaches 585
Permissions 586
Visual Basic Methods 586
File Methods 586
File System Methods 588
Sequential-File Access 589
Random-File Access 589
Binary-File Access 592
.NET Framework Classes 592
Directory 592
File 594
Drive Info 595
Directory Info 596
File Info 598
File System Watcher 600
Path 602
My.Computer.File System 604
My.Computer.File System.Special Directories 606
Summary 606
PART V: APPENDICES
APPENDIX A: USEFUL CONTROL PROPERTIES, METHODS, AND EVENTS 611
APPENDIX B: VARIABLE DECLARATIONS AND DATA TYPES 619
APPENDIX C: OPERATORS 629
APPENDIX D: SUBROUTINE AND FUNCTION DECLARATIONS 637
APPENDIX E: CONTROL STATEMENTS 641
APPENDIX F: ERROR HANDLING 647
APPENDIX G: WINDOWS FORMS CONTROLS AND COMPONENTS 649
APPENDIX H: WPF CONTROLS 657
APPENDIX I: VISUAL BASIC POWER PACKS 665
APPENDIX J: FORM OBJECTS 669
APPENDIX K: CLASSES AND STRUCTURES 681
APPENDIX L: LINQ 685
APPENDIX M: GENERICS 695
APPENDIX N: GRAPHICS 699
APPENDIX O: USEFUL EXCEPTION CLASSES 711
APPENDIX P: DATE AND TIME FORMAT SPECIFIERS 715
APPENDIX Q: OTHER FORMAT SPECIFIERS 719
APPENDIX R: THE APPLICATION CLASS 725
APPENDIX S: THE MY NAMESPACE 729
APPENDIX T: STREAMS 747
APPENDIX U: FILESYSTEM CLASSES 755
APPENDIX V: VISUAL STUDIO VERSIONS 771
INDEX 773
关于作者
Rod Stephens is a VB programming guru and the author of more than two dozen programming books, including Stephens’ Visual Basic Programming 24-Hour Trainer. He also writes frequently for such magazines as Visual Basic Developer, Visual Basic Programmer’s Journal, and Dr. Dobb’s Journal. Rod’s VB Helper website (vb-helper.com) provides thousands of pages of tips, tricks, and code examples for VB programmers.