Concept of Event Driven Programming


Visual Basic is an event-driven programming language. Before proceeding to the next chapter, it is very important to have a good concept of event-driven programming. The common events are Click, DblClick, Load, MouseMove, MouseDown, MouseUp, KeyPress, KeyUp, KeyDown, GotFocus, LostFocus, etc.

When you click, press a key, move the mouse or fire other events, the particular block of code of the corresponding event procedure is executed, and then the program behaves in a certain way. This is called event-driven programming. 


When you fire an event, the code in the event procedure is executed, and then visual basic performs its operations as per the instructions written in the event procedure code. For example, in the first sample program, when you click the 'Print' button, the click event is fired, and then the code in the click event procedure gets executed. The code tells Visual Basic to print a text on the form. So as a result, you see a text printed on the form. 

Example: 
Write the following code in the DblClick event procedure of the form.

Private Sub Form_DblClick()
    Print "You have double-clicked" 

End Sub


Output:

Double Click mouse event


When you double-click on the form, the DblClick event procedure of the Form object is invoked, and then the code in the DblClick event procedure is executed. Thus, the code instructs Visual Basic to print a text on the form.

    
Now download the following sample programs and experiment with them.
    Download the following programs: 
     
    GotFocus & LostFocus events are fired when a control gets a focus or loses focus respectively.