![]() ![]() | ||
Technically speaking, forms are what you work with in forms designers; they represent the windows that will appear in your application. However, it's become common to refer to both the windows under design and the windows in your running application as forms in Visual Basic applications.
The whole power of Visual Basic has been that you can develop forms visually, adding controls and other items from the toolbox. In VB .NET, the support for Windows forms is in the System.Windows.Forms namespace, and the form class is System.Windows.Forms.Form. The Form class itself is based on the Control class, which means that forms share a lot of the properties and methods that controls do. Here's what the class hierarchy looks like for the Form class; every level is derived from the one above it (note that all classes are derived from the Object class):
Object MarshalByRefObject Component Control ScrollableControl ContainerControl Form
You can see a form in a form designer in the Visual Basic Integrated Development Environment (IDE) in Figure 4.1, which shows several aspects of forms. At the top of the form is the title bar, which displays the form's title; here that's just Form1. At right in the title bar is the control box, including the minimizing/maximizing buttons and the close button. These are controls the user takes for granted in most windows, although we'll see that they are inappropriate in others, such as dialog boxes.
Under the title bar comes the menu bar, if there is one. In Figure 4.1, the form has one menu-the File menu. (We'll see how to work with menus in the next chapter). Under the menu bar, forms can have toolbars, as you see in the IDE itself.
The main area of a form-the area where everything takes place-is called the client area. In general, Visual Basic code works with controls in the client area and leaves the rest of the form to Visual Basic. (In fact, the client area is itself a window.) In Figure 4.1, I've added a control-a command button-to the form.
Finally, the whole form is surrounded by a border. There are several types of borders that you can use, as we'll see when working with dialog boxes and using the fixed, non-resizable borders appropriate to them.
The important class for Windows forms is the Form class in the System.Windows.Forms namespace. Each form in this namespace is an instance (that is, an object) of that class. As mentioned in Chapter 2, objects are instances of classes, much as an integer variable is an instance of the Integer type. (You can think of a class as a type you create objects from.) As we also know, classes can have members-fields (data items), methods (built-in procedures), and properties (data items accessed through an interface based on methods).
As I mentioned in Chapter 2, it's important to realize-now that we're actually starting to work with classes such as the Form class-that there are two kinds of class members. First, there are those members inherent to the class itself (accessed through the class), such as Form.ActiveForm, or just ActiveForm. For these members-called Static, Shared, or class members-you don't need an object. Then there are those members, called instance or object members, that are built into objects, such as MyForm1.BackColor. With this type, MyForm1 is an instance of the Form class, where you do need an object. In other words, the difference is that to use class members, you don't need an object of that class to work with, and with object members, you do:
Static/Shared members are class members, accessed directly using the class like this: classname.membername. No object needed.
Instance members are object members, accessed by using an instance of a class (an object) like this: objectname.membername.
I prefer the terms "class members" and "object members," because that makes clear what kinds of members they are, but the VB .NET documentation often uses the terms "Static (Shared) members" and "Instance members." With all that under our belts, we can talk about the members of the Form class. For more information on this topic, see "Class Vs Object Members" in the In Depth section of Chapter 11-in that chapter, we see how to create both class and object members from scratch.
The Form class only has one class property, ActiveForm, which holds the currently active form for the entire application. If you want to determine what window has the focus (that is, is the target of keystrokes), use the ActiveForm property. However, the Form class does have many object properties. Some of these object properties are public, some private to the object, and some protected (that is, only accessible to objects of the Form class or objects of classes derived from Form). When working with forms, one usually uses the public object members (that is, the public properties, methods, and events of Form objects); you'll find an overview of the most interesting Form public object properties in Table 4.1, and the most interesting Form public object methods (recall that methods are the procedures built into a class) in Table 4.2. Note that-as is usual with properties and methods in Visual Basic-not all these properties and methods will be available at the time you're designing your code-some only will be available at run time. It's worth scanning through these tables to familiarize yourself with what's available-such as the Icon property, which sets the icon for the form in Windows, or the BackColor property, which sets the background color of the form, and so on.
Method |
Description |
---|---|
Activate |
Activates the form (gives it focus and makes it active). |
AddOwnedForm |
Adds an owned form to this form. |
BringToFront |
Brings the form to the front of the stacking order. |
Close |
Closes the form. |
Contains |
Indicates if the specified control is a child of this form. |
Dispose |
Releases the resources used by the form. |
DoDragDrop |
Begins a drag-and-drop operation. |
Focus |
Gives the form the focus. |
GetChildAtPoint |
Gets the child control that is located at the specified coordinates. |
GetNextControl |
Gets the next control in the tab order of child controls. |
Hide |
Hides the form. |
LayoutMdi |
Arranges the MDI child forms within the MDI parent form. |
PointToClient |
Finds the location of the specified screen point to client coordinates. |
PointToScreen |
Finds the location of the specified client point to screen coordinates. |
RectangleToClient |
Finds the location of the specified screen rectangle to client coordinates. |
RectangleToScreen |
Finds the location of the specified client rectangle to screen coordinates. |
Refresh |
Forces the form to repaint (redraw) itself and any child controls. |
Select |
Selects this form. |
SendToBack |
Sends the form to the back of the stacking order. |
SetBounds |
Sets the bounds of the form. |
SetDesktopBounds |
Sets the bounds of the form in desktop coordinates. |
SetDesktopLocation |
Sets the location of the form in desktop coordinates. |
Show |
Makes the form display by setting the visible property to true. |
ShowDialog |
Displays the form as a modal dialog box. |
Windows forms also support events, which we've discussed as far back as Chapter 1. Events let you know that something's happened with a form; for example, when you click a form, a Click event occurs, and when the form is closed, a Closed event occurs. You'll find an overview of the more interesting public object events for Windows forms in Table 4.3.
![]() ![]() | ||