Connect an event to complete behavior
Understand the binding, the procedure signature and the state transition behind a working event.
An event has two halves#
A handler needs both a binding and a matching BASIC procedure. A procedure named cmdSave_Click is not sufficient if the control’s event list points somewhere else. Conversely, a binding cannot execute a missing procedure. Select the control, choose the event in the event editor, and confirm the generated signature against the component reference.
Preserve the signature#
Keep parameter types, order and ByRef modifiers exactly as the event contract declares. ByRef parameters can let your handler influence the operation, such as cancelling a close or marking keyboard input handled. Renaming a local parameter is different from changing its type or direction.
Private Sub Form_QueryUnload(ByRef Cancel As Integer, ByVal UnloadMode As Integer)
If mBusy Then
Cancel = 1
lblStatus.Caption = "Wait for the current operation to finish."
End If
End SubThis guard alone does not implement cancellation. The download lesson includes the complete cancel-and-close sequence.
Choose the event by meaning#
| Event family | Typical responsibility |
|---|---|
| Click / command activation | Start a user-requested action. |
| Change / ValueChanged | React to the new value of a component. Use its actual documented event name. |
| Shown | Start work that needs the visible Form, such as an asynchronous load. |
| KeyDown / KeyPress / KeyUp | Handle physical keys separately from committed text input. |
| SelectionChanged | Inspect the new selection and guard the no-selection case. |
| Completed / Failed | Finish or report an asynchronous media operation. |
| QueryUnload / Unload | Decide whether closure may proceed, then release owned resources. |
Not every component offers every event. The reference shows each supported signature and its parameters.
Make state transitions explicit#
A long-running action normally has idle, busy, success and failure states. Reject repeated activation while busy, disable conflicting commands and restore them in Finally. A success label belongs after the operation succeeds, not immediately after starting it. Keep previous valid data visible if a refresh fails.
Avoid accidental loops#
Assigning a property inside its own Change handler may cause another value transition. Normalize only when needed and keep the operation idempotent. Use a clearly scoped update flag when several controls must be updated together, and release it in every exit path. Do not use a timer as a substitute for correctly handling an event.
20f4bfed6b · Build manifest