Visual Basic 2010 offers a great deal of functionality in both tools and language. No one book could ever cover Visual Basic 2010 in its entirety—you would need a library of books. What this book aims to do is to get you started as quickly and easily as possible. It shows you the roadmap, so to speak, of what there is and where to go. Once we’ve taught you the basics of creating working applications (creating the windows and controls, how your code should handle unexpected events, what object-oriented programming is, how to use it in your applications, and so on) we’ll show you some of the areas you might want to try your hand at next.
Table of Content
Introduction xxix
Chapter 1: Welcome to Visual Basic 2010 1
Event-Driven Programming 2
Installing Visual Basic 2010 3
The Visual Studio 2010 IDE 6
The Profile Setup Page 7
The Menu 7
The Toolbars 9
Creating a Simple Application 10
Windows in the Visual Studio 2010 IDE 11
The Toolbox 13
Modified Hungarian Notation 15
The Code Editor 16
Using the Help System 20
Summary 20
Chapter 2: The Microsoft .Net Framework 23
Microsoft’s Reliance on Windows 23
MSN 1.0 24
The .NET Vision 25
This Sounds Like Java 26
Where Now? 27
Writing Software for Windows 27
The .NET Framework Classes 28
Executing Code 29
Common Language Runtime 30
Code Loading and Execution 31
Application Isolation 31
Security 31
Interoperability 32
Exception Handling 32
The Common Type System and Common Language Specification 33
Summary 33
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 43
Data Types 44
Working with Numbers 44
Common Integer Math Operations 45
Integer Math Shorthand 47
The Problem with Integer Math 48
Floating-Point Math 49
Other States 51
Single-Precision Floating-Point Numbers 51
Working with Strings 51
Concatenation 53
Using the Concatenation Operator Inline 54
More String Operations 54
Substrings 55
Formatting Strings 56
Localized Formatting 58
Replacing Substrings 58
Using Dates 59
Formatting Date Strings 60
Extracting Date Properties 61
Date Constants 62
Defining Date Literals 63
Manipulating Dates 64
Boolean 65
Storing Variables 65
Binary 66
Bits and Bytes 66
Representing Values 67
Converting Values 68
Methods 70
Why Use Methods? 70
Methods You’ve Already Seen 71
Building a Method 75
Choosing Method Names 77
Scope 78
Summary 80
Chapter 4: Controlling the Flow 83
Making Decisions 83
The If Statement 84
The Else Statement 86
Allowing Multiple Alternatives with Else If 86
Nested If Statements 88
Single-Line If Statement 88
Comparison Operators 88
Using Not Equal To 88
Using the Numeric Operators 90
The And and Or Operators 93
Using the And Operator 95
More on And and Or 96
String Comparison 97
Select Case 98
Case-Insensitive Select Case 101
Multiple Selections 104
The Case Else Statement 105
Different Data Types with Select Case 105
Loops 106
The For … Next Loop 106
Using the Step Keyword 108
Looping Backwards 109
The For Each … Next Loop 110
The Do … Loop Loops 111
Do While … Loop 113
Acceptable Expressions for a Do … Loop 114
Other Versions of the Do … Loop 114
Nested Loops 115
Quitting Early 117
Quitting Do … Loops 118
Infinite Loops 119
Summary 120
Chapter 5: Working With Data Structures 123
Understanding Arrays 123
Defining and Using Arrays 123
Using For Each … Next 126
Passing Arrays As Parameters 128
Sorting Arrays 130
Going Backwards 131
Initializing Arrays with Values 132
Understanding Enumerations 133
Using Enumerations 134
Determining the State 137
Setting Invalid Values 139
Understanding Constants 139
Using Constants 140
Different Constant Types 142
Structures 142
Building Structures 142
Adding Properties to Structures 145
Working with Array Lists 146
Using an Array List 146
Deleting from an Array List 149
Showing Items in the Array List 152
Working with Collections 153
Creating Customer Collection 154
Adding an Item Property 155
Building Lookup Tables with Hashtable 157
Using Hashtables 157
Cleaning Up: Remove, Remove At, and Clear 160
Case Sensitivity 162
Advanced Array Manipulation 164
Dynamic Arrays 164
Using Preserve 165
Summary 166
Chapter 6: Extensible Application Markup Language (XAML) 169
What is XAML? 170
XAML Syntax 171
Windows Presentation Foundation 174
Creating a Rich WPF User Interface 175
Using WPF Common Controls 180
Wiring Up Events 186
Summary 189
Chapter 7: Building Windows Applications 193
Responding to Events 193
Setting Up a Button Event 194
Building a Simple Application 201
Building the Form 201
Counting Characters 205
Counting Words 207
Creating More Complex Applications 213
The Text Editor Project 213
Creating the Toolbar 214
Creating the Status Bar 218
Creating an Edit Box 220
Clearing the Edit Box 222
Responding to Toolbar Buttons 224
Using Multiple Forms 230
The About Dialog Box 230
Summary 233
Chapter 8: Displaying Dialog Boxes 237
The Message Box 237
Available Icons for Message Box 238
Available Buttons for Message Box 239
Setting the Default Button 239
Miscellaneous Options 239
The Show Method Syntax 240
Example Message Boxes 241
The Open File Dialog Control 244
The Open File Dialog Control 245
The Properties of Open File Dialog 246
Open File Dialog Methods 247
Using the Open File Dialog Control 247
The Save Dialog Control 252
The Properties of Save File Dialog 252
Save File Dialog Methods 253
Using the Save File Dialog Control 253
The Font Dialog Control 257
The Properties of Font Dialog 257
The Methods of Font Dialog 257
Using the Font Dialog Control 258
The Color Dialog Control 260
The Properties of Color Dialog 261
Using the Color Dialog Control 261
The Print Dialog Control 263
The Properties of Print Dialog 264
Using the Print Dialog Control 264
The Print Document Class 264
The Properties of the Print Document Class 265
Printing a Document 265
The Folder Browser Dialog Control 271
The Properties of Folder Browser Dialog 272
Using the Folder Browser Dialog Control 272
Summary 275
Chapter 9: Creating Menus 277
Understanding Menu Features 277
Images 278
Access Keys 278
Shortcut Keys 278
Check Marks 278
The Properties Window 279
Creating Menus 280
Designing the Menus 280
Adding Toolbars and Controls 282
Coding Menus 283
Coding the View Menu and Toolbars 287
Testing Your Code 288
Context Menus 290
Creating Context Menus 291
Enabling and Disabling Menu Items and Toolbar Buttons 294
Summary 297
Chapter 10: Debugging and Error Handling 299
Major Error Types 300
Syntax Errors 300
Execution Errors 303
Logic Errors 304
Debugging 305
Creating a Sample Project 305
Setting Breakpoints 321
Debugging Using the Watch Window and Quick Watch Dialog Box 327
Debugging with the Autos Window 330
Debugging with the Locals Window 330
Error Handling 331
Using Structured Error Handling 333
Summary 335
Chapter 11: Building Objects 339
Understanding Objects 339
Encapsulation 341
Methods and Properties 341
Events 341
Visibility 342
What is a Class? 342
Building Classes 343
Reusability 344
Designing an Object 345
State 346
Behavior 346
Storing State 347
Real Properties 349
Read/Write Properties 352
The Is Moving Method 355
Constructors 357
Inheritance 359
Adding New Methods and Properties 360
Adding a Get Power To Weight Ratio Method 363
Changing Defaults 365
Polymorphism: Scary Word, Simple Concept 366
Overriding More Methods 367
Inheriting from the Object Class 369
Objects and Structures 369
The Framework Classes 370
Namespaces 370
The Imports Statement 373
Creating Your Own Namespace 373
Inheritance in the .NET Framework 377
Summary 377
Chapter 12: Advanced Object-Oriented Techniques 381
Building a Favorites Viewer 381
Internet Shortcuts and Favorites 381
Using Classes 384
Scanning Favorites 390
Viewing Favorites 397
An Alternative Favorite Viewer 399
Building a Favorites Tray 399
Displaying Favorites 401
Using Shared Properties and Methods 404
Using Shared Properties 405
Using Shared Methods 408
Understanding Object-Oriented Programming and Memory Management 410
Garbage Collection 411
Releasing Resources 412
Defragmentation and Compaction 413
Summary 414
Chapter 13: Building Class Libraries 417
Understanding Class Libraries 418
Creating a Class Library 418
Building a Class Library for Favorites Viewer 420
A Multitiered Application 423
Using Strong Names 424
Signing Assemblies 425
Assembly Versions 428
Registering Assemblies 428
Gacutil Utility 428
Why is My Assembly Not Visible in the References Dialog Box? 429
Designing Class Libraries 429
Using Third-Party Class Libraries 430
Viewing Classes with the Object Browser 431
Summary 432
Chapter 14: Creating Windows Forms User Controls 435
Windows Forms Controls 436
Creating and Testing a User Control 436
Exposing Properties from User Controls 440
Adding Properties 440
Exposing Methods from User Controls 442
Exposing Events from User Controls 443
Design Time or Runtime 447
Creating a Command Link Control 449
Building the Command Link Control 450
Using the Command Link Control 458
Summary 460
Chapter 15: Accessing Databases 463
What is a Database? 464
Microsoft Access Objects 464
Tables 464
Queries 464
The SQL SELECT Statement 465
Queries in Access 467
Data Access Components and Controls 471
Data Set 471
Data Grid View 472
Binding Source 472
Binding Navigator 472
Table Adapter 472
Data Binding 473
Summary 479
Chapter 16: Database Programming With Sql Server and ADO.Net 483
ADO.NET 485
ADO.NET Data Namespaces 485
The Sql Connection Class 486
Working with the Connection String Parameters 486
Opening and Closing the Connection 487
The Sql Command Class 488
The Connection Property 488
The Command Text Property 489
The Parameters Collection 489
The Execute Non Query Method 490
The Sql Data Adapter Class 490
The Select Command Property 491
Using Command Builders to Create the Other Commands 493
The Fill Method 493
The Data Set Class 494
Data View 495
The Sort Property 496
The Row Filter Property 496
The Find Method 497
The ADO.NET Classes in Action 497
Examining a Data Set Example 498
Data Binding 506
Binding Context and Currency Manager 506
Binding Controls 507
Binding Example 508
Summary 533
Chapter 17: Dynamic Data Web Site 537
Creating a Dynamic Data Linq to SQL Web Site 537
Customizing the Design of a Dynamic Data Web Site 541
Page Templates 542
Entity Templates 542
Field Templates 542
Filter Templates 542
Summary 547
Chapter 18: ASP.Net 549
Thin-Client Architecture 550
Web Forms versus Windows Forms 551
Windows Forms Advantages 551
Web Forms Advantages 551
Web Applications: The Basic Pieces 552
Web Servers 552
Browsers 552
Hyper Text Markup Language 552
Java Script 553
Cascading Style Sheets 553
Active Server Pages 553
Benefits of ASP.NET Web Pages 553
Special Website Files 554
Global.asax 554
Web.config 554
Development 554
Controls: The Toolbox 555
Building Web Sites 555
Creating a Web Form for Client- and Server-Side Processing 555
Website Locations with VS 2010 560
Performing Data Entry and Validation 562
Site Layout, Themes, and Navigation 568
Using the Grid View to Build a Data-Driven Web Form 574
Summary 579
Chapter 19: Visual Basic 2010 and Xml 583
Understanding XML 583
What Does XML Look Like? 585
XML for Visual Basic Newcomers 586
The Rules 587
The Address Book Project 587
Creating the Project 587
The Serializable Data Class 589
Loading the XML File 594
Changing the Data 597
Sending E-mail 598
Creating a List of Addresses 599
Ignoring Members 603
Loading Addresses 605
Adding New Addresses 606
Navigating Addresses 607
Deleting Addresses 609
Testing at the Edges 611
Integrating with the Address Book Application 611
Demonstrating the Principle of Integration 612
Reading the Address Book from Another Application 613
Summary 618
Chapter 20: Deploying Your Application 621
What is Deployment? 621
Click Once Deployment 622
XCOPY Deployment 627
Creating a Visual Studio 2010 Setup Application 627
User Interface Editor 630
Deploying Different Solutions 633
Private Assemblies 634
Shared Assemblies 634
Deploying Desktop Applications 635
Deploying Web Applications 635
Deploying XML Web Services 636
Useful Tools 636
Summary 637
Appendix A: Exercise Solutions 639
Appendix B: Where to Now? 655
Index 661
About the author
Thearon Willis is an author or coauthor of nearly a dozen books and a senior consultant who develops intranet applications using ASP.NET, DHTML, XML, Java Script, VBScript, VB COM components, and SQL Server.
Bryan Newsome is an author or coauthor of??many books and works for a Microsoft Partner in Charlotte specializing in Custom Software Solutions. He provides clients with solutions and mentoring on leading-edge Microsoft technologies.
Wrox Beginning guides are crafted to make learning programming languages and technologies easier than you think, providing a structured, tutorial format that will guide you through all the techniques involved.