The following table describes some of the most useful Form methods.
Method | Description |
---|---|
Activate | Activates the form and gives it the focus. Normally, this pops the form to the top. Note that forcing a form to the top takes control of the desktop away from the user, so you should use this method sparingly. For example, if the user dismisses one form, you might activate the next form in a logical sequence. You should not activate a form to get the user’s attention every few minutes. |
AddOwnedForm | Adds an owned form to this 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. |
BringToFront | Brings the form to the top of the z-order. This applies only to other forms in the application. This form will pop to the top of other forms in this program, but not forms in other applications. |
Close | Closes the form. The program can still prevent the form from closing by catching the FormClosing event and setting e.Cancel to True. |
Contains | Returns True if a specified control is contained in the form. This includes controls inside GroupBox controls, Panel controls, and other containers, which are not listed in the form’s Controls collection. |
CreateGraphics | Creates a Graphics object that the program can use to draw on the form’s surface. For example, the following code draws a circle when the user presses a button:
Private Sub Button1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim gr As Graphics = Me.CreateGraphics() gr.FillEllipse(Brushes.Orange, 10, 10, 210, 220) gr.DrawEllipse(Pens.Red, 10, 10, 210, 220) End Sub Note that the Paint event handler provides a Graphics object in its e.Graphics parameter when the form needs to be redrawn. You should use that object rather than a new one returned by CreateGraphics while inside a Paint event handler. Otherwise, the Paint event handler’s version will draw over anything that you draw using the object returned by CreateGraphics. |
DoDragDrop | Begins a drag-and-drop operation. For more information on drag and drop, see Chapter 13. |
GetChildAtPoint | Returns a reference to the child control at a specific point. Note that the control is the outermost control at that point. For example, if a GroupBox contains a Button and you call GetChildAtPoint for a point above the Button, GetChildAtPoint returns the GroupBox. To find the Button, you would need to use the GroupBox control’s GetChildAtPoint method. Note also that the position of the Button within the GroupBox is relative to the GroupBox control’s origin, so you would need to subtract the GroupBox control’s position from the X and Y coordinates of the point relative to the form’s origin. |
GetNextControl | Returns the next control in the tab order. Parameters indicate the control to start from and whether the search should move forward or backward through the tab order. |
Hide | Hides the form. This sets the form’s Visible property to False. |
Invalidate | Invalidates some or all of the form’s area and generates a Paint event. |
LayoutMdi | If this form is an MDI parent form, arranges its MDI child forms. This method can take the parameters ArrangeIcons, Cascade, TileHorizontal, and TileVertical. Typically, this command is used in a menu titled Window. |
PointToClient | Converts a point from screen coordinates into the form’s coordinate system. |
PointToScreen | Converts a point from the form’s coordinate system into screen coordinates. |
RectangleToClient | Converts a rectangle from screen coordinates into the form’s coordinate system. |
RectangleToScreen | Converts a rectangle from the form’s coordinate system into screen coordinates. |
Refresh | Invalidates the form’s client area and forces it to redraw itself and its controls. |
RemoveOwnedForm | Removes an owned form from this form’s OwnedForms collection. |
ResetBackColor | Resets the form’s BackColor property to its default value (Control). This change is adopted by any controls on the form that do not have their BackColor properties explicitly set. |
ResetCursor | Resets the form’s Cursor property to its default value (Default). This change is adopted by any controls on the form that do not have their Cursor properties explicitly set. |
ResetFont | Resets the form’s Font property to its default value (8-point regular Microsoft Sans Serif). This change is adopted by any controls on the form that do not have their Font properties explicitly set. |
ResetForeColor | Resets the form’s ForeColor property to its default value (ControlText). This change is adopted by any controls on the form that do not have their ForeColor properties explicitly set. |
ResetText | Resets the form’s Text property to its default value (an empty string). |
Scale | Resizes the form and the controls it contains by a scale factor. A second overloaded version scales by different amounts in the X and Y directions. Note that this doesn’t change the controls’ font sizes, just their dimensions. |
ScrollControlIntoView | If the form has AutoScroll set to True, this scrolls to make the indicated control visible. |
SelectNextControl | Activates the next control in the tab order. Parameters indicate the control to start at, whether the search should move forward or backward through the tab order, whether the search should include only controls with TabStop set to True or all controls, whether to include controls nested inside other controls, and whether to wrap around to the first/last control if the search passes the last/first control. |
SendToBack | Sends the form to the back of the z-order. This puts the form behind all other forms in all applications, although it does not remove the focus from this form. |
SetAutoScrollMargin | If AutoScroll is True, this method sets the AutoScroll margin. The control will provide scroll bars if necessary to display its controls plus this much margin. |
SetBounds | Sets some or all of the form’s bounds: X, Y, Width, and Height. |
SetDesktopBounds | Sets the form’s position and size in desktop coordinates. See SetDesktopLocation for more information. |
SetDesktopLocation | Sets the form’s position in desktop coordinates. Desktop coordinates include only the screen’s working area and do not include the area occupied by the taskbar. For example, if the taskbar is attached to the left edge of the screen, then the point (0, 0) in screen coordinates is beneath the taskbar. However the point (0, 0) in desktop coordinates is just to the right of the taskbar. If you set the form’s location to (0, 0), part of the form is hidden by the taskbar. If you set the form’s desktop location to (0, 0), then the form is visible just to the right of the taskbar. |
Show | Displays the form. This has the same effect as setting the form’s Visible property to True. |
ShowDialog | Displays the form as a modal dialog box. The user cannot interact with other parts of the application before this form closes. Note that some other processes may still be running. For example, a Timer control on another form still raises Tick events and the program can still respond to them. |