Chapter #3: Events
Let´s talk a litle about program flow, before:
VB is an "object-oriented" programming language. Unlike BASIC (which was "problem solving" oriented language), the flow of the program doesnt go in a top-down order passing "line-by-line" through the whole code. When your program enters run-time, what it does is respond to certein events that are triggered, by executing lines of code assigned to those events.
This "pieces" of code represent the modular part that will be executed only when that event is "raised" (triggered). It is marked on the code by a structure that could be common to you if you´ve programmed in BASIC:
|
Sub Recipient_Event() End Sub |
An example would be the first of all events to ocur during the run-time stage of a program:
|
Form_Load() End Sub |
So, the code between "Sub..." and "End Sub" will only run when the Form is loading. After all that code is executed, the program will completely stop doing anything, until the user "uses a control" to... control (duh!) the flow of the program triggering another event. But, that´s not "exactly" how Event Subs appear in the code-sheet. They are declared as private:
|
Form_Load() End Sub |
That is automatically set, but you can change it to 'Public' or even erase the word 'Private' if you don´t like it. The "Public/Private" concept is a litle more advance and I won´t explain it here. We´ll concentrate on the matter of "when should I do what?"
What?
hehe... ok, here it goes:
Let´s see a small "hello world" program that shows a text on the Form´s caption and displays a message to the user when they click a button:
|
'You can try this one out by copying and pasting this code on a "Standard EXE" project. To paste the code on the 'code sheet go to the menu "View" and choose "Code", then paste there (CTRL+V). Then double-click on the 'CommandButton button on the "tools panel" at the left-most of the IDE. ' Private Sub Form_Load() Form1.Caption = "Hello there!" End Sub Private Sub Command1_Click() MsgBox "Hello world!" End Sub |
If you´ve ran this program you´ve seen what it does. In case you haven´t, when the Form loads, its caption (The text on the title bar) changes form the default 'Form1' to 'Hello there!'. That´s it. The program doesn´t do anything else until the user does something else with the program. After the load stage, the program just sits there and does nothing, until...
The user clicks on 'Command1'. A command button. We can code up a section where we control what will happen if the user clicks on the button named 'Command1'. We will use the function 'MsgBox' (MessageBox) to show an informative message to the user. So now we´ve added code to control two stages of the program:
The 'Load' process of 'Form1' (Form1_Load)
The 'click' on the button 'Command1' (Command1_Click)
Other events are:
To the CommandButton, for example:
|
Click DragDrop (the user released an object -ie:a file from explorer- over the button) MouseMouve (the mouse is moving over the button) KeyPress (a key has been stroke while the button had the focus) LostFocus (the user has given focus to another control) |
Focus is given by clicks and mouse-moves on the screen. Buttons have focus when a dotted-path is strapped around its caption. Try this to know what I´m talkin about:
If you are using Windows, go to "Start/Shut Down System" and on the Form that shows up stating what kind of shut down process do you request (Shut Down/Restart/Log off/etc) start pressing the 'TAB' key. You´ll see a dotted-path around the control that has the focus. Those are all buttons.
The buttons with these captions:
Ok/Cancel/Help
are commandButtons
and the buttons with these captions:
Shut Down/Restart/Log off
are optionButtons.
Only few things (properties/events/methods) change between one and other kinds of buttons.
Speaking of methodsssss...... we´ll talk about them on the next chapter.