You are previewing Programming Visual Basic 2005.
O'Reilly logo
Programming Visual Basic 2005

Book Description

This newest programming guide by bestselling author Jesse Liberty isn't your typical Visual Basic book. It's not a primer on the language, and it won't dull your brain with arguments hyping .NET either. Its goal, rather, is to make you immediately productive, creating Windows and Web applications using Visual Basic 2005 and Visual Studio 2005.

Written for VB6 and novice programmers, the book shows how Visual Basic 2005 can be used to rapidly build modern Windows and web applications.

What makes this book different is what's not included. There's no introduction to Visual Basic, no explanation of how it fits into the .NET world. Why waste time reading about something you'll learn for yourself as soon as you start creating applications? You won't even write a "Hello World" program. With Programming Visual Basic 2005 you'll get started building something meaningful, right away.

The book is divided into three parts--Building Windows Applications, Building Web Applications, and Programming with Visual Basic--each of which could be a book on its own. The author shares his thorough understanding of the subject matter through lucid explanations and intelligently designed lessons that guide you to increasing levels of expertise. By the time you've finished the book, you'll know how to program both Windows and web applications with VB 2005.

The support for this book extends beyond its covers. Jesse offers a FAQ, Errata, complete source code and a link to a free private support discussion center on his web site: LibertyAssociates.com - just click on books.

Jesse Liberty, Microsoft .NET MVP, is the best-selling author of O'Reilly Media's Programming ASP.NET and over a dozen other books on web and object-oriented programming. Jesse is a frequent contributor to many industry publications and websites, and has spoken at numerous industry events. He is a former Distinguished Software Engineer at AT&T and Vice President for technology development at CitiBank.

Jesse Liberty's books have successfully guided thousands of programmers into the world of .NET programming, and Programming Visual Basic 2005 is no exception.

Table of Contents

  1. Programming Visual Basic 2005
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. What You Need to Know About This Book
      2. What You Need to Use This Book
      3. What I Threw Away
      4. How This Book Is Organized
        1. Part I: Building Windows Applications
        2. Part II: Building Web Applications
        3. Part III: Working with Visual Basic 2005
      5. Conventions Used in This Book
      6. Using Code Examples
      7. I'd Like to Hear from You
      8. Comments and Questions
      9. Safari Enabled
      10. Acknowledgments
    4. I. Building Windows Applications
      1. 1. Design and First Forms
        1. 1.1. The Requirements
        2. 1.2. Getting Started
          1. 1.2.1. Rearranging Windows
          2. 1.2.2. Renaming and Sizing the Form
          3. 1.2.3. MinimumSize and MaximumSize
          4. 1.2.4. Using Picture Boxes, Panels, and Labels
          5. 1.2.5. Adding Group Boxes, Buttons, and Text Boxes
            1. 1.2.5.1. Aligning controls
            2. 1.2.5.2. Copying and moving controls
          6. 1.2.6. Events
          7. 1.2.7. Creating Event Handlers
          8. 1.2.8. Default Event Handler
          9. 1.2.9. Event-Handler Parameters
          10. 1.2.10. Sharing Event Handlers
          11. 1.2.11. Differentiating Which Button Was Pressed
        3. 1.3. Creating the Customer Detail Page
          1. 1.3.1. Adding a Menu
          2. 1.3.2. Hooking Up the Second Form to the First
          3. 1.3.3. Adding Tab Controls
          4. 1.3.4. Adding Tabs to the TabControl
        4. 1.4. Summary
      2. 2. Data Access
        1. 2.1. Adding Data to the Customer Page
          1. 2.1.1. Create a Data Connection
          2. 2.1.2. Create a Data Set Declaratively
          3. 2.1.3. Binding Data Controls with Drag and Drop
          4. 2.1.4. Querying with Parameters
            1. 2.1.4.1. One row found in search
          5. 2.1.5. Finding More Than One Match
        2. 2.2. Using the Details View to Create the Detail Form
          1. 2.2.1. Adding New Tables
          2. 2.2.2. Create a Details View Declaratively
          3. 2.2.3. Declare Master/Detail Relationship
          4. 2.2.4. Feeding the Suppliers to the Suppliers Page
          5. 2.2.5. Factor Out Common Code
          6. 2.2.6. Updating Data
        3. 2.3. Modify the Display with Events
      3. 3. Cool Controls
        1. 3.1. Adding a Menu and Toolbar
          1. 3.1.1. Rename All the Menu Choices
          2. 3.1.2. Other Cool Controls
        2. 3.2. Displaying Web Documents
          1. 3.2.1. Setting Web Browser Event Handlers
          2. 3.2.2. Adding an Auto-Complete Text Box to Navigate URLs
          3. 3.2.3. Displaying XML documents
          4. 3.2.4. Using Drag and Drop
        3. 3.3. Masked Text Box
          1. 3.3.1. Hooking the Masked Control to the Data
        4. 3.4. Printing a Document
          1. 3.4.1. Handling the Print Click Event
        5. 3.5. Copying Files Using Tree Views
          1. 3.5.1. Populating the TreeView Controls
            1. 3.5.1.1. TreeNode objects
          2. 3.5.2. Displaying the Directories
            1. 3.5.2.1. Recursing through the subdirectories
            2. 3.5.2.2. Getting the files in the directory
          3. 3.5.3. Handling TreeView Events
            1. 3.5.3.1. Clicking the source TreeView
            2. 3.5.3.2. Expanding a directory
            3. 3.5.3.3. Clicking the target TreeView
            4. 3.5.3.4. Handling the Clear button event
          4. 3.5.4. Implementing the Copy Button Event
            1. 3.5.4.1. Getting the selected files
          5. 3.5.5. Handling the Delete Button Event
      4. 4. Custom Controls
        1. 4.1. Custom Controls
        2. 4.2. Design
        3. 4.3. Building the Controls
          1. 4.3.1. Building the Rolodex Panel
        4. 4.4. Using the Custom Controls
          1. 4.4.1. Building the Specialized Forms
          2. 4.4.2. Displaying the Rolodex, Step by Step
          3. 4.4.3. Clicking on an Entry
          4. 4.4.4. Walking Through a Letter Button Click
      5. 5. GDI+ and Drawing
        1. 5.1. The Graphics Class
          1. 5.1.1.
            1. 5.1.1.1. Coordinates
            2. 5.1.1.2. Transforms introduced
        2. 5.2. Implementing the Control
          1. 5.2.1.
            1. 5.2.1.1. The StringDraw class
            2. 5.2.1.2. The LtrDraw class
            3. 5.2.1.3. The DrawString.DrawTheString() method
            4. 5.2.1.4. Drawing the clock face
            5. 5.2.1.5. Drawing the date
          2. 5.2.2. Adding the Control to a Form
            1. 5.2.2.1. Drawing the numbers
            2. 5.2.2.2. Drawing the time
            3. 5.2.2.3. Drawing the hands
            4. 5.2.2.4. RotateTransform ()
            5. 5.2.2.5. FillEllipse
            6. 5.2.2.6. Refactor
          3. 5.2.3. Switching from 12-Hour to 24-Hour Display
      6. 6. Mice and Fonts
        1. 6.1. Click the Mouse
          1. 6.1.1. Using MouseDown, MouseUp, and MouseMove
          2. 6.1.2. Using MouseEnter and MouseLeave
          3. 6.1.3. Rebuild and Run the Application
          4. 6.1.4. Setting Fonts
      7. 7. Legacy COM Controls
        1. 7.1. Importing ActiveX Controls
          1. 7.1.1. Creating an ActiveX Control
          2. 7.1.2. Importing a Control in .NET
            1. 7.1.2.1. Importing a control
        2. 7.2. Importing COM Components
          1. 7.2.1. Integrating the COM DLL into .NET
          2. 7.2.2. Importing the Type Library
          3. 7.2.3. Creating a Test Program
          4. 7.2.4. Late Binding
    5. II. Building Web Applications
      1. 8. Web Application, Design, and First Forms
        1. 8.1. Understanding Web Forms
          1. 8.1.1. Web Form Events
          2. 8.1.2. Event Model
          3. 8.1.3. ASP.NET Events
          4. 8.1.4. Application and Session Events
          5. 8.1.5. Events in Visual Studio .NET
          6. 8.1.6. Multiple Controls to One Event Handler
          7. 8.1.7. Postback Versus Non-Postback Events
          8. 8.1.8. State
        2. 8.2. Getting Started
          1. 8.2.1. Code-Behind Files
          2. 8.2.2. Put a Toe in the Water
        3. 8.3. Adding Controls
          1. 8.3.1. Server Controls
          2. 8.3.2. Adding Controls and Events
          3. 8.3.3. Adding the Shipper Page
        4. 8.4. State
          1. 8.4.1. View State
          2. 8.4.2. State Bag
          3. 8.4.3. Session State
          4. 8.4.4. Session-State Configuration
          5. 8.4.5. Session-Scoped Application Objects
        5. 8.5. Lifecycle
        6. 8.6. Directives
          1. 8.6.1. Application Directive
          2. 8.6.2. Assembly Directive
      2. 9. Validation Controls
        1. 9.1. The RequiredFieldValidator
        2. 9.2. Client-Side Evaluation
        3. 9.3. The Summary Validator
        4. 9.4. The Compare Validator
          1. 9.4.1. Checking the Input Type
          2. 9.4.2. Comparing to Another Control
        5. 9.5. Range Checking
        6. 9.6. Regular Expressions
        7. 9.7. Custom Validation
        8. 9.8. Validation Groups
      3. 10. Master Pages and Navigation
        1. 10.1. Creating Master Pages
        2. 10.2. Navigation
          1. 10.2.1. Getting Started with Site Navigation
          2. 10.2.2. Setting Up the Pages
          3. 10.2.3. Customizing the Look and Feel
            1. 10.2.3.1. Limiting the number of links shown
          4. 10.2.4. Populating on Demand
      4. 11. Web Data Access
        1. 11.1. Getting Data from a Database
          1. 11.1.1. Create a Data Connection
          2. 11.1.2. Data Source Controls
          3. 11.1.3. Binding Data Controls with Drag and Drop
          4. 11.1.4. Adding Features to the Grid
          5. 11.1.5. Adding Insert, Update, and Delete Statements
        2. 11.2. Multiuser Updates
          1. 11.2.1. Lock the Records
          2. 11.2.2. Update Only the Records You Change
          3. 11.2.3. Compare Original Against New
          4. 11.2.4. Handle the Errors
          5. 11.2.5. Displaying and Updating the Grid
          6. 11.2.6. Take It for a Spin
          7. 11.2.7. Tracking the Update with Events
          8. 11.2.8. Modifying the Grid Based on Events
          9. 11.2.9. Using IntelliSense to Help You Identify the Type to Declare
          10. 11.2.10. Passing Parameters to the Select Query
        3. 11.3. The DataList Control
          1. 11.3.1. Organizing the Flow of the DataList
          2. 11.3.2. Editing Items in List Controls
          3. 11.3.3. Deleting Items from a List Control
          4. 11.3.4. Trying It Out
          5. 11.3.5. Examining One Record at a Time: The DetailsView
          6. 11.3.6. Examining Single Records with FormView (Master/Detail)
          7. 11.3.7. Inserting New Records
          8. 11.3.8. Updating the Drop-Down List
      5. 12. Personalization
        1. 12.1. Implementing Forms-Based Security
          1. 12.1.1. Creating the User Through IIS
          2. 12.1.2. Creating Accounts Programatically
          3. 12.1.3. Creating the Welcome Page
          4. 12.1.4. Create a Login Page
          5. 12.1.5. Adding a Password Reminder
        2. 12.2. Add Roles to ASP.NET Accounts
          1. 12.2.1. Create a New Application with Roles
          2. 12.2.2. Restricting Access to Pages Based on Roles
          3. 12.2.3. Restricting Access to a Set of Pages
        3. 12.3. Create Personalized Web Sites
          1. 12.3.1. Recording Personalization Information
            1. 12.3.1.1. Setting up profile handling
          2. 12.3.2. Exploring the Profile Tables
        4. 12.4. Personalize with Complex Types
        5. 12.5. Anonymous Personalization
          1. 12.5.1. Migrating the Anonymous Data to the Actual User's Record
        6. 12.6. Themes and Skins
          1. 12.6.1. Create the Test Site
          2. 12.6.2. Organize Site Themes and Skins
          3. 12.6.3. Enable Themes and Skins
          4. 12.6.4. Specify Themes for Your Page
          5. 12.6.5. Using Named Skins
        7. 12.7. Web Parts
          1. 12.7.1. Create a New Site
          2. 12.7.2. Web Parts Architecture
          3. 12.7.3. Creating Zones
          4. 12.7.4. Adding Controls to Zones
          5. 12.7.5. Minimizing and Restoring
        8. 12.8. Enabling Editing and Layout Changes
          1. 12.8.1. Creating a User Control to Enable Changing Page Layout
            1. 12.8.1.1. Moving a Part
            2. 12.8.1.2. Editing a Part
          2. 12.8.2. Adding Parts from a Catalog
      6. 13. Custom Controls
        1. 13.1. User Controls
          1. 13.1.1. Adding Code to User Controls
            1. 13.1.1.1. Control properties
        2. 13.2. Custom Controls
          1. 13.2.1. Creating a Web Control Library
          2. 13.2.2. Web Custom Control 1
          3. 13.2.3. Properties
          4. 13.2.4. The Render Method
          5. 13.2.5. Rendering Text with Tags
          6. 13.2.6. Maintaining State
          7. 13.2.7. Creating Derived Controls
          8. 13.2.8. Adding the Derived Custom Control to the ASPX Page
          9. 13.2.9. Creating Composite Controls
          10. 13.2.10. Modifying the CountedButton Derived Control
          11. 13.2.11. Creating the BookCounter Control
          12. 13.2.12. INamingContainer
          13. 13.2.13. Creating the BookInquiryList Composite Control
          14. 13.2.14. Declaring the BookCounters in the .aspx File
          15. 13.2.15. Implementing BookInquiryList.Render
          16. 13.2.16. Assignment of Responsibilities
      7. 14. Web Services
        1. 14.1. Platform Independence
        2. 14.2. How Web Services Work
        3. 14.3. Creating a Web Service
        4. 14.4. WebMethod Properties
          1. 14.4.1. The BufferResponse Property
          2. 14.4.2. The CacheDuration Property
          3. 14.4.3. The Description Property
          4. 14.4.4. The EnableSession Property
          5. 14.4.5. The TransactionOption Property
          6. 14.4.6. The MessageName Property
        5. 14.5. Testing Your Web Service
          1. 14.5.1. Examining the WSDL
        6. 14.6. Writing the Client
    6. III. Programming with Visual Basic 2005
      1. 15. Visual Studio 2005
        1. 15.1. Start Page
        2. 15.2. Projects and Solutions
          1. 15.2.1. Templates
          2. 15.2.2. Web Sites
          3. 15.2.3. Solutions
        3. 15.3. The Integrated Development Environment (IDE )
          1. 15.3.1. Layout
          2. 15.3.2. Creating a Web Application
          3. 15.3.3. Creating a Windows Application
          4. 15.3.4. Menus and Toolbars
          5. 15.3.5. File Menu
            1. 15.3.5.1. New
          6. 15.3.6. Edit Menu
            1. 15.3.6.1. Cycle Clipboard Ring Clipboard Ring
            2. 15.3.6.2. Find and Replace → Quick Find (Ctrl-F) / Quick Replace (Ctrl-H)
            3. 15.3.6.3. Find and Replace → Find in Files (Ctrl-Shift-F)
            4. 15.3.6.4. Find and Replace → Replace in Files (Ctrl-Shift-H)
            5. 15.3.6.5. Find and Replace → Find Symbol (Alt-F12)
            6. 15.3.6.6. Go To...
            7. 15.3.6.7. Find All References
            8. 15.3.6.8. Insert File As Text...
            9. 15.3.6.9. Advanced
            10. 15.3.6.10. Incremental search (Ctrl-I)
            11. 15.3.6.11. Bookmarks
            12. 15.3.6.12. Outlining
            13. 15.3.6.13. IntelliSense
          7. 15.3.7. View Menu
            1. 15.3.7.1. Class View (Ctrl-Shift-C)
            2. 15.3.7.2. Code Definition Window (Ctrl-W, D)
            3. 15.3.7.3. Error List (Ctrl-W, Ctrl-E)
            4. 15.3.7.4. Output (Ctrl-Alt-O)
            5. 15.3.7.5. Properties Windows (F4)
            6. 15.3.7.6. Solution Explorer (Ctrl-Alt-L)
            7. 15.3.7.7. Task List (Ctrl-W, Ctrl-T)
            8. 15.3.7.8. Toolbox (Ctrl-Alt-X)
            9. 15.3.7.9. Other Windows
          8. 15.3.8. Refactor Menu
          9. 15.3.9. Build Menu
          10. 15.3.10. Debug Menu
          11. 15.3.11. Data Menu
          12. 15.3.12. Format Menu
          13. 15.3.13. Tools Menu
            1. 15.3.13.1. Connect to Device ...
            2. 15.3.13.2. Connect to Database ...
            3. 15.3.13.3. Code Snippets Manager (Ctrl-K, Ctrl-B)
            4. 15.3.13.4. Choose Toolbox Items...
            5. 15.3.13.5. Macros
            6. 15.3.13.6. External Tools...
            7. 15.3.13.7. Performance Tools
            8. 15.3.13.8. Import and Export Settings...
            9. 15.3.13.9. Customize ...
            10. 15.3.13.10. Options...
          14. 15.3.14. Window Menu
          15. 15.3.15. Help Menu
            1. 15.3.15.1. Dynamic Help (Ctrl-Alt-F4)
            2. 15.3.15.2. Contents ...(Ctrl-Alt-F1) / Index ...(Ctrl-Alt-F2) / Search ...(Ctrl-Alt-F3)
            3. 15.3.15.3. Index Results... (Shift-Alt-F2)
            4. 15.3.15.4. Check for Updates
        4. 15.4. Building and Running
      2. 16. Visual Basic 2005 Fundamentals
        1. 16.1. Types
          1. 16.1.1. Numeric Types
            1. 16.1.1.1. Nonnumeric types
            2. 16.1.1.2. Understanding types
        2. 16.2. Variables
          1. 16.2.1.
            1. 16.2.1.1. Type characters
            2. 16.2.1.2. Initializing variables
          2. 16.2.2. Default Values
          3. 16.2.3. Constants
            1. 16.2.3.1. Literal constants
            2. 16.2.3.2. Symbolic constants
          4. 16.2.4. Enumerations
          5. 16.2.5. Casting
          6. 16.2.6. Strings
        3. 16.3. Whitespace
        4. 16.4. Statements
        5. 16.5. Branching
          1. 16.5.1. Unconditional Branching Statements
          2. 16.5.2. Conditional Branching Statements
            1. 16.5.2.1. If statements
            2. 16.5.2.2. If...Else statements
          3. 16.5.3. Select Case Statements
            1. 16.5.3.1. Checking for a range of values
        6. 16.6. Iteration Statements
          1. 16.6.1. The Do Loop
            1. 16.6.1.1. Do While
            2. 16.6.1.2. Do Until
            3. 16.6.1.3. Breaking out of a Do loop
          2. 16.6.2. While Loops
          3. 16.6.3. The For loop
        7. 16.7. Operators
          1. 16.7.1. Arithmetic Operators
            1. 16.7.1.1. The modulus operator (Mod) modulus operator (Mod)
            2. 16.7.1.2. The exponentiation (^) operator
          2. 16.7.2. Relational Operators
          3. 16.7.3. Use of Logical Operators with Conditionals
          4. 16.7.4. Operator Precedence
      3. 17. Using Collections and Generics
        1. 17.1. Arrays
          1. 17.1.1. Declaring Arrays
          2. 17.1.2. Understanding Default Values
          3. 17.1.3. Accessing Array Elements
          4. 17.1.4. Initializing Array Elements
          5. 17.1.5. Multidimensional Arrays
            1. 17.1.5.1. Rectangular arrays
          6. 17.1.6. Jagged Arrays
        2. 17.2. Generics
          1. 17.2.1. Generic List Class
          2. 17.2.2. Implementing IComparable
        3. 17.3. Queues
        4. 17.4. Stacks
        5. 17.5. Dictionaries
          1. 17.5.1. IDictionary IDictionary
      4. 18. Object-Oriented Visual Basic 2005
        1. 18.1. Defining Classes
        2. 18.2. Instantiating Objects
        3. 18.3. Scope
        4. 18.4. The Heap
        5. 18.5. Access Modifiers
        6. 18.6. Method Arguments
        7. 18.7. Constructors
        8. 18.8. Initializers
        9. 18.9. Copy Constructors
          1. 18.9.1. The Me Keyword
        10. 18.10. Using Shared Members
        11. 18.11. Destroying Objects
        12. 18.12. Overloading Methods and Constructors
          1. 18.12.1. Overloaded Methods—Different Signatures
        13. 18.13. Encapsulating Data with Properties
          1. 18.13.1. The Get Accessor
          2. 18.13.2. The Set Accessor
        14. 18.14. Specialization and Generalization
        15. 18.15. Inheritance
          1. 18.15.1. Implementing Inheritance
          2. 18.15.2. Calling Base Class Constructors
        16. 18.16. Polymorphism
          1. 18.16.1. Creating Polymorphic Types
          2. 18.16.2. Creating Polymorphic Methods
        17. 18.17. Abstract Classes
          1. 18.17.1. NotInheritable Class
        18. 18.18. The Root of All Classes: Object
        19. 18.19. Boxing and Unboxing Types
          1. 18.19.1. Boxing Is Implicit
          2. 18.19.2. Unboxing Must Be Explicit
        20. 18.20. Interfaces
        21. 18.21. Interfaces Versus Abstract Base Classes
        22. 18.22. Defining an Interface
        23. 18.23. Implementing an Interface
          1. 18.23.1. Implementing More Than One Interface
          2. 18.23.2. Casting to an Interface
    7. About the Author
    8. Colophon
    9. SPECIAL OFFER: Upgrade this ebook with O’Reilly