Visual Basic Tutorial continued…

More About Events
The Form has more events besides the Form_Load event.
How can you find them?

Click on the Drop-Down List that found in the upper left corner of
the Code Window, where appears right now the text “Form”


You will see a list of the components that found in your program.
You have 1 command button with the name “Command1” and 1 Form.
Here you select which component’s event you want to program.
We want to program a form’s event, so select “Form” from the list.
Which events the form has?
Click on the Drop-Down List that found in the upper right corner of
the Code Window, where appears right now the text “Load”.


You will see the complete list of the form’s events:
Load, LostFocus, MouseDown, MouseMove and more.

Lets program the Form_Unload event.
Select “Unload” from the form’s events list.
The Form_Unload event occurs when the form is being unloaded,
and this happening when you close the program (Using the form’s X button).
So the code that you will write in the Form_Unload event will be launched
when you close the program.

Insert the following line to the Form_Unload event:

MsgBox "GoodBye"

After you’ve inserted this line to your Form_Unload event,
the Form_Unload event should look like this:

Private Sub Form_Unload(Cancel As Integer)
MsgBox "GoodBye"
End Sub

Run the program.
When the form is being loaded at the very start, The Form_Load event
is being executed and a message box with “Hello” text is popping.
When you close the program by clicking the form’s X button, the Form_Unload
event is being executed and a message box with “GoodBye” text is popping.

You can leave a response, or trackback from your own site.

Leave a Reply