VB6 Tutorial 28: Form Events

By | 2018-04-15

Before a form becomes fully functional, a series of form events is invoked one by one when you run your program. In this lesson, we’ll discuss those events.

If you need information about common events for most controls, see the lesson onĀ Common events.

Initialize

The Initialize event is the first event of a form when the program runs. This event is raised even before the actual form window is created. You can use this event to initialize form’s properties.

Example

Private Sub Form_Initialize()
   Text1.Text = ""
   Text2.Text = ""
End Sub

Load

After the Initialize event, Load event fires when the Form is loaded in the memory. This Form event is invoked for assigning values to a control’s property and for initializing variables.

Note that the form is not visible yet. For this reason, you cannot invoke a graphic function like Cls, PSet, Point, Circle, Line etc and you cannot give the focus to any control with the SetFocus method in the form’s Load event procedure. The Print command will not even work.

On the other hand, this won’t be a problem setting a control’s property in the form’s load event.

Example

Private Sub Form_Load()
   Text1.Text = ""
   Text2.Text = ""
   var1 = 0
End Sub

Resize

After the Load event, the form receives the Resize event. The form’s Resize event is also raised when you resize the form either manually or programmatically.

Activate and Deactivate

After the Resize event, Activate event fires. While working with multiple forms, the Activate event fires when the form becomes an active form in the current application, and the Deactivate event fires when the other form becomes the active Form.Another important form event is the Paint event which I’ll not discuss here. Paint event will be covered later in another lesson.Note that when you run your program, form events such as Initialize, Load, Resize, Activate and Paint events are raised automatically one by one. The Paint event is not raised when the form’s AutoRedraw property is set to True.

QueryUnload

When you close or unload the form, the form first receives the QueryUnload event and then the Unload event.

The QueryUnload event is invoked when the form is about to be closed. When the form is closed, it may be unloaded by the user, the task manager, the code, owner form, MDI form or it may be closed when the current windows session is ending.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   'code here
End Sub

This event procedure takes two parameters, Cancel and UnloadMode. The Cancel parameter cancels the unload operation, and depending on the symbolic values of UnloadMode, a particular task can be performed.

Example

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   If UnloadMode = vbFormControlMenu Then 'vbFormControlMenu=0
      MsgBox "the form is being closed by the user."
   End If
End Sub

The other constants are vbFormCode, vbAppWindows, vbAppTaskManager, vbFormMDIForm, vbFormOwner.

Symbolic constant values for the UnloadMode parameter are explained below:

  • vbFormControlMenu: when the form is closed by the user.
  • vbFormCode: Use this constant when you’re closing a form by code.
  • vbAppWindows: when the current windows session is ending.
  • vbAppTaskManager: when the task manager is closing the application.
  • vbFormMDIForm: when the MDI parent is closing the form.
  • vbFormOwner: when the owner form is closing.

Unload

The Unload event fires when the Form unloads. You can use this event to warn the user that data needs to be saved before closing the application.

Example

In this program, you cannot close the Form keeping the text field blank

Private Sub Form_Unload(Cancel As Integer)
   If Text1.Text = "" Then
      MsgBox "You cannot exit keeping the text field blank"
      Cancel = True
   End If
End Sub

When Cancel = True, you cannot close the Form. The Cancel parameter is used to cancel the the form’s unload operation.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.