VB6 Tutorial 05: Event Driven Programming

By | 2018-03-22

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 the user clicks , presses key or moves the mouse, the particular block of code of the corresponding event procedure is executed. Then the program behaves in a certain way. This is event driven programming. When you fire an event, the code in the event procedure is executed, and then visual basic does what the code in the event procedure instructs to do. For example, in the first sample program, when you click the Print button, click event is fired. Then the code in the click event procedure gets executed. The code instructs to print a text on the form. After that you see a text printed on the form. This is the concept of event driven programming. That means , the code is not executed from top to bottom but it works when the corresponding event procedure is invoked.

Example

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

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

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

Event Driven Programming Demo

There are three sample projects attached to this post, up in the top section. Give them a try, and experiment with them a bit.

QuickHint:

Sample programs and a detailed explanation of other events are given in the appropriate lessons.

Later in this series, there will be a lesson about to how to name a control, a very important programming guide for use with any computer language.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.