By Kardi Teknomo, PhD .

< Previous | Next | VB Tutorial | Contents >

VB Form

Form modules (.FRM file name extension) are the foundation of most Visual Basic applications. Inside form module, you can define the following:

1. event procedures
2. general procedures, and
3. form-level declarations of variables, constants, types.



The code that you write in a form module is specific to the particular application to which the form belongs; it might also reference to other forms or objects within that application.

You can see all the form events in the drop down list of Form code view.


Form Events

When a form is loaded into memory, the form is hidden. It will not be shown to the user. You still need to show the form to the user. Form has several events that are triggered at certain state of the form depending on users' activity and the code that you provide. Several form events that you may need to consider are as follow:

  • Initialize event is called before the form is loaded. You may use this event for intial configuration. This even is called only one time.
  • Load event is called after the Initialize event. This is the place to put your code to control behavior of form and any Controls on the form. This event is called only one time.
  • Activate event is called every time the form become active form. Active form means that form is on the top windows among all. If our form is hiden and then you show again, it will become active form.
  • GotFocus event is called after that. It is called when user click the form or you load the form through code.
  • Resize event is called when either user or your code specify change the size of the form.
  • Paint event is called when the form is redrawn.

Other evets are explained in Event Procedure of this tutorial.

General procedures are sub procedure and functions. It does not need to follow any predefined name as events procedure. You can create your own general procedure with more meaningful name.

VB Form Declaration General declaration is located at the top of the form code. This is the place where you put the declaration of your variables, constants, and types

To understand how it works, let us try a trivia project.

1. Start VB with Standard Exe project. You will get one form

2. In the form, do double click to see the code view

3. In the general declaration section copy and paste the following code

Private Sub Form_Activate()
MsgBox ("form1 activate")
End Sub

Private Sub Form_Click()
Form2.Show
End Sub

Private Sub Form_GotFocus()
MsgBox ("form1 got focus")
End Sub

Private Sub Form_Initialize()
MsgBox ("form1 Initialize")
End Sub

Private Sub Form_Load()
MsgBox ("form1 load")
End Sub

Private Sub Form_Paint()
MsgBox ("form1 paint")
End Sub

Private Sub Form_Resize()
MsgBox ("form1 resize")
End Sub

4. In the Project window , do right-click. Select Add then Form .Now you have two forms (Form1 and Form2).

  1. Add Module VB Forms

5. Double click Form2 from the project window and double click the form to get code view.

6. In the general declaration section of Form2, copy and paste the following code

  1. Private Sub Form_Activate()
    MsgBox ("form2 activate")
    End Sub

    Private Sub Form_Click()
    Form1.Show
    End Sub

    Private Sub Form_GotFocus()
    MsgBox ("form2 got focus")
    End Sub

    Private Sub Form_Initialize()
    MsgBox ("form2 Initialize")
    End Sub

    Private Sub Form_Load()
    MsgBox ("form2 load")
    End Sub

    Private Sub Form_Paint()
    MsgBox ("form2 paint")
    End Sub

    Private Sub Form_Resize()
    MsgBox ("form2 resize")
    End Sub

7. Run the program (F5) and you will see the sequence of events that VB performs. Once you get form1, click on the form to get form2.

Note: you can download the file of this example from here .

< Previous | Next | VB Tutorial | Contents >

Rate this tutorial or give your comments about this tutorial

This tutorial is copyrighted .