Event procedures sometimes challenge beginning GFA-BASIC 32 programmers, but the concept of an event procedure is very simple. When the user presses a command button or enters text into a text box, something has to happen that tells the application the user has just made a move. Windows receives events from all kinds of sources. Most events come directly from the user at the keyboard and mouse running applications within Windows.
When Windows recognizes that the user triggered an event and that the event is not a system event, such as a Windows Start button click, but an event directly needed by an application, Windows passes that the event to the application. If you've written an event procedure to respond to that exact event, your application will respond to the event. If you haven't written an event procedure, the event goes unhandled.
Code in a GFA-BASIC 32 application is divided into smaller blocks called procedures. An event procedure contains code that is executed when an event occurs (such as when a user clicks a button). An event procedure for a control combines the control's actual name (specified in the Name property), an underscore (_), and the event name. For example, if you want a form named frm1 to invoke an event procedure when it is clicked, use the procedure Sub frm1_Click.
One way to create an event procedure, is to select the name of a form in the Properties sidebar. The second half of the sidebar window displays all event subs for the form. Select the name of an event for the form. Note that a template for the event procedure is now displayed in the Code window.
Another way to create an event procedure is by typing the Sub statement at the beginning of a line, and then type the OCX name followed by an underscore. A list box with all event names pops up and lists all available and already used events (bold). A short description is displayed in the status bar.
The underscore separates the OCX name from the event name and is required. All event procedures are named this way. Therefore, an event procedure named cmdExit_DblClick () executes if and only if the command button named cmdExit's event named DblClick occurs.
You should familiarize yourself with common events that can occur for the controls that you know about. Both the form and its controls can receive events. Here are some common form events that can occur during an application's execution:
Activate | This event occurs when a form gets the focus. If an application contains multiple forms, the Activate event occurs when the user changes to a different form by clicking on the form or by selecting the form from a menu. |
Click | This event occurs when the user clicks anywhere on the form. If the user clicks a form that's partially hidden from view because another form has the focus, both a Click and an Activate event take place. |
DblClick | This event occurs when the user double-clicks the form. |
Deactivate | This event occurs when another form gets the focus. Therefore, both the Activate and Deactivate events occur when the user selects a different form. You may choose to write event procedures for both events for each form, for only one event for one of the forms, or a combination thereof depending on the needs of your application. |
Load | This event occurs right as the form is loaded into active memory (using LoadForm!)and appears on the screen. |
Paint | This event occurs when Windows must redraw the form because the user uncovered part of the form from under another object, such as an icon. |
Resize | This event occurs when the user changes the size of the form. |
Destroy | This event occurs when the application removes a form from the window using code. When an application is terminated, all loaded forms are first unloaded, so you must write an Unload event procedure for each form if you want to perform some kind of clean-up or file-saving procedure at the end of an application's session. |
Next: Accessing HTML Help Files
Back to: Creating An Application.
{Created by Sjouke Hamstra; Last updated: 16/05/2020 by James Gaite}