VB6 Tutorial 28: Form Events

Posted On 2018-04-15 by VB6Boy
Tags: VB6 Tutorials 
Views: 50

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

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.


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.


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


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.


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


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.


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.


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.


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.


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.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

About the Author

VB6Boy has posted a total of 71 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links