JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

All About Windows Forms

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.

Click To expand
Figure 4.1: A form under design.

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.

Table 4.1: Windows forms public object properties.

Property

Description

AcceptButton

Gets or sets the button on the form that is pressed when the user uses the Enter key.

ActiveControl

Gets or sets the active control (the one with the focus).

ActiveMdiChild

Gets the currently active multiple document interface (MDI) child window.

AllowDrop

Indicates if the form can accept data that the user drags and drops into it.

AutoScale

Indicates if the form adjusts its size to fit the height of the font used on the form and scales its controls.

AutoScroll

Indicates if the form implements autoscrolling.

BackColor

Gets or sets the background color for this form.

BackgroundImage

Gets or sets the background image in the form.

Bottom

Gets the location of the bottom of the form.

Bounds

Gets or sets the bounding rectangle for the form.

CancelButton

Indicates the button control that is pressed when the user presses the ESC key.

CanFocus

Indicates if the form can receive focus.

Capture

Specifies if the form has captured the mouse, or captures the mouse.

ClientRectangle

Returns the rectangle that represents the client area.

ClientSize

Gets or sets the size of the client area.

ContainsFocus

Indicates if the form, or a child control, has the input focus.

ContextMenu

Gets or sets the shortcut menu for this form.

ControlBox

Gets or sets a value indicating if a control box is displayed.

Controls

Gets or sets the collection of controls contained within the form.

Cursor

Gets or sets the cursor that is displayed when the user moves the mouse pointer over this form.

DesktopBounds

Gets or sets the size and location of the form on the Windows desktop.

DesktopLocation

Gets or sets the location of the form on the Windows desktop.

DialogResult

Gets or sets the dialog result for the form.

Enabled

Gets or sets a value indicating if the form is enabled.

Focused

Indicates if the form has input focus.

ForeColor

Gets or sets the foreground color of the form.

FormBorderStyle

Gets or sets the border style of the form.

Height

Gets or sets the height of the form.

Icon

Gets or sets the icon for the form.

IsMdiChild

Indicates if the form is an MDI child form.

IsMdiContainer

Gets or sets a value indicating if the form is a container for MDI child forms.

Left

Gets or sets the x-coordinate of a form's left edge in pixels.

Location

Gets or sets the coordinates of the upper-left corner of the form relative to the upper-left corner of its container.

MaximizeBox

Gets or sets a value indicating if the maximize button is displayed in the caption bar of the form.

MaximumSize

Returns the maximum size the form can be resized to.

MdiChildren

Returns an array of forms of the MDI child forms that are parented to this form.

MdiParent

Gets or sets the current MDI parent form of this form.

Menu

Gets or sets the MainMenu that is displayed in the form.

MinimizeBox

Gets or sets a value indicating if the minimize button is displayed in the caption bar of the form.

MinimumSize

Gets the minimum size the form can be resized to.

Modal

Gets a value indicating if this form is displayed modally.

Name

Gets or sets the name of the form.

OwnedForms

Gets an array of Form objects of all forms that are owned by this form.

Owner

Gets or sets the form that owns this form.

Parent

Gets or sets the parent container of this form.

ParentForm

Gets or sets the form that the container form is assigned to.

Right

Gets the distance between the right edge of the form and the left edge of its container.

ShowInTaskbar

Gets or sets a value indicating if the form is displayed in the Windows taskbar.

Size

Gets or sets the size of the form.

StartPosition

Gets or sets the starting position of the form at run time.

TabStop

Gets or sets a value indicating if the user can give the focus to this form using the Tab key.

Tag

Gets or sets the object that contains data about the form.

Text

Gets or sets the text associated with this form.

Top

Gets or sets the top coordinate of the form.

TopLevel

Gets or sets a value indicating if the form should be displayed as a top-level window.

TopMost

Gets or sets a value indicating if the form should be displayed as the topmost form of your application.

Visible

Gets or sets a value indicating if the form is visible.

Width

Gets or sets the width of the form.

WindowState

Gets or sets the form's window state.

Table 4.2: Windows forms public object methods.

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.

Table 4.3: Windows forms public object events

Event

Description

Activated

Occurs when the form is activated in code or by the user.

Click

Occurs when the form is clicked.

Closed

Occurs when the form is closed.

Closing

Occurs when the form is closing.

ControlAdded

Occurs when a new control is added.

ControlRemoved

Occurs when a control is removed.

CursorChanged

Occurs when the cursor property value has changed.

Deactivate

Occurs when the form loses focus and is not the active form.

DoubleClick

Occurs when the form is double-clicked.

DragDrop

Occurs when a drag-and-drop operation is completed.

DragEnter

Occurs when an object is dragged into the form's bounds.

DragLeave

Occurs when an object has been dragged into and out of the form's bounds.

DragOver

Occurs when an object has been dragged over the form's bounds.

Enter

Occurs when the form is entered.

ForeColorChanged

Occurs when the ForeColor property value has changed.

GotFocus

Occurs when the form receives focus.

KeyDown

Occurs when a key is pressed down while the form has focus.

KeyPress

Occurs when a key is pressed while the form has focus.

KeyUp

Occurs when a key is released while the form has focus.

Layout

Occurs when a form has to lay out its child controls.

Load

Occurs before a form is displayed for the first time.

LocationChanged

Occurs when the Location property value has changed.

LostFocus

Occurs when the form loses focus.

MdiChildActivate

Occurs when an MDI child form is activated or closed within an MDI application.

MouseDown

Occurs when the mouse pointer is over the form and a mouse button is pressed.

MouseEnter

Occurs when the mouse pointer enters the form.

MouseHover

Occurs when the mouse pointer hovers over the form.

MouseLeave

Occurs when the mouse pointer leaves the form.

MouseMove

Occurs when the mouse pointer is moved over the form.

MouseUp

Occurs when the mouse pointer is over the form and a mouse button is released.

MouseWheel

Occurs when the mouse wheel moves while the form has focus.

Move

Occurs when the form is moved.

Paint

Occurs when the form is redrawn.

Resize

Occurs when the form is resized.

SizeChanged

Occurs when the Size property value has changed.

TextChanged

Occurs when the Text property value has changed.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor