GotFocus, LostFocus Events

Purpose

Occurs when an object receives or loses the focus respectively. The focus is shifted either by user action, such as tabbing to or clicking the object, or by changing the focus in code using the SetFocus method. A form receives the focus only when all visible controls are disabled or when it is explicitly given the focus.

Syntax

Sub object_GotFocus([index%])

Sub object_LostFocus([index%])

object:Ocx Object
index%iexp

Description

Typically, you use a GotFocus event procedure to specify the actions that occur when a control or form first receives the focus. For example, by attaching a GotFocus event procedure to each control on a form, you can guide the user by displaying brief instructions or status bar messages. You can also provide visual cues by enabling, disabling, or showing other controls that depend on the control that has the focus.

A LostFocus event procedure is primarily useful for verification and validation updates. Using LostFocus can cause validation to take place as the user moves the focus from the control. Another use for this type of event procedure is enabling, disabling, hiding, and displaying other objects as in a GotFocus event procedure. You can also reverse or change conditions that you set up in the object's GotFocus event procedure.

index% - An integer that uniquely identifies a form or control if it's in a form or control array.

Example

Form frm1 = "Key Events", 20, 20, 300, 300

Ocx TextBox txt(1) = , 10, 10, 150, 40

txt(1).BorderStyle = 1

Ocx TextBox txt(2) = , 10, 70, 150, 40

txt(2).BorderStyle = 1

Do

Sleep

Until Me Is Nothing

 

Sub txt_GotFocus(Index%)

' Show focus with red.

txt(Index).BackColor = RGB(255, 0, 0)

End Sub

 

Sub txt_LostFocus(Index%)

' Show loss of focus with blue.

txt(Index).BackColor = RGB(0, 0, 255)

End Sub

Remarks

An object can receive the focus only if it's Enabled and Visible properties are set to True. To customize the keyboard interface in GFA-BASIC 32 for moving the focus, set the tab order by rearranging the controls using the 'Ocx Overview' window or specify access keys for controls on a form.

See Also

Form, Activate, Enabled, SetFocus

{Created by Sjouke Hamstra; Last updated: 08/10/2014 by James Gaite}