Monday, 8 August 2011

Learning about Events

0 comments
 
Visual Basic is "Event Driven" language.
What does it mean?

Everything that happening, launch an event.
You've moved the mouse? The "MouseMove" event has been launched.
You've pressed a key? The "KeyPress" event has been launched.

You can program the events.
When the mouse moves, you can change the Form's color (for example),
and when a key is pressed, You can play a MP3 file.

To start programming the events, double click on the form.You will see the "Code Window"


The Code Window opened with the Form_Load event.
The Form_Load event occurs when the form is loaded, and this happening
when you start the program.
So the code that you will enter to the Form_Load event will be launched
when the program is being started.

The code that belongs to the Form_Load event should be placed
between Private Sub Form_Load()    and    End Sub
The Form_Load event should look like this:

Private Sub Form_Load() (The beginning of the Form_Load event)
This is the code that belongs to the Form_Load event
End Sub
(The end of the Form_Load event)

Lets program the Form_Load event.

"MsgBox" is Visual Basic command that launch a message box.
for example, the line:

MsgBox "Hello"Will launch a message box with the text "Hello".

Insert the line   MsgBox "Hello"   to the Form_Load event.


Now run your program using the Play button. When the program is started, a message box with the
text "Hello" is appear

More 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 (Figure 23).

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" (Figure 24).

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 (Figure 7)).
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.

The Command Button's Events

Now lets program some of the Command Button's events. Select "Command1" from the components list (Figure 23). Check which events the Command Button has by clicking the Events list (Figure 24).

We want to execute a code when the user is clicking on the button,
So lets program the Command Button's Click event.
Select "Click" from the Events list.
Insert the following line to the Click event:

MsgBox "You have Clicked on the button!"
After you inserted this line to the Click event the Click event should look like this:


Private Sub Command1_Click()
    MsgBox "You have Clicked on the button!"
End Sub



As you can see, the Command Button's Click event called "Command1_Click",
because the name of the specific Command Button that we program
its Click event is "Command1".
If we had program the Click event of a Command Button with the
name "BlahBlah7", the Click event would be called "BlahBlah7_Click".
Every component has its own unique events, therefore if you had
5 Command Buttons on your form, every one of them has its
own unique Click event.


Run the program, and click the button.
When you clicking the Command Button with the
name "Command1", The Command1_Click event is being
executed, and a message box with the text "You have Clicked on the button!"
is popping.
To learn about more events, we will use the "Print" command.
The "Print" command simply writing a text on the form.
For example, the following line:

Print "Hello"

Will write "Hello" on the form (Figure 25)



Add another Command Button to your form. The New Command Button's name is "Command2" by default




Now, rewrite the Command1 Click event and insert the
following line to it:

Print "This is Command1"

Select "Command2" (This is the name of the second
Command Button) from the Components list (Figure 23),
And select "Click" from the Command2 events List.

Enter the following line to the Command2_Click event:

Print "This is Command2"

After you've done so, your code should look like this:


Private Sub Command1_Click()
    Print "This is Command1"
End Sub

Private Sub Command2_Click()
    Print "This is Command2"
End Sub


Run the program.
When you are clicking on Command1 Button, the text "This is Command1"
appears on the form, and when you are clicking on Command2 Button,
the text "This is Command2" appears on the form.

Leave a Reply