Understanding Windows Forms

Understanding Windows Forms

Windows Forms is a rich programming framework for building client applications that provides improved ease-of-use, tool support, and lower deployment costs. Traditional Windows desktop applications are often referred to as rich-client applications to differentiate them from browser-based Web applications that are downloaded from a central location. Classes used to create rich-client applications are found in the System.Windows.Forms namespace and are collectively known as the Windows Forms classes. This namespace includes the Form class, which is used as a base class for all dialog boxes and top-level windows in a .NET desktop application. In addition, the System.Windows.Forms namespace includes classes that manage controls, interaction with the clipboard, menus, printing, and more.

Using Forms as Dialog Boxes

A common way to use forms is as dialog boxes that interact with the user. Dialog boxes can range from basic forms that contain a simple text box control to elaborate forms with large numbers of controls. The .NET Framework includes a wide variety of controls that can be used with dialog boxes. Much of the discussion of Windows Forms in this book will focus on the controls available to you.

Some types of dialog boxes are used so often they’re included as part of the operating system. These dialog boxes, known collectively as the common dialog boxes, include prebuilt dialog boxes that handle tasks such as selecting files, specifying colors, choosing fonts, and configuring printer settings. The .NET Framework includes classes that provide access to these common dialog boxes; these classes will be covered throughout the next few chapters.

Dialog boxes are often used to display notification messages to the user. Instead of requiring each programmer to develop dialog boxes used for generic notification messages, the Windows operating system provides a message box control. The message box is a specialized dialog box that simplifies the task of displaying a simple text message to the user, using a variety of predefined icons and button layouts. The .NET Framework wraps the Windows message box in the MessageBox class. (The MessageBox class is discussed later in this chapter, in the section “Displaying Information with Message Boxes.”)

Using Forms as Views

A typical Windows Forms application is made up of more than just top-level windows and dialog boxes; Windows Forms applications can also include child windows that allow interaction between the user and the application. If you’re familiar with the Microsoft Foundation Classes (MFC) library included with Microsoft Visual C++, you’ll remember that this type of window is called a view and is derived from the CView class. In the .NET Framework, top-level windows, modal dialog boxes, and nonmodal application windows all share Form as their base class.

Forms used as views typically differ from dialog boxes in that they usually don’t have button controls that are used to dismiss the form. To use a form as a view, call its Show method, as shown here:

UserView userView = new UserView();
userView.Show(); 

When the view is no longer needed, use the Close method to dismiss the form:

userView.Close(); 

As you’ll see later in this chapter, in the section “Modal Forms vs. Modeless Forms,” the Show command is used to display a modeless dialog box. In most cases, a dialog box is modal, meaning that no work can be done with the rest of the application while the dialog box is displayed. Using a nonmodal form to interact with the user allows several forms to be open simultaneously, which can provide a richer experience for some types of applications.



Part III: Programming Windows Forms