Contents

Acknowledgments

Introduction

Part I: IDE

Chapter 1: Introduction to the IDE

Different IDE Appearance

IDE Configurations

Projects and Solutions

Starting the IDE

Creating a Project

Saving a Project

Summary

Chapter 2: Menus, Toolbars, and Windows

Menus

Toolbars

Secondary Windows

Summary

Chapter 3: Customization

Adding Commands

Removing Commands

Modifying Commands

Making Keyboard Shortcuts

Summary

Chapter 4: Windows Form Designer

Setting Designer Options

Adding Controls

Selecting Controls

Copying Controls

Moving and Sizing Controls

Arranging Controls

Setting Properties

Adding Code to Controls

Summary

Chapter 5: WPF Designer

Early Version Warning

Recognizing Designer Windows

Adding Controls

Selecting Controls

Copying Controls

Moving and Sizing Controls

Setting Properties

Setting Group Properties

Adding Code to Controls

Summary

Chapter 6: Visual Basic Code Editor

Margin Icons

Outlining

Tooltips

IntelliSense

Code Coloring and Highlighting

Code Snippets

The Code Editor at Runtime

Summary

Chapter 7: Debugging

The Debug Menu

The Debug ⇒ Windows Submenu

The Breakpoints Window

The Command and Immediate Windows

Summary

Part II: Getting Started

Chapter 8: Selecting Windows Forms Controls

Controls Overview

Choosing Controls

Third-Party Controls

Summary

Chapter 9: Using Windows Forms Controls

Controls and Components

Creating Controls

Properties

Methods

Events

Summary

Chapter 10: Windows Forms

Transparency

About, Splash, and Login Forms

Mouse Cursors

Icons

Properties Adopted by Child Controls

Property Reset Methods

Overriding WndProc

SDI and MDI

MRU Lists

Dialog Boxes

Wizards

Summary

Chapter 11: Selecting WPF Controls

Controls Overview

Containing and Arranging Controls

Making Selections

Entering Data

Displaying Data

Providing Feedback

Initiating Action

Presenting Graphics and Media

Providing Navigation

Managing Documents

Digital Ink

Summary

Chapter 12: Using WPF Controls

WPF Concepts

WPF in the IDE

XAML Features

Procedural WPF

Documents

Summary

Chapter 13: WPF Windows

Window Applications

Page Applications

Summary

Chapter 14: Program and Module Structure

Hidden Files

Code File Structure

Typographic Code Elements

Summary

Chapter 15: Data Types, Variables, and Constants

Data Types

Type Characters

Data Type Conversion

Variable Declarations

Option Explicit and Option Strict

Scope

Parameter Declarations

Property Procedures

Enumerated Data Types

Anonymous Types

Nullable Types

Constants

Delegates

Naming Conventions

Summary

Chapter 16: Operators

Arithmetic Operators

Concatenation Operators

Comparison Operators

Logical Operators

Bitwise Operators

Operator Precedence

Assignment Operators

The StringBuilder Class

Date and TimeSpan Operations

Operator Overloading

Operators with Nullable Types

Summary

Chapter 17: Subroutines and Functions

Subroutines

Functions

Property Procedures

Extension Methods

Lambda Functions

Relaxed Delegates

Partial Methods

Summary

Chapter 18: Program Control Statements

Decision Statements

Looping Statements

GoTo

Summary

Chapter 19: Error Handling

Bugs versus Unplanned Conditions

Structured Error Handling

Visual Basic Classic Error Handling

Structured versus Classic Error Handling

The Err Object

Debugging

Summary

Chapter 20: Database Controls and Objects

Automatically Connecting to Data

Automatically Created Objects

Other Data Objects

Data Overview

Connection Objects

Transaction Objects

Data Adapters

Command Objects

DataView

DataRowView

Simple Data Binding

CurrencyManager

Complex Data Binding

Summary

Chapter 21: LINQ

Introduction to LINQ

Basic LINQ Query Syntax

Advanced LINQ Query Syntax

LINQ Functions

LINQ Extension Methods

LINQ to Objects

LINQ to XML

LINQ to ADO.NET

Summary

Chapter 22: Custom Controls

Custom Controls in General

Derived Controls

Composite Controls

Controls Built from Scratch

Components

Invisible Controls

Picking a Control Class

Controls and Components in Executable Projects

Custom Component Security

Summary

Chapter 23: Drag and Drop, and the Clipboard

Drag-and-Drop Events

Using the Clipboard

Summary

Chapter 24: UAC Security

UAC Overview

Designing for UAC

Elevating Programs

Summary

Part III: Object-Oriented Programming

Chapter 25: OOP Concepts

Classes

Encapsulation

Inheritance

Polymorphism

Overloading

Extension Methods

Summary

Chapter 26: Classes and Structures

Classes

Structures

Class Instantiation Details

Structure Instantiation Details

Garbage Collection

Constants, Properties, and Methods

Events

Summary

Chapter 27: Namespaces

The Imports Statement

The Root Namespace

Making Namespaces

Classes, Structures, and Modules

Resolving Namespaces

Summary

Chapter 28: Collection Classes

What Is a Collection?

Arrays

Collections

Dictionaries

CollectionsUtil

Stacks and Queues

Generics

Summary

Chapter 29: Generics

Advantages of Generics

Defining Generics

Using Generics

Predefined Generic Classes

Generic Methods

Generics and Extension Methods

Summary

Part IV: Graphics

Chapter 30: Drawing Basics

Drawing Overview

Drawing Namespaces

Graphics

Drawing Events

Summary

Chapter 31: Brushes, Pens, and Paths

Pen

Brush

GraphicsPath Objects

Garbage-Collection Issues

Summary

Chapter 32: Text

Drawing Text

Text Formatting

MeasureString

Font Metrics

Summary

Chapter 33: Image processing

Image

Bitmap

Metafile Objects

Summary

Chapter 34: Printing

How Not to Print

Basic Printing

Printing Text

Centering Printouts

Fitting Pictures to the Page

Simplifying Drawing and Printing

Summary

Chapter 35: Reporting

Report Objects

Building a Report

CrystalReportViewer

Customizing a Report

External Reports

ReportDocument

Summary

Part V: Interacting with the Environment

Chapter 36: Configuration and Resources

My

Environment

Registry

Configuration Files

Resource Files

Application

Summary

Chapter 37: Streams

Stream

FileStream

MemoryStream

BufferedStream

BinaryReader and BinaryWriter

TextReader and TextWriter

StringReader and StringWriter

StreamReader and StreamWriter

OpenText, CreateText, and AppendText

Custom Stream Classes

Summary

Chapter 38: File-System Objects

Permissions

Visual Basic Methods

.NET Framework Classes

My.Computer.FileSystem

My.Computer.FileSystem.SpecialDirectories

Summary

Chapter 39: Windows Communication Foundation

Early Version Warning

WCF Concepts

WCF Example

Building the Initial Service

Building QuoteService

Testing QuoteService

Building QuoteClient

Hosting the Service

Summary

Chapter 40: Useful Namespaces

High-Level Namespaces

Advanced Examples

Summary

Part VI: Appendices

Appendix A: Useful Control Properties, Methods, and Events

Appendix B: Variable Declarations and Data Types

Appendix C: Operators

Appendix D: Subroutine and Function Declarations

Appendix E: Control Statements

Appendix F: Error Handling

Appendix G: Windows Forms Controls and Components

Appendix H: WPF Controls

Appendix I: Visual Basic Power Packs

Appendix J: Form Objects

Appendix K: Classes and Structures

Appendix L: LINQ

Appendix M: Generics

Appendix N: Graphics

Appendix O: Useful Exception Classes

Appendix P: Date and Time Format Specifiers

Appendix Q: Other Format Specifiers

Appendix R: The Application Class

Appendix S: The My Namespace

Appendix T: Streams

Appendix U: File-System Classes

Appendix V: Index of Examples

Index

Visual Basic® 2008 Programmer’s Reference

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-18262-8

Library of Congress Cataloging-in-Publication Data is available from Publisher.

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 http://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 warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created 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. Visual Basic is a registered trademark 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.

About the Author

Rod Stephens started out as a mathematician, but while studying at MIT, discovered the joys of programming and has been programming professionally ever since. During his career, he has worked on an eclectic assortment of applications in such fields as telephone switching, billing, repair dispatching, tax processing, wastewater treatment, concert ticket sales, cartography, and training for professional football players.

Rod is a Microsoft Visual Basic Most Valuable Professional (MVP) and ITT adjunct instructor. He has written 18 books that have been translated into half a dozen different languages, and more than 200 magazine articles covering Visual Basic, Visual Basic for Applications, Delphi, and Java. He is currently a regular contributor to DevX (www.DevX.com).

Rod’s popular VB Helper web site www.vb-helper.com receives several million hits per month and contains thousands of pages of tips, tricks, and example code for Visual Basic programmers, as well as example code for this book.

Credits

Executive Editor

Robert Elliott

Development Editor

Christopher J. Rivera

Technical Editor

John Mueller

Production Editor

Angela Smith

Copy Editor

Kim Cofer

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

Nancy Hanger, Windhaven

Indexer

J & J Indexing

Acknowledgments

Thanks to Bob Elliott, Christopher Rivera, Angela Smith, and all of the others who work so hard to make producing any book possible.

Thanks also to technical editor John Mueller for adding extra depth and perspective to the book. Visit www.mwt.net/~jmueller to learn about John’s books and to sign up for his free newsletter .NET Tips, Trends & Technology eXTRA.

Introduction

It has been said the Sir Isaac Newton was the last person to know everything. He was an accomplished physicist (his three laws of motion were the basis of classical mechanics, which defined astrophysics for three centuries), mathematician (he was one of the inventors of calculus and developed Newton’s Method for finding roots of equations), astronomer, natural philosopher, and alchemist. He invented the reflecting telescope, a theory of color, a law of cooling, and studied the speed of sound.

Just as important, he was born before relativity, quantum mechanics, gene sequencing, thermodynamics, parallel computation, and a swarm of other extremely difficult branches of science.

If you ever used Visual Basic 3, you too could have known everything. Visual Basic 3 was a reasonably small but powerful language. Visual Basic 4 added classes to the language and made Visual Basic much more complicated. Versions 4, 5, and 6 added more support for database programming and other topics such as custom controls, but Visual Basic was still a fairly understandable language, and if you took the time you could become an expert in just about all of it.

Visual Basic .NET accelerated the expansion of Visual Basic tremendously. The .NET Framework added powerful new tools to Visual Basic, but those tools came at the cost of increased complexity. Associated technologies have been added to the language at an ever-increasing rate, so, today, it is impossible for anyone to be an expert on every topic that deals with Visual Basic.

To cover every nook and cranny in Visual Basic you would need an in-depth understanding of database technologies, custom controls, custom property editors, XML, cryptography, serialization, two- and three-dimensional graphics, multi-threading, reflection, the code document object model (DOM), diagnostics, globalization, Web Services, inter-process communication, work flow, Office, ASP, and much more.

This book doesn’t even attempt to cover all of these topics. Instead, it provides a broad, solid understanding of essential Visual Basic topics. It explains the powerful development environment that makes Visual Basic such a productive language. It describes the Visual Basic language itself and explains how to use it to perform a host of important development tasks.

It also explains the forms, controls, and other objects that Visual Basic provides for building applications in a modern windowing environment.

This book may not cover every possible topic related to Visual Basic, but it does cover the majority of the technologies that developers need to build sophisticated applications.

Should You Use Visual Basic 2008?

Software engineers talk about five generations of languages (so far). A first-generation language (1GL) is machine language: 0s and 1s.

A second-generation language (2GL) is an assembly language that provides terse mnemonics for machine instructions. It provides few additional tools beyond an easier way to write machine code.

Third-generation languages (3GLs) are higher-level languages such as Pascal and FORTRAN. They provide much more sophisticated language elements such as subroutines, loops, and data structures.

Fourth-generation languages (4GLs) are “natural languages” such as SQL. They let developers use a language that is sort of similar to a human language to execute programming tasks. For example, the SQL statement “SELECT * FROM Customers WHERE Balance > 50” tells the database to return information about customers that owe more than $50.

Fifth-generation languages (5GLs) provide powerful, highly graphical development environments to allow developers to use the underlying language in more sophisticated ways. The Visual Studio development environment is extremely powerful. It provides graphical editors to make building forms and editing properties easy and intuitive; IntelliSense to help developers remember what to type next; auto-completion so developers can use meaningful variable names without needing to waste time typing them completely by hand; and breakpoints, watches, and other advanced debugging tools that make building applications easier.

Visual Basic uses one of the most powerful development environments ever built, Visual Studio, but it is not the only language that does so. The C# language also uses the Visual Studio development environment. So the question is, should you use Visual Basic or C#?

A Visual Basic programmer’s joke asks, “What’s the difference between Visual Basic .NET and C#? About three months!” The implication is that Visual Basic .NET syntax is easier to understand, and building applications with it is faster. Similarly, C# programmers have their jokes about Visual Basic .NET, implying that C# is more powerful.

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!