Home > Language Reference > Classes

Form Class

  + Object
    + Display
      + UIComponent
        + Form

Description

The Form class implements behaviors common to all forms (see Forms and controls). The form is the main element of an user interface. An HB++ program cannot draw or display anything without having first opened a form. This class is not instantiable nor cloneable; its features are only available through derivation.

Adding a form to your project by selecting 'Project > Add Form' creates both a new resource in your executable file, and a new class deriving from Form. The resource will describe the appearence of the form, as defined in the form editor (type and location of each controls, properties, and so on). The events your code handles in the class will define the behavior of the form (what happens if the user taps a given control, for example).

Opening and closing a form

To display a form at runtime, simply declare and instantiate an object whose type corresponds to the form you wish to open, and call its Show method. For example, a typical application startup code is:

Private Sub Application_NormalLaunch()
  Dim f As New frmMain
  f.Show hbFormModeless+hbFormGoto
End Sub

A form can be either modeless or modal, depending on the value of the parameter passed to the Show method. When opening a modeless form, the Show method returns immediately after the form is displayed. This behavior is typically used when opening background forms, such as your main application form. When opening a modal form, the Show method does not return until the form is closed by calling the Unload function. This behavior is typically used to implement dialog boxes. Note that these behaviors are not related to the Modal property, which only defines the appearence of the form border and caption.

Each time a form is loaded, all form and controls properties are reset to their design time value. Therefore, once a form is closed, you cannot access the values the user entered in controls such as Field or List anymore. Accessing those controls properties would simply reload the form, with default values. So, a typical dialog box code will save any value that will be needed after the form is closed into member variables. For example, a dialog box containing a text field and two buttons labelled 'OK' and 'Cancel' should be implemented as follows:

Public bOK As Boolean
Public sField As String

Private Sub Form_Load()
  Field1.Text = sField
End Sub

Private Sub cmdOK_Click()
  sField = Field1.Text
  bOK = True
  Unload Me
End Sub

Private Sub cmdCancel_Click()
  bOK = False
  Unload Me
End Sub

Invoking this dialog box can be performed using the following code:

Private Sub Button1_Click()
  Dim f As frmDialog

  f.sField="Init Value"
  f.Show hbFormModal+hbFormPopup
  If f.bOk Then
    MsgBox "Entered text: " & f.sField
  End If
End If

Life cycle of forms

As soon as you access one of the form controls, or invoke one of the form properties or methods, or call the Load function on a form, the following happens:

When the form is displayed for the first time, the following happens:

If any window covers your form (for example a popup list, a dialog box, or a menu), it receives an Activate (False) event. It is typically time to interrupt timers used for animation, for example. When the form is again activated, it receives the same events as if it was displayed for the first time, except that the Paint event may not be raised if the form that is dismissed has its SaveBehind property set to True.

When the form is dismissed, the following happens:

Note: you should not perform any action that causes a form to be loaded from the Initialize method of a form. Doing so may lead to a system crash. Also, you should not try to open another form or a dialog box from while handling a Load event.

Handling common behaviors

It is a common situation to have to handle the exact same events on several forms. For example, you may have an About menu item on some of your forms, and the following code duplicated in each of those forms:

Private Sub mnuAbout_Click()
  Dim f as New frmAbout
  f.Show hbFormModal+hbFormPopup
End Sub

Fortunately, the HB++ object model offers an elegant feature to avoid such a situation. The tip is to use derivation and inheritance to implement that code in a generic class, and to manually declare the relevant controls or menu items to override default processing.

Create a new class in your project, and name it clsGenericForm. Paste the following code into that class:

Public mnuAbout As MenuItem ' Prevent HB++ from declaring this into the derived forms
                            ' and allows the event to be caught below

Private Sub mnuAbout_Click()
  Dim f as New frmAbout
  f.Show hbFormModal+hbFormPopup
End Sub

Then, just make each form that has a mnuAbout menu item derive from this class by entering clsGenericForm into its Extends property.

Note however that this only applies to code; you still have to duplicate the relevant controls and menu items on all the forms.

Members

MembersDescription
Activate Event raised when the form is activated or deactivated.
Caption Form title.
CmdBarAddButton Inserts a new button to the command bar.
CmdBarOpen Event raised when the command bar is about to be displayed.
Controls Builds a list of controls.
EraseStatus Erases the status bar.
Focus Control having the focus.
Group Element selected from a control group.
Help Help text associated with a form.
KeyPress Event raised when a character is entered or a key is pressed.
Load Event raised when the form is loaded.
Modal Indicates if a form is modal.
NavState Navigation state of the form.
OpenHelp Opens the help dialog box associated with a form.
Paint Event raised when the form should be drawn.
Repaint Redraws the whole form.
Resize Event raised when the form is resized.
SaveBehind Indicates if the screen is saved before opening a form.
Show Displays a form.
Unload Event raised when a form is unloaded.
Inherited from UIComponentDescription
ComponentToScreen Converts coordinates relative to a component into coordinates relative to the screen.
FiveWayNavigator Event raised when the user presses one of the 5-way navigator keys.
Height Element height.
Left Distance between the left edge of the form and the left edge of the element.
Move Moves or resizes an element.
PenDown Event raised when the user touches an UI element with the stylus.
PenMove Event raised when the user moves the stylus.
PenUp Event raised when the user releases the stylus.
ScreenToComponent Converts coordinates relative to the screen into coordinates relative a component.
Top Distance between the top edge of the form and the top edge of the element.
UIRef Internal reference to a control.
Width Element width.
Inherited from DisplayDescription
Arc Draws an arc.
BackColor Background color.
BatchLimit Size of the graphics instruction buffer.
CoordinateSystem Coordinates system.
CopyArea Efficiently copies part of a Display object to another.
DrawFont Font used to draw the text.
Flush Empties the graphics instruction buffer.
FontMetrics Returns a FontMetrics object describing the physical charateristics of the currently active font.
ForeColor Foreground color.
GetPixel Queries the color of a given pixel.
hWin Returns the window handle.
Line Draws a line.
Oval Draws a circle or ellipse.
Pattern Defines the pattern used to fill drawings.
Polygon Draws a polygon.
PutPixel Sets the color of a pixel.
Rectangle Draw a rectangle.
ResID Identifier of the resource associated with the object.
ScaleFont Enable or disable font scaling.
TextColor Text color.
TextOut Draw text.
Inherited from ObjectDescription
ClassID Returns the type identifier corresponding to the actual class of the object.
Implements Determines whether the object implements the features of a given class.
Iterate Event raised to iterate over the elements of a container object.
Recipient Recipient of events sent by the object.
Serialize Event raised to serialize the object content into a stream.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0N/A