Pub. Date:
Visual Basic .NET and the .NET Platform: An Advanced Guide / Edition 1

Visual Basic .NET and the .NET Platform: An Advanced Guide / Edition 1

by Andrew Troelsen


Current price is , Original price is $59.95. You
Select a Purchase Option
  • purchase options

Product Details

ISBN-13: 9781893115262
Publisher: Apress
Publication date: 10/22/2001
Pages: 1053
Product dimensions: 7.00(w) x 9.25(h) x 2.24(d)

About the Author

Andrew Troelsen is a partner, trainer, and consultant at Intertech-Inc., and is a leading authority on both .NET and COM. His book Pro C# 2005 and the .NET 2.0 Platform won the prestigious 2003 Referenceware Excellence Award and is now in its third edition. Also of note are his earlier five-star treatment of traditional COM in the bestselling Developer's Workshop to COM and ATL mirrored in his book, COM and .NET Interoperability, and his top-notch investigation of VB .NET in Visual Basic .NET and the .NET Platform: An Advanced Guide. Troelsen has a degree in mathematical linguistics and South Asian studies from the University of Minnesota and is a frequent speaker at numerous .NET-related conferences. He currently lives in Minneapolis, Minnesota, with his wife, Amanda, and spends his free time investigating .NET and waiting for the Wild to win the Stanley Cup. You can check out his blog here: Troelsen's Tutorials

Read an Excerpt

Chapter 9: Building a Better Window (Introducing Windows Forms)

If you have read the previous eight chapters, you should have a solid handle on the VB .NET programming language as well as the core aspects of the .NET archi-tecture. While you could take your newfound knowledge and begin building the next generation of Console applications (boring!) you are more likely to be inter-ested in building an attractive graphical user interface (GUI) to allow the outside world to interact with your system.

This chapter introduces you to the System.Windows.Forms namespace. Here, you learn the details of building a highly stylized main window (using a custom Form-derived type). In the process, you learn about a number of window-related classes, including Application, MenuItem, ToolBar, and StatusBar. This chapter also examines how to capture and respond to user input (i.e., handling mouse and keyboard events) within the context of a GUI environment. As you will quickly discover, Windows Forms directly exposes much more code (and with that code, much more power) than VB 6.0 GUI development.

This chapter also illustrates a more exotic Windows Forms example that stores user preferences in the system registry (using .NET types) and interacts with the Windows 2000 Event Log. Finally, the chapter wraps up by examining the construction of a basic MDI application. The information presented here prepares you for the material presented in Chapters 10 and 11 (GDI+ and programming with Windows Forms controls). Once you complete these chapters, you are in a perfect position to build sophisticated user interfaces using the .NET Framework and VB .NET.

A Tale of Two GUI Namespaces

The .NET universe supplies two GUI toolkits: Windows Forms and Web Forms. The System.Windows.Forms namespace contains a number of types that allow you to build traditional desktop applications as well as feature-rich presentation layers (or "fat clients") for use in a distributed enterprise application. As you expect, Windows Forms (much like VB 6.0) hides the raw Win32 APIs from view, allowing you to focus on the functionality of your application using the familiar .NET-type system.

Web Forms, on the other hand, is a GUI toolkit used during ASP.NET development. The bulk of the Web Forms types are contained in the System.Web.UI and System.Web.UI.WebControls namespaces. Using these types, you are able to build browser-independent front ends based on various industry standards (HTML, HTTP, and so forth). You examine ASP.NET (as well as the related topic of Web services) in Chapters 15 and 16. This chapter focuses on building traditional desktop applications using the Windows Forms namespace.

As a relevant side note, it is worth pointing out that while Windows Forms and Web Forms contain a number of identically named types (e.g., Button and CheckBox) with similar members (e.g., BackColor and Text), they do not share a common implementation and cannot be treated identically. Nevertheless, as you become comfortable with the Windows Forms namespace, you should find the process of learning Web Forms far more palatable.

Overview of the Windows Forms Namespace

The System.Windows.Forms namespace contains a large number of types to aid in the process of building rich user interfaces. Like any namespace, System.Windows.Forms is composed of a number of classes, structures, interfaces, and enumerations. Over the next couple of chapters, you will drill into the specifics of a good number of these types. While it is redundant to list every member of the Windows Forms family (as they are all documented in online Help), Table 9-1 lists some (but by no means all) of the core classes.

Building a Windows Forms Application by Hand

When you are building a Windows Forms application, you may choose to write all the relevant code by hand (using Notepad, perhaps) and send the resulting *.vb file into the VB .NET compiler using the /target:winexe flag. Taking time to build some Windows Forms applications by hand is not only a great learning experience, but it also helps you understand the code generated by various GUI wizards provided by VS .NET.

Visual Studio .NET supplies a number of great wizards, starter templates, and configuration tools that make working with Windows Forms extremely simple. The only downside of wizard-generated code is that if you do not understand exactly what the code is doing on your behalf, you have a much tougher time fully understanding the underlying .NET types and the richness of the Windows Forms namespace. To ensure you do not become a victim of wizard code paralysis, write as much "wizard-free" code as possible and make use of the integrated tools where appropriate.

Prepping the Project Workspace

To begin understanding Windows Forms programming, let's build a simple main window by hand. While you could make use of vbc.exe directly, simply create a new, empty VB .NET project workspace named "MyRawWindow" using the VS. NET IDE. Next, insert a new VB .NET class definition (resist the temptation to insert a new Windows Form class) from the "Project | Add Class. . ." menu option (see Figure 9-1). Go ahead and name this class "MainWindow."

When you build a main window by hand, you need to use the Form and Application types (at a minimum), both of which are contained in the System.Windows.Forms.dll assembly. A Windows Forms application also needs to reference System.dll given that some Windows Forms types make use of types in the System.dll assembly. Add references to these assemblies now using the Add Reference dialog box (see Figure 9-2).

Building a Main Window

In the world of Windows Forms, the Form object is used to represent any window in your application. This includes a topmost main window in a Single Document Interface (SDI) application, modeless and modal dialog boxes, and the parent and child windows of a Multiple Document Interface (MDI) application. When you are interested in creating a new main window by hand, you must perform the following two steps:

1. Derive a new class from System.Windows.Forms.Form.

2. Configure the application's shared Main() method to call Application.Run(), passing an instance of your new Form-derived class as an argument.

With these steps in mind, update your empty class definition as follows:

' Need to import the Windows Forms namespace..
Imports System.Windows.Forms
Public Class MainWindow
Inherits Form ' Derive from Form..
Public Shared Sub Main()
' Launch the window!!
Application.Run(New MainWindow())
End Sub
End Class

Figure 9-3 shows a test run.

Now you have a minimizable, maximizable, resizable, and closable main window (with a default system-supplied icon to boot). Granted, your MainWindow does not do too much at this point. However, you build Forms with enhanced functionality as you move through the chapter.

SOURCE CODE You can find the MyRawWindow application under the Chap-ter 9 subdirectory.

Building a Visual Studio .NET Windows Forms Project Workspace

The benefit of building Windows Forms applications using Visual Studio .NET is that the integrated CASE tools can take care of a number of mundane coding details by delegating them to a number of wizards, configuration windows, and so forth. To illustrate how to make use of such assistance, close your current workspace. Now, select a new VB .NET Windows Application project type (see Figure 9-4).

When you click OK, you find that you are automatically given a new class derived from System.Windows.Forms.Form and have references set to each required assembly (as well as some additional assemblies). Notice, however, that you do not directly see a Main() method calling the shared Application.Run() method. This is because the initial Form in a Windows Application is con-figured (by default) to behave as the startup object (which can be confirmed by viewing the Project Properties window). Given that the main Form is the bootstrapper of the entire executable, Application.Run() is called automatically on your behalf.

Much like VB 6.0, Windows Forms applications offer a design-time template that you can use to assemble the user interface of your Form (see Figure 9-5). Understand that as you update this design-time template, you are indirectly adding code to the associated Form-derived class (named Form1.vb by default). This is quite a change from VB 6.0, where the changes you made to a GUI type with the properties window were never realized directly in the code window (although similar information was stored in the corresponding VB 6.0 *.frm file).

Using the Solution Explorer window, you are able to alternate between this design-time template and the underlying VB .NET code. To view the code that represents your current design, simply right-click the *.vb file and select View Code, as shown in Figure 9-6.

You can also open the code window by double-clicking anywhere on the design-time Form; however, this has the (possibly undesirable) effect of writing an event handler for the Form's Load event (more on GUI event processing later in this chapter). In any case, once you open the code window, you see a class looking very much like the following (note that the "Windows Form Designer generated code" region has been fully expanded)...

Table of Contents

Chapter 1: The Philosophy of .NET
Chapter 2: Building VB.NET Applications
Chapter 3: VB.NET Language Fundamentals
Chapter 4: Object Oriented Programming with VB.NET
Chapter 5: Interfaces and Collections
Chapter 6: Delegates, Events and Callback Interfaces
Chapter 7: Assemblies, AppDomains and Threads
Chapter 8: Type Reflection and Attribute-Based Programming
Chapter 9: Building a Better Window
Chapter 10: GDI+
Chapter 11: Intrinsic Windows Forms Controls
Chapter 12: Input, Output and Object Serialization
Chapter 13: Interacting with Legacy Systems
Chapter 14: ADO.NET
Chapter 15: Web Development and ASP.NET
Chapter 16: Building Web Services

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews