Visual Basic 2005 with .NET 3.0 Programmer's Reference
Authors: Stephens R
Published year: 2007
The .NET Framework is a large collection of libraries that provide support for Visual Basic and the other .NET languages. It includes runtime libraries needed to run the code itself, plus classes for performing common important chores such as working with files, manipulating databases, and displaying controls on forms.
Visual Basic is inextricably tied to the .NET Framework and cannot run without it. Many of the tools that a Visual Basic program uses are actually provided by the .NET Framework. In fact, the two are so closely intertwined that it is often hard to tell which features are part of Visual Basic and which are provided by the Framework.
Recently, Microsoft released version 3.0 of the .NET Framework (formerly known as WinFX). This version of the Framework is clearly linked to the Vista operating system, released at roughly the same time. The Vista operating system uses windows and controls with translucent backgrounds similar to those provided by new controls in .NET Framework 3.0.
Vista is also a visually attractive operating system, clearly showing the work of skilled graphics designers. At the same time, the new Windows Presentation Foundation (WPF) tools in .NET Framework 3.0 are designed for use by graphics designers.
This revision updates the book Visual Basic 2005 Programmer’s Reference for Vista and .NET Framework 3.0. All of the example programs were tested in Vista and minor modifications were made as necessary to allow the programs to continue working in the new environment. The programs’ screen shots were retaken so that you can see the wide-bordered, translucent Aero look and feel of Vista. Completely new chapters and appendixes have also been added to cover key topics added by Windows Vista and version 3.0 of the .NET Framework. These include the following:
Chapter 14, “UAC Security”
Chapter 26, “Windows Presentation Foundation”
Chapter 30, “Windows Workflow Foundation”
Chapter 31, “Windows Communication Foundation”
Appendix H, “WPF Controls”
Appendix I, “Visual Basic Power Packs”
This book is intended for programmers of all levels. It describes the Visual Basic .NET language from scratch, so you don’t need experience with previous versions of the language. The book also covers many intermediate and advanced topics. It covers topics in enough depth that even experienced developers will discover new tips, tricks, and language details. After you have mastered the language, you may still find useful tidbits throughout the book, and the reference appendixes will help you look up easily forgotten details.
The chapters move quickly through the more introductory material. If you have never programmed before and are intimidated by computers, then you might want to read a more introductory book first. If you are a beginner who’s not afraid of the computer, then you should have few problems learning Visual Basic .NET from this book.
If you have programmed in any other language, then fundamentals such as variable declarations, data types, and arrays should be familiar to you, so you should have no problem with this book. The index and reference appendices should be particularly useful in helping you translate from the languages you already know into the corresponding Visual Basic syntax.
The chapters in this book are divided into four parts plus appendixes. The chapters in each part are described here. If you are an experienced programmer, you can use these descriptions to decide which chapters to skim and which to read in detail.
The chapters in this part of the book explain the basics of Visual Basic .NET programming. They describe the development environment, basic program syntax, and how to interact with standard controls. More advanced topics include how to build custom controls and how to implement drag and drop.
Chapter 1, “IDE,” describes the integrated development environment (IDE). It explains the IDE’s windows and how to customize the IDE. It also explains tools that provide help while you’re programming such features as the Object Browser and the code window’s IntelliSense.
Chapter 2, “Controls in General,” describes general Windows Forms control concepts. It explains how to add controls to a form, how to read and change a control’s properties at design time and at runtime, and how to use some of the more complicated control properties (such as Dock and Anchor ). This chapter shows how to catch and respond to events, and how to change event handlers in code.
Chapter 3, “Program and Module Structure,” analyzes a simple Visual Basic program and explains the structure created by Visual Studio. It describes the program’s code regions and comments, and tells how you can use similar techniques to make your code more readable and manageable.
Chapter 4, “Data Types, Variables, and Constants,” explains the standard data types provided by Visual Basic. It shows how to declare and initialize variables and constants, and explains variable scope. It discusses value and reference types, passing parameters by value or reference, and creating parameter variables on the fly. It also explains how to create arrays, enumerated types, and structures.
Chapter 5, “Operators,” describes the operators a program uses to perform calculations. These include mathematical operators ( + , * , \ ), string operators ( & ), and Boolean operators ( And , Or ). The chapter explains operator precedence and type conversion issues that arise when an expression combines more than one type of operator (for example, arithmetic and Boolean).
Chapter 6, “Subroutines and Functions,” explains how you can use subroutines and functions to break a program into manageable pieces. It describes routine overloading and scope.
Chapter 7, “Program Control Statements,” describes the statements that a Visual Basic program uses to control code execution. These include decision statements ( If Then Else , Select Case , IIF , Choose ) and looping statements ( For Next , For Each , Do While , While Do , Repeat Until ).
Chapter 8, “Error Handling,” explains error handling and debugging techniques. It describes the Try Catch structured error handler, in addition to the older On Error statement inherited from earlier versions of Visual Basic. It discusses typical actions a program might take when it catches an error. It also describes techniques for preventing errors and making errors more obvious when they do occur.
Chapter 9, “Introduction to Windows Forms Controls,” explains the standard Visual Basic controls that you can use on Windows forms. It describes the most useful properties, methods , and events provided by these Introduction controls, and it gives examples showing how to use them. It also describes cases where these controls rely on each other. For example, several controls such as the ToolBar obtain images from an associated ImageList control. Note that Windows Forms controls described in this chapter are different from the newer controls introduced by WPF and described in Chapter 26, “Windows Presentation Foundation.”
Chapter 10, “Forms,” explains typical uses of forms. It tells how to build partially transparent forms for use as splash, login, and About forms. It describes form cursors and icons, how to override WndProc to intercept a form’s Windows messages, how to make a Multiple Document Interface (MDI) application, and how to implement a Most Recently Used (MRU) file list. It does not cover all of the Form object’s properties, methods, and events in detail; those are described in Appendix J, “Form Objects.”
Chapter 11, “Database Controls and Objects,” explains how to use the standard Visual Basic database controls. These include database connection components that handle connections to a database, DataSet components that hold data within an application, and data adapter controls that move data between data connections and DataSets .
Chapter 12, “Custom Controls,” explains how to build your own customized controls that you can then use in other applications. It covers the three main methods for creating a custom control: derivation, composition, and building from scratch. This chapter also provides several examples that you can use as a starting point for controls of your own.
Chapter 13, “Drag and Drop, and the Clipboard,” explains how a Visual Basic program can support drag-and-drop operations. It tells how your program can start a drag to another application, how to respond to drag operations started by another application, and how to receive a drop from another application. This chapter also explains how a program can copy data to and from the clipboard. Using the clipboard is similar to certain types of drag-and-drop operations, so these topics fit naturally in one chapter.
Chapter 14, “UAC Security,” describes the new User Access security model used by the Vista operating system. With UAC security, all users run with reduced “normal” user privileges. If a program must perform tasks requiring administrator permissions, a UAC dialog box allows you to elevate the application’s privilege level. This chapter describes UAC security and explains how you can mark a program for privilege elevation.
This part explains fundamental concepts in object-oriented programming (OOP) with Visual Basic. It also describes some of the more important classes and objects that you can use when building an application.
Chapter 15, “OOP Concepts,” explains the fundamental ideas behind object-oriented programming (OOP). It describes the three main features of OOP: encapsulation, polymorphism, and inheritance. It explains the benefits of these features, and tells how you can take advantage of them in Visual Basic.
Chapter 16, “Classes and Structures,” explains how to declare and use classes and structures. It explains what classes and structures are, and it describes their differences. It shows the basic declaration syntax and tells how to create instances of classes and structures. It also explains some of the trickier class issues (such as private class scope, declaring events, and shared variables and methods).
Chapter 17, “Namespaces,” explains namespaces. It discusses how Visual Studio uses namespaces to categorize code and to prevent name collisions. It describes a project’s root namespace, tells how Visual Basic Introduction uses namespaces to resolve names (such as function and class names ), and demonstrates how you can add namespaces to an application yourself.
Chapter 18, “Collection Classes,” explains classes included in Visual Studio that you can use to hold groups of objects. It describes the various collection, dictionary, queue, and stack classes; tells how to make strongly typed versions of those classes; and gives some guidance on deciding which class to use under different circumstances.
Chapter 19, “Generics,” explains templates that you can use to build new classes designed to work with specific data types. For example, you can build a generic binary tree, and then later use it to build classes to represent binary trees of customer orders, employees , or work items.
The chapters in this part of the book describe graphics in Visual Basic .NET. They explain the Graphics Device Interface+ (GDI+) routines that programs use to draw images in Visual Basic. They explain how to draw lines and text; how to draw and fill circles and other shapes ; and how to load, manipulate, and save bitmap images. This part also explains how to generate printed output and how to send reports to the screen or to the printer.
Chapter 20, “Drawing Basics,” explains the fundamentals of drawing graphics in Visual Basic .NET. It describes the graphics namespaces and the classes they contain. It describes the most important of these classes, Graphics , in detail. It also describes the Paint event handler and other events that a program should use to keep its graphics up-to-date.
Chapter 21, “Brushes, Pens, and Paths,” explains the most important graphics classes after Graphics : Pen and Brush . It tells how you can use Pen s to draw solid lines, dashed lines, lines with custom dash patterns, and lines with custom lengthwise stripe patterns. It tells how to use Brushes to fill areas with colors, hatch patterns, linear color gradients, color gradients that follow a path , and tiled images. This chapter also describes the GraphicsPath class, which represents a series of lines, shapes, curves, and text.
Chapter 22, “Text,” explains how to draw strings of text. It shows how to create different kinds of fonts, determine exactly how big text will be when drawn in a particular font, and use GDI+ functions to make positioning text simple. It shows how to use a StringFormat object to determine how text is aligned, wrapped, and trimmed , and how to read and define tab stops.
Chapter 23, “Image Processing,” explains how to load, modify, and save image files. It shows how to read and write the pixels in an image, and how to save the result in different file formats such as BMP, GIF, and JPEG. It tells how to use images to provide auto- redraw features, and how to manipulate an image pixel by pixel, both using a Bitmap ’s GetPixel and SetPixel methods and using “unsafe” access techniques that make pixel manipulation much faster than is possible with normal GDI+ methods.
Chapter 24, “Printing,” explains different ways that a program can send output to the printer. It shows how you can use the PrintDocument object to generate printout data. You can then use the PrintDocument to print the data immediately, use a PrintDialog control to let the user select the printer and set its characteristics, or use a PrintPreviewDialog control to let the user preview the results before printing.
Chapter 25, “Reporting,” provides an introduction to Crystal Reports, a tool that makes generating reports in Visual Basic relatively easy. The chapter explains the basics of Crystal Reports and steps through an example that builds a simple report.
Chapter 26, “Windows Presentation Foundation ,” describes one of the biggest innovations provided by version 3.0 of the .NET Framework. Windows Presentation Foundation (WPF) includes a completely new set of controls that provide remarkable new features. This chapter describes some of these controls and explains how you can define an interface by using the extensible application markup language (XAML, pronounced “zammel”) that allows you to define a user interface. It also shows how you can use XAML to define complex appearances and behaviors for these new controls.
The chapters in this part of the book explain how an application can interact with its environment. They show how the program can save and load data in external sources (such as the System Registry, resource files, and text files); work with the computer’s screen, keyboard, and mouse; and interact with the user through standard dialog controls.
Chapter 27, “Configuration and Resources,” describes some of the ways that a Visual Basic program can store configuration and resource values for use at runtime. Some of the most useful of these include environment variables, the Registry, configuration files, and resource files.
Chapter 28, “Streams,” explains the classes that a Visual Basic application can use to work with stream data. Some of these classes are FileStream , MemoryStream , BufferedStream , TextReader , and TextWriter .
Chapter 29, “File-System Objects,” describes classes that let a Visual Basic application interact with the file system. These include classes such as Directory , DirectoryInfo , File , and FileInfo that make it easy to create, examine, move, rename, and delete directories and files.
Chapter 30, “Windows Workflow Foundation,” describes one of the major new tools provided by the .NET Framework 3.0. Windows Workflow Foundation (WF) includes a workflow runtime engine and an assortment of classes for representing workflows. This chapter explains how you can define a workflow by using objects, and then let the runtime engine move a job through the flow until it reaches an end state.
Chapter 31, “Windows Communication Foundation,” describes another of the new tools included in the .NET Framework 3.0. Windows Communication Foundation (WCF) is a library and set of tools that make building service-oriented applications easier. This chapter explains how to use new WCF attributes to easily define a service, how to use configuration files to configure the service, and how to use WCF tools to consume the service.
Chapter 32, “Useful Namespaces,” describes some of the most commonly useful namespaces defined by the .NET Framework. It provides a brief overview of some of the most important System namespaces and gives more detailed examples that demonstrate regular expressions, XML, cryptography, reflection, threading, and Direct3D.
The book’s appendixes provide a categorized reference of the Visual Basic .NET language. You can use them to quickly review the syntax of a particular command, select from among several overloaded versions of a routine, or refresh your memory of what a particular class can do. The chapters earlier in the book give more context, explaining how to perform specific tasks and why one approach might be preferred over another.
Appendix A, “Useful Control Properties, Methods, and Events,” describes properties, methods, and events that are useful with many different kinds of controls.
Appendix B, “Variable Declarations and Data Types,” summarizes the syntax for declaring variables. It also gives the sizes and ranges of allowed values for the fundamental data types.
Appendix C, “Operators,” summarizes the standard operators such as + , << , OrElse , and Like . It also gives the syntax for operator overloading.
Appendix D, “Subroutine and Function Declarations,” summarizes the syntax for subroutine, function, and property procedure declarations.
Appendix E, “Control Statements,” summarizes statements that control program flow, such as If Then , Select Case , and looping statements.
Appendix F, “Error Handling,” summarizes both structured and “classic” error handling. It describes some useful exception classes and gives an example showing how to build a custom exception class.
Appendix G, “Standard Controls and Components,” describes standard Windows Forms controls and components provided by Visual Basic .NET. It explains the properties, methods, and events that I have found most useful when working with these components.
Appendix H, “WPF Controls,” describes the controls that are placed in the toolbox by default when you build WPF applications. This is a relatively brief list to give you a feel for the kinds of controls that are available, rather than an in-depth description like the one provided for the standard Windows Forms controls in Appendix G.
Appendix I, “Visual Basic Power Packs,” lists some additional tools that you can download to make Visual Basic development easier. This appendix describes some Visual Basic 6 compatibility tools provided by Microsoft, and some controls built in Visual Basic 2003 that are available on the GotDotNet web site.
Appendix J, “Form Objects,” describes forms. In a very real sense, forms are just another type of component. They play such a key role in Visual Basic applications, however, that they deserve special attention in their own appendix.
Appendix K, “Classes and Structures,” summarizes the syntax for declaring classes and structures, and defining their constructors and events.
Appendix L, “Generics,” summarizes the syntax for declaring generic classes.
Appendix M, “Graphics,” summarizes the objects used to generate graphics in Visual Basic .NET. It covers the most useful graphics namespaces.
Appendix N, “Useful Exception Classes,” lists some of the more useful exception classes defined by Visual Basic. You may want to throw these exceptions in your own code.
Appendix O, “Date and Time Format Specifiers,” summarizes specifier characters that you can use to format dates and times. For example, they let you display a time using a 12-hour or 24- hour clock. Introduction
Appendix P, “Other Format Specifiers,” summarizes formatting for numbers and enumerated types.
Appendix Q, “The Application Class,” summarizes the Application class that provides properties and methods for controlling the current application.
Appendix R, “The My Namespace,” describes the My namespace, which provides shortcuts to useful features scattered around other parts of the .NET Framework. It provides shortcuts for working with the application, computer hardware, application forms, resources, and the current user.
Appendix S, “Streams,” summarizes the Visual Basic stream classes such as Stream , FileStream , MemoryStream , TextReader , CryptoStream , and so forth.
Appendix T, “File-System Classes,” summarizes methods that an application can use to learn about and manipulate the file system. It explains classic Visual Basic methods such as FreeFile , WriteLine , and ChDir , as well as newer .NET Framework classes such as FileSystem , Directory , and File .
Visual Basic 2005 with .NET 3.0 Programmer's Reference
Authors: Stephens R
Published year: 2007