Appendix J : Form Objects


This appendix describes the most useful properties, methods, and events provided by the Windows Form class.

The Form class inherits indirectly from the Control class (Control is the Form class’s “great-grandparent”), so in many ways, a form is just another type of control. Except where overridden, Form inherits the properties, methods, and events defined by the Control class. Chapter 2 discusses some of the more useful properties, methods, and events provided by the Control class and most of those apply to the Form class as well.

Properties

The following table describes some of the most useful Form properties.

Open table as spreadsheet

Property

Description

AcceptButton

Determines the button that clicks when the user presses the Enter key. This button basically gives the form a default action. Most forms used as dialog boxes should have an Accept button and a Cancel button (see the CancelButton property described shortly). This makes the form more accessible to the visually impaired and is more efficient for users who prefer to use the keyboard.

ActiveControl

Gets the form’s currently active control.

ActiveForm

Gets the application’s currently active form. If an MDI child form is active, this returns the active form’s MDI parent.

ActiveMdiChild

Gets the MDI parent form’s currently active MDI child form.

AllowDrop

Determines whether the form processes drag-and-drop events. See Chapter 13 for more information on drag-and-drop tasks.

Anchor

Determines which edges of the form are anchored to the edges of its container. This lets MDI child forms resize with their MDI parents.

AutoScroll

Determines whether the form automatically provides scroll bars when it is too small to display all of the controls it contains.

AutoScrollMargin

If AutoScroll is True, then the control will provide scroll bars if necessary to display its controls plus this much margin.

AutoScrollPosition

Adjusts the AutoScroll scroll bars so this point on the form is placed at the upper-left corner of the visible area (if possible). For example, if a button has location (100, 20), the statement AutoScrollPosition = New Point(100, 20) scrolls the form so the button is in the upper-left corner of the visible area.

BackColor

Determines the form’s background color.

BackgroundImage

Determines the image displayed in the form’s background.

BackgroundImageLayout

Determines how the BackgroundImage is displayed. This can be None (the image is displayed at up to normal scale, or compressed, if necessary, to make it fit vertically or horizontally), Tile (the image is tiled to fill the form), Center (the image is centered on the form at up to normal scale, or compressed, if necessary, to make it fit vertically or horizontally), Stretch (the image is resized to fill the form exactly), or Zoom (the image is resized to fill the form as much as possible without distorting it).

Bottom

Returns the distance between the form’s bottom edge and the top edge of its container.

Bounds

Determines the form’s size and location within its container. These bounds include the form’s client and non-client areas (such as the borders and caption area).

CancelButton

Determines the button that clicks when the user presses the Escape key. This button basically gives the form a cancel action. If the form is being displayed modally, clicking this button either manually or by pressing Escape automatically closes the form.

Capture

Determines whether the form has captured mouse events. While this is True, all mouse events go to the form’s event handlers. For example, pressing the mouse button sends the form a MouseDown event even if the mouse is over a control on the form or even if it is off of the form completely.

ClientRectangle

Returns a Rectangle object representing the form’s client area.

ClientSize

Gets or sets a Size object representing the client area’s size. If you set this value, the form automatically adjusts to make the client area this size while allowing room for its non-client areas (such as borders and title bar). For example, the following statement makes the form just big enough to display the txtNotes control within the client area.

 Me.ClientSize = New Size( _     lblNotes.Left + lblNotes.Width, _     lblNotes.Top + lblNotes.Height)

ContainsFocus

Returns True if the form or one of its controls has the input focus.

ContextMenuStrip

Gets or sets the form’s context menu. If the user right-clicks the form, Visual Basic automatically displays this menu. Note that controls on the form share this menu unless they have context menus of their own. Also note that some controls have their own context menus by default. For example, a TextBox displays a Copy, Cut, Paste menu, unless you explicitly set its ContextMenu property.

ControlBox

Determines whether the form displays a control box (the Minimize, Maxi mize, Restore, and Close buttons) on the right side of its caption area.

Controls

Returns a collection containing references to all the controls on the form. This includes only the controls contained directly within the form, and not controls contained within other controls. For example, if a form contains a GroupBox that holds several TextBox controls, then only the GroupBox is listed in the form’s Controls collection. You would need to search the GroupBox control’s Controls collection to find the TextBox controls.

Cursor

Determines the cursor displayed by the mouse when it is over the form.

DesktopBounds

Determines the form’s location and size as a Rectangle.

DesktopLocation

Determines the form’s location as a Point.

DialogResult

Gets or sets the form’s dialog box result. If code displays the form modally using its ShowDialog method, the method returns the DialogResult value the form has when it closes. Setting the form’s DialogResult value automatically closes the dialog box. Triggering the form’s CancelButton automatically sets DialogResult to Cancel and closes the dialog box.

DisplayRectangle

Gets a Rectangle representing the form’s display area. This is the area where you should display things on the form. In theory, this might not include all of the client area and could exclude form decorations, although in practice it seems to be the same as ClientRectangle.

Enabled

Determines whether the form will respond to user events. If the form is disabled, all of its controls are disabled and drawn grayed out. The user can still resize the form and its controls’ Anchor and Dock properties still rearrange the controls accordingly. The user can also click the form’s Minimize, Maximize, Restore, and Close buttons. Note that you cannot display a form modally using ShowDialog if it is disabled.

Font

Determines the form’s font.

ForeColor

Determines the foreground color defined for the form.

FormBorderStyle

Determines the form’s border style. This can be None, FixedSingle, Fixed3D, FixedDialog, Sizeable, FixedToolWindow, or SizeableToolWindow.

Handle

Returns the form’s integer window handle (hWnd). You can pass this value to API functions that work with window handles. Many of the API functions that are necessary in Visual Basic 6 are no longer needed in Visual Basic .NET because their functions have been incorporated into the .NET Framework, but there are still occasions when the form’s handle is useful.

HasChildren

Returns True if the form contains child controls.

Height

Determines the form’s height.

HelpButton

Determines whether the form displays a Help button with a question mark in the caption area to the left of the close button. The button is only visible if the MaximizeBox and MinimizeBox properties are both False. If the user clicks the Help button, the mouse pointer turns into a question mark arrow. When the user clicks on the form, Visual Basic raises the form’s HelpRequested event. The form can provide help based on the location of the click and, if it provides help, it should set the event handler’s hlpevent.Handled parameter to True.

Icon

Determines the form’s icon displayed in the left of the form’s caption area, in the taskbar, and by the Task Manager. Typically, this icon should contain images at the sizes 16 × 16 pixels and 32 × 32 pixels, so different displays can use an image with the correct size without resizing.

IsMdiChild

Returns True if the form is an MDI child form. To make an MDI application, set IsMdiContainer = True for the MDI parent form. Then display a child form, as shown in the following code. In the child form, IsMdiChild will return True.

 Dim child_form As New MyChildForm child_form.MdiParent = MdiParentForm child_form.Show

IsMdiContainer

Returns True if the form is an MDI parent form. See the description of IsMdiChild for more information.

KeyPreview

Determines whether the form receives key events before they are passed to the control with the input focus. If KeyPreview is True, the form’s key event handlers can see the key, take action, and hide the key from the control that would normally receive it, if necessary. For example, the following statement in a KeyDown event handler would close the form if the user presses Escape, no matter what control has the focus.

 If e.Keys = Keys.Escape Then Me.Close

Left

Determines the distance between the form’s left edge and the left edge of its container.

Location

Determines the coordinates of the form’s upper-left corner.

MainMenuStrip

Gets or sets the form’s main menu.

MaximizeBox

Determines whether the form displays a Maximize button on the right of its caption area.

MaximumSize

This Size object determines the maximum size the form can take.

MdiChildren

Returns an array of forms that are this form’s MDI children.

MdiParent

Gets or sets the form’s MDI parent form.

MinimizeBox

Determines whether the form displays a Minimize button on the right of its caption area.

MinimumSize

This Size object determines the minimum size the form can take.

Modal

Returns True if the form is displayed modally.

Name

Gets or sets the form’s name. Initially, this is the form’s class name, but your code can change it to anything, possibly even duplicating another form’s name.

Opacity

Determines the form’s opacity level between 0.0 (transparent) and 1.0 (opaque).

OwnedForms

Returns an array listing this form’s owned forms. To make this form own another form, call this form’s AddOwnedForm method, passing it the other form. Owned forms are minimized and restored with the owner and can never lie behind the owner. Typically, they are used for things like Toolboxes and search forms that should remain above the owner form.

Region

Gets or sets the region that defines the area that the form can occupy. Pieces of the form that lie outside of the region are clipped. For more information on regions, see Chapter 21.

Right

Returns the distance between the form’s right edge and the left edge of its container.

ShowIcon

Determines whether the form displays an icon in its title bar. If this is False, the system displays a default icon in the taskbar and Task Manager if ShowInTaskbar is True.

ShowInTaskbar

Determines whether the form is displayed in the taskbar and Task Manager.

Size

Gets or sets a Size object representing the form’s size, including client and non-client areas.

SizeGripStyle

Determines how the resize grip is shown in the form’ lower-right corner. This can be Show, Hide, or Auto.

StartPosition

Determines the form’s position when it is first displayed at runtime. This can be Manual (use the size and position specified by the form’s properties), CenterScreen (center the form on the screen taking the taskbar into account), WindowsDefaultLocation (use a default position defined by Windows and use the form’s specified size), and WindowsDefaultBounds (use Windows default position and size).

Tag

Gets or sets an object associated with the form. You can use this for whatever purpose you see fit.

Text

Determines the text displayed in the form’s caption.

Top

Determines the distance between the form’s top edge and the top edge of its container.

TopMost

Determines whether the form is a topmost form. A topmost form always sits above all other non-topmost forms, even when the other forms have the input focus.

TransparencyKey

Gets or sets a color that determines the areas of the form that are shown as transparent. This applies to the form itself and any controls it contains. For example, if you set TransparencyKey to the default form and control color Colors.Control, then the whole form and the bodies of many of its controls are invisible, so you will see text and borders floating above whatever forms lie behind.

UseWaitCursor

Determines whether the form is currently displaying the wait cursor.

Visible

Determines whether the form is visible. If the form is not visible, the user cannot interact with it. If you set Visible = False, the form’s icon is also removed from the taskbar and Task Manager.

Width

Determines the form’s width.

WindowState

Gets or sets the form’s state. This can be Normal, Minimized, or Maximized.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net