Driven by Events
Visual Basic is an "event driven" language. What this means is that your coding will be performed in the sequence that the events occur, as opposed to the order in which it appears in your form module. It is therefore very important, if you want to create quality applications, to know which events will happen when.
Start Up and Shut Down Events
The most logical place to start would be the 6 events that occur when the form loads up and the 3 events that occur when the form shuts down.
They are, in this sequence:
Start Up:
Form_Initialize
Form_Load
Form_Resize
Form_Activate
Form_GotFocus
Form_Paint
Shut Down:
Form_QueryUnload
Form_Unload
Form_Terminate
This is what happens most of the time. You know, with a normal form in a little demo program. In many real life situations, things can be very different. Read on.
The Form_Initialize event always happens, and it always happens first. After that, unless something dreadful happens, like an END statement or a fatal error (you cannot Unload at this stage), the next event will be triggered.
That event is always the Form_Load event, without exception. From this point on you can unload the form, or do all sorts of other stuff to end the program - so the following 4 events rely on this not happening. Let's assume that you don't end the program at this stage.
The Form_Resize event will always happen, whether the form is visible, minimized or maximized, it will happen. The form receives its physical dimensions even if it is hidden - so after being loaded, it resizes.
The Form_Activate event is a little more picky. It will only happen if the form becomes the active form in the application. If for example you have loaded another form in this form's Load event, and that other form is the active one, this form's Activate event will not happen until it becomes the activated form. The event will also not be triggered if this form starts as minimized. Only when the form is restored or maximized, and becomes active, does this event occur. This event can of course happen any number of times in the lifetime of a form. While your user jumps from form to form, the Form_Deactivate and Form_Activate events occur, telling you whether your user is coming or going.
The Form_GotFocus event, like the Activate event, relies on the form being visible. But even more picky - only when the form gets the focus. This hardly ever happens, except if the form does not contain any other controls that can get the focus. For example, if your form contains a command button, the button gets the focus after the form loads up, and the Form_Gotfocus does not occur. If you can think of a good use for this event, please let me know.
The Form_Paint event occurs whenever the form, or any part of it, gets redrawn. So obviously it happens when the form becomes visible after loading up. It also happens when you drag something over the form - as each part of the form becomes exposed and is redrawn, the Form_Paint event occurs. Also, if you are making a form bigger by resizing it, the parts of the form that becomes visible as you drag it, triggers the Form_Paint event, sometimes several times a second.
Let's look at the three shut down events:
The Form_QueryUnload event is the first indication that the form wants to shut down. This is triggered by using the VB "Unload" command, or by clicking the little cross in the top right corner of the form, by pressing Alt-F4 to close the form, when Windows is shutting down, when the form's MDIForm parent is shutting down (if applicable), and so on. The only time your form will disappear without triggering this event is if you pull the plug on your computer, or a fatal error occurs in Windows or in your application, or if you use the END statement. This event is commonly used for asking the user "Do you want to save changes?" and then saving the changes if the user so wishes. You can cancel the unloading of the form by setting the Cancel parameter to True (or any non-zero value).
The Form_Unload event confuses a lot of people who all want to know what the difference is between QueryUnload and Unload. Well, the main difference is that the QueryUnload event is for asking the user "Do you want to save" and "Are you sure?", and then tidying up user stuff, like saving changes etc. The Unload event happens after the user has said "Yeah sure, go ahead" and you have saved all the changes. Now you are tidying up the system stuff - making sure all files are closed and all objects are set to nothing. Also making sure that all other forms are also unloaded. You can Cancel the unloading of the form at this point as well, although this is seldom done and is not recommended unless doing so can save the user from potential loss of data.
The Form_Terminate event also happens.
The Other Events
Although starting up and shutting down are by far the most common event sequences, there are several other very important combinations of events to take note of, as listed delicately below:
Clicking on a Form:
Form_MouseDown
Form_MouseUp
Form_Click
Double Clicking on a Form:
Form_MouseDown
Form_MouseUp
Form_Click
Form_MouseMove
Form_DblClick
Form_MouseUp
Pressing a Key on a Form
Note that the active control of the form normally receives the Key events, unless there is no active control, or if the KeyPreview property of the form is True.
Form_KeyDown
Form_KeyPress
Form_KeyUp
Minimizing then Restoring a Form:
Form_Resize (when minimizing)
Form_Resize (When Restoring. No events occur while the form is minimized.)
Form_Paint
Calling a Message box from a Form
The form does not respond to ANY events while the message box is displayed.
Form_Paint (this only happens if a part of the form becomes visible when the message box is closed.)
Dragging something onto a Form
If the Form's OLEDropMode property is set to "None", nothing happens, otherwise the events below occur.
Form_OLEDragOver (occurs continuously while dragging.)
Form_MouseMove (once, when dropping)
Form_OLEDragDrop (once, when dropping)
ThisArticle_Terminate Event
There are of course many more combinations of events that you and your form may experience, but I believe that the most common ones had been covered here. If this article triggers enough question and request events, I might just write a follow up.