Event Driven Programming And Event Procedure – Visual Basic

Event Driven Programming

The interface components have the ability to recognize user events. This code is built in to the interface component. The programmer determines the response of the component to the event.

When an interface component is used, the programmer decides the action required of component in response to a particular event and writes code that will be executed when the event occurs. Thus, the response of identical interface components to an event can be different in different situations.

For example, in the Math calculator the number buttons display the selected number in response to the mouse click while the button Sqrt calculates and displays the square root of the number.

Each interface component can have multiple events. It is not necessary that the component should respond to all events that it can recognize. For example, we can choose not to respond to the Key Press event in the case of button.

In addition, an interface component may also respond to multiple events. A text box can respond to a mouse click as well as a key press event.

In event driven programming, an application is built up as a series of responses to user events. The calculator is a good example of an application that is event driven. All actions in the application are built as responses to events.

The programmer decides the action required of the application and divides them up as responses to events for different interface components used. The interface components used and the events that they respond to depend on the functionality required.

In the Math calculator, the programmer decided to use buttons as a means of specifying the numbers as well as the required operations and the test box to display the result. Since buttons are being used, the user would instinctively click on them. Thus, in the Click event of the various buttons, code was added to provide the necessary functionality.

In the case of the number buttons the code will display the selected number in the text box while for the ‘=’ button the code will display the result of the operation.

The code executed in response to an event is called the event handler.

Response to an Event

Whenever a user makes an action on the interface, an event associated with it is to be responded. Here is an example that explains the sequence of action that take place in response to an event consider the following diagram that displays the interface of an application and the code in the background.

Response to an event

Figure 1 Response to an event

Figure 1: Response to an Event

Let’s take an example of any page or form of an application which may consist many of the user interfacing controls. They do not perform any actions until and unless they are triggered or clicked or any event is made, thought there is code associated with the application. The code will get executed only in response to a user event.

When a user clicks of the button with label ‘Click me’ then the click event for the command button is generated. The application searches the code to locate an event handler for the click event of the particular command button. Then the event handler code is loaded into memory and executed. This then updates the appearance or changes the state of application.

Thus, in an event driven application all actions are responses to events.

Events need not necessarily be initiated by users. It may also be triggered by another event.

For example, the press of the Enter key in the case of a Word processor. The response to this event is the cursor moving on to the next line. However, when this event occurs at the end of a page then the code responsible for inserting a new page gets executed.

Another example of an event not triggered by the user is a timer. The timer starts when the application is first loaded and then at predetermined intervals will execute a certain piece of code. A screen saver is another example if a timer initiated action.

Thus, the response to an event may in turn act as an event itself triggering the execution of another piece of code.

Event Procedure in Visual Basic

Most events are associated with objects. The event clicking cmdButton is different from the event clicking picBox. These two events are specified cmdButton_Click and picBox_Click. The statements to be executed when an event occurs are written in a block of code called an event procedure of Visual Basic. The structure of an event procedure is

 

Private Sub objectName_event()

statements

End Sub

The word Sub in the first line signals the beginning of the event procedure, and the first line identifies the object and the event occurring to that object. The last line signals the termination of the event procedure. The statements to be executed appear between these two lines.

(Note: The word Private indicates that the event procedure cannot be invoked by an event from another form. This will not concern us until much later in the text. The word Sub is an abbreviation of Subprogram.) For instance, the event procedure

Private Sub cmdButton_Click()

txtBox.Text = “”

End Sub

clears the contents of the text box when the command button is clicked.

Writing a Event Procedure in Visual Basic 6

Interface to a event procedure example

Figure 1-1Interface to a event procedure

The form in Figure 1-1, which contains a text box and a command button (they can be add from visual basic development environment), will be used to demonstrate what event procedures are and how they are created. Three event procedures will be used to alter the appearance of a phrase that is typed into the text box. The event procedures are txtPhrase_LostFocus, txtPhrase_GotFocus, and cmdBold_Click.

Object Property Label
frmDemo Caption Demonstration
txtPhrase Text (blank)
cmdBold Caption Make Phrase Bold

Steps involved:

1. Create the interface in Figure 1-1. The Name properties of the form, text box, and command button should be set as shown in the Object column. The Caption property of the form should be set to Demonstration, the Text property of the text box should be made blank, and the Caption property of the command button should be set to Make Phrase Bold.

2. Double-click on the text box. A window, called the Code window, appears. See Figure 1-2. Just below the title bar is two drop-down list boxes. The left box is called the Object box and the right box is called the Procedure box. (When you position the mouse pointer over one of these list boxes, its type appears.)

Code window Visual Basic

Figure 1-2 Code window Visual Basic

3. Click on the down-arrow button to the right of the Procedure box. The dropdown menu that appears contains a list of all possible event procedures associated with text boxes. See Figure 1-3.

Figure 1-3 Drop down menu event procedure

Figure 1-3 Drop down menu event procedure

4. Scroll down the list of event procedures and click on LostFocus. (LostFocus is the 14th event procedure.) The lines

 

Private Sub txtPhrase_LostFocus()

End Sub

 

appear in the code window with a blinking cursor poised at the beginning of the blank line.

Figure 1-4 LostFocus Event procedure

Figure 1-4 LostFocus Event procedure

5. Type the line

txtPhrase.Font.Size = 12

 

between the existing two lines. (We usually indent lines inside procedures.)

(After you type each period, the editor displays a list containing possible choices of items to follow the period. See Figure 1-4. This feature is called “List Properties/Methods.” In Figure 1-4, instead of typing the word “Size,” you can double-click on “Size” in the displayed list or highlight the word “Size” and press Tab.) The screen appears as in Figure 1-5. We have now created an event procedure that is activated whenever the text box loses the focus.

Figure 1-5 LostFocus Event Procedure

Figure 1-5 LostFocus Event Procedure

6. Let’s create another event procedure for the text box. Click on the down-arrow button to the right of the Procedure box, scroll up the list of event procedures, and click on GotFocus. Then type the lines

txtPhrase.Font.Size = 8

txtPhrase.Font.Bold = False

between the existing two lines. See Figure 1-6.

Figure 1-6 GotFocus Event Procedure

Figure 1-6 GotFocus Event Procedure

7. The txtPhrase_Change event procedure in Figure 1-6 was not used and can be deleted. To delete the procedure, highlight it by dragging the mouse across the two lines of code, and then press the Del key.

8. Let’s now create an event procedure for the command button. Click on the down-arrow button to the right of the Object box. The drop-down menu contains a list of the objects, along with a mysterious object called (General). See Figure 1-7. [We’ll discuss (General) in the next section.]

Figure 1-7 List of Objects visual basic

Figure 1-7 List of Objects visual basic

9. Click on cmdBold. The event procedure cmdBold_Click is displayed. Type in the line

txtPhrase.Font.Bold = True

The screen appears as in Figure 1-8, and the program is complete

Figure 1-8 Three event procedure in visual basic

Figure 1-8 Three event procedure in visual basic

.

10. Now run the program by pressing F5.

Figure 1-9 Textbox containing input visual basic

11. Type something into the text box. In Figure 1-9, the words “Hello Friend” have been typed. (A text box has the focus whenever it is ready to accept typing; that is, whenever it contains a blinking cursor.)

12. Press the Tab key. The contents of the text box will be enlarged as in Figure 1-10. When Tab was pressed, the text box lost the focus; that is, the event Lost-Focus happened to txtPhrase. Thus, the event procedure txtPhrase_ LostFocus was called, and the code inside the procedure was executed.

Figure 1-10 TextBox after LostFocus

13. Click on the command button. This calls the event procedure cmd Bold_Click, which converts the text to boldface. See Figure 1-11.

14. Click on the text box or press the Tab key to move the cursor (and, therefore, the focus) to the text box. This calls the event procedure txtPhrase_GotFocus, which restores the text to its original state.

Figure 1-11 TextBox after event is clicked

Figure 1-11 TextBox after event is clicked

15. You can repeat Steps 11 through 14 as many times as you like. When you are finished, end the program by pressing Alt+F4, clicking the End icon on the Toolbar, or clicking the Close button (X) on the form.

The Importance of Event-Driven Programming

When you create a program in Visual Basic, you’ll generally be doing event-driven programming. Event-driven programming means that most of the code you write will be run as users do things within your program or even when certain things happen in Windows–when events occur. Of course, programming this way means that you have to know when events occur and have to write code that will make your program do something in response to the event.

Fortunately, Windows and Visual Basic do most of the work for you. Whenever an event takes place, Windows sends out a message to your program. Your program reads this message, and then runs the code you’ve attached to the event. If you don’t specify code for an event, your program will simply ignore the event.

Generally, this code is known as a procedure, defined as any block of code that can be called from within your application. This code might be used to move objects around on a form, calculate a value from a formula, or write data to a database. No matter the purpose, a procedure always uses this format:

[Public|Private] [Static] Sub|Function|Property _

function_name (arguments) [As Type]

{...Your procedure code...}

End Sub|Function|Property

An event procedure is the place in your project where you put the code that will run when an event occurs. To write an event procedure, you must access the Code window for your object by doing one of the following:

  • Double-clicking the object
  • Selecting the object with the mouse and pressing F7
  • Selecting the object and choosing Code from the View menu
  • Selecting the object’s form in the Project Explorer, clicking the View Code button, and choosing the object from the Code window

The code you write in an event procedure will run whenever the event occurs.

Visual Basic automatically generates an event procedure when you select an event in the Code Window.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>