Programming Visual Basic 2008.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info Programming Visual Basic 2008 www.it-ebooks.info Other Microsoft .NET resources from O’Reilly Related titles ADO.NET 3.5 Cookbook™ C# 3.0 Pocket Reference Building a Web 2.0 Portal Learning ASP.NET 3.5 with ASP.NET 3.5 Programming ASP.NET 3.5 ™ C# Cookbook Programming ASP.NET AJAX C# 3.0 in a Nutshell Visual Basic 2005 Cookbook™ .NET Books dotnet.oreilly.com is a complete catalog of O’Reilly’s books on Resource Center .NET and related technologies, including sample chapters and code examples. ONDotnet.com provides independent coverage of fundamental, interoperable, and emerging Microsoft .NET programming and web services technologies. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. www.it-ebooks.info Programming Visual Basic 2008 Tim Patrick Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Programming Visual Basic 2008 by Tim Patrick Copyright © 2008 Tim Patrick. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: John Osborn Indexer: Ellen Troutman Zaig Production Editor: Rachel Monaghan Cover Designer: Karen Montgomery Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreader: Rachel Monaghan Illustrator: Robert Romano Printing History: May 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Visual Basic 2008, the image of a bufflehead duck, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-51843-1 [M] www.it-ebooks.info To Maki, my lovely wife www.it-ebooks.info www.it-ebooks.info Table of Contents Preface . xv 1. Introducing .NET . 1 Before .NET 1 Back to Introducing .NET 2 The .NET Object 3 The Parts of the .NET Framework 7 From Source Code to EXE 16 What About Visual Studio and Visual Basic? 17 Visual Studio 2008 18 Summary 22 Project 23 2. Introducing Visual Basic . 30 The History of the Visual Basic Revolution 30 Visual Basic from the Inside Out 32 The Basics of Logic and Data 32 Data Types and Variables 35 Intermission 44 Comments 44 Option Statements 45 Basic Operators 47 Using Functions and Subroutines 49 Conditions 50 Loops 54 Creating Your Own Procedures 57 Other Flow Control Features 61 vii www.it-ebooks.info Events and Event Handlers 64 Namespaces 66 The My Namespace 69 Summary 70 Project 70 3. Introducing the Project . 75 The Library Project 76 The Needs of the Users 79 The Life of a Project 83 Summary 90 Project 90 4. Designing the Database . 95 Relational Databases 95 SQL Server 2005 98 SQL 100 Using Databases in Visual Basic 107 Documenting the Database 108 Summary 109 Project 109 5. .NET Assemblies . 126 What Is an Assembly? 126 What’s Inside an Assembly? 128 Assemblies and Applications 131 The My Namespace and Assemblies 132 Directives and Assemblies 134 Summary 136 Project 137 6. Data and Data Types . 151 The Nature of Computer Data 151 Data in .NET 153 Visual Basic Data Types 158 Literals 159 Constants 160 viii | Table of Contents www.it-ebooks.info Enumerations 161 Variables 162 Variable and Constant Naming Conventions 165 Local Type Inference 166 Operators 167 Static Variables 172 Arrays 172 Nullable Types 175 Common Visual Basic Functions 176 Summary 181 Project 182 7. Windows Forms . 187 Inside a Windows Application 187 Windows in .NET 191 Making Forms Useful 210 Summary 212 Project 212 8. Classes and Inheritance . 219 Object-Oriented Programming Concepts 219 OOP in Visual Basic and .NET 224 Related Issues 241 Summary 244 Project 244 9. Functional Programming . 253 Lambda Expressions 253 Object Initializers 259 Error Handling in Visual Basic 260 The Nature of Errors in Visual Basic 260 Unstructured Error Handling 263 Structured Error Handling 265 Unhandled Errors 267 Managing Errors 269 Summary 273 Project 274 Table of Contents | ix www.it-ebooks.info 10. ADO.NET . 277 What Is ADO.NET? 278 Overview of ADO.NET 279 Data Sets Versus No Data Sets 282 Connecting to SQL Server with Visual Studio 284 Interacting with SQL Server in Code 289 Database Transactions 292 ADO.NET Entity Framework 293 Summary 294 Project 294 11. Security . 303 Security Features in .NET 304 Cryptography and Encryption 304 Encryption in .NET 306 Other Security Features 311 Summary 312 Project 312 12. Overloads and Extensions . 330 What Is Operator Overloading? 330 What Can You Overload? 332 Other Operator Overloading Issues 338 Extension Methods 340 Summary 342 Project 342 13. XML . 361 What Is XML? 361 The XML Rule 364 XML Content 365 Using XML in .NET: The Old Way 371 Using XML in .NET: The New Way 375 Summary 378 Project 379 x | Table of Contents www.it-ebooks.info 14. Application Settings . 392 A Short History of Settings 392 Settings in Visual Basic 2008 394 Summary 400 Project 401 15. Files and Directories . 416 Traditional Visual Basic File Management 417 Manipulating Files Through Streams 418 File Management with the My Namespace 424 Summary 427 Project 428 16. Generics . 433 What Are Generics? 433 Variations of Generic Declaration 436 Summary 442 Project 442 17. LINQ . 448 What Is LINQ? 448 Anonymous Types 450 LINQ to Objects 451 Basic Query Expressions 453 Converting Results to Other Forms 459 Aggregate Queries 459 Advanced Query Expressions 460 LINQ to XML 461 LINQ for ADO.NET-Related Data 463 Deferred Execution 468 Summary 469 Project 470 18. User Interface . 486 Overview of GDI+ 487 Selecting a Canvas 488 Choosing Pens and Brushes 490 Table of Contents | xi www.it-ebooks.info Flowing Text from the Font 493 Imagining Images 497 Exposing Your True Artist 499 Paths: Drawings on Macro-Vision 501 Keeping It Regional 502 Twisting and Turning with Transformations 503 Enhancing Controls Through Owner Draw 505 Windows Presentation Foundation 507 Enhancing Classes with Attributes 510 Summary 511 Project 511 19. Localization and Globalization . 526 Defining Globalization and Localization 526 Resource Files 527 The My.Resources Object 529 Localizing Forms Within Visual Studio 531 Adding Resources Outside Visual Studio 534 Manually Compiling Resources 535 Other Localization Features 537 Summary 538 Project 539 20. Printing . 552 Printing in Windows 553 Printing in .NET 554 Printing a Document 557 Print Preview 559 Counting and Numbering Pages 561 Printing in “Raw” Mode 563 Summary 564 Project 564 21. Reporting . 581 Report Options in .NET 581 Using Reporting Controls in .NET 584 Summary 597 Project 597 xii | Table of Contents www.it-ebooks.info 22. Licensing Your Application . 611 Software Licensing Options 611 License Agreements 615 Obfuscation 616 The Library Licensing System 618 Summary 621 Project 622 23. Web Development . 642 How the Internet Works 642 Programming the Internet 644 ASP.NET Features 645 Trying Out ASP.NET 646 More About Events 653 State and View State 654 Data Validation 655 Database Integration 657 Windows Communication Foundation 658 Summary 663 Project 663 24. Adding Online Help . 672 Windows Online Help Options 672 Designing HTML Help 674 Accessing HTML Help 680 Summary 683 Project 683 25. Deployment . 689 What’s Involved in Deployment? 689 Deployment Methods Within Visual Studio 690 Summary 700 Project 700 26. Project Complete . 711 The Library Project 711 Visual Basic Flexibility 714 The Programming Mindset 716 Summary 717 Table of Contents | xiii www.it-ebooks.info A. Installing the Software . 719 B. Software License Agreement . 722 Index . 725 xiv | Table of Contents www.it-ebooks.info Preface1 Welcome to Programming Visual Basic 2008! I know you’re going to enjoy it; I’ve read it five times already. You’re probably anxious to get to Chapter 1, but I recom- mend you read this preface to make sure you paid for the right book. Who Is Reading This Book? Writing a book is a lot like writing a Visual Basic application. Well, except for the part about finding a publisher, and working with an editor. And then there’s that pesky rule about correct spelling. Come to think of it, they’re really quite different. But in one way, books and programs are similar: both are written to meet the needs of the user. When writing software applications, the user’s needs drive the organiza- tion and features of the final program.