Home -> Programming -> Processing -> Event Procedures

Event Procedures

An event procedure is a normal procedure which has one addition. Visual Basic is a GUI program and offers a suite of controls which make it easy for the new programmer to serve up a modern looking and behaving program. The key to how this is done is the event procedure. For example if we want to use a button control we place it on the form and then we need to name it. Actually it automatically comes with a default name to prevent name duplication of controls. However, if we need 3-4 buttons and they are named button1, button2, button3 and button4 no one is going to remember which button does what. So we give each button a name which describes its function. Now we are ready to program the actions of the button. When we double-click the button, we are switched into the code editor with an event procedure being created for us for the control we just double-clicked. If we had named the button btnExit then the event procedure would look like this:

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

End Sub

Look closely at the end of the procedure declaration. (The first line of the procedure.) On the end of the procedure is the statement Handles btnExit.Click. This is the key to turning an ordinary procedure into an event procedure, or in other words, to link a control on your form to a procedure in your written code. When we click the control on a form, VB used the control name to create the declaration and close for this Event Procedure. In this way we also do not need to learn the details of how to create these subroutines when we are starting out. All we have to do is add our code inside the event procedure which VB creates for us. For this particular procedure we add a comment and the statement to end the program. The complete procedure it would look like this:

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    'Close the form
    Me.Close()
End Sub

The nice thing about event procedures is that by double-clicking the control VB automatically creates the declaration complete with the correct event binding. Simple, elegant, well designed and a pleasure to use, so use it. Don't type out the event procedures yourself! One spelling mistake and nothing works! If you want to know which events go with which controls, you can refresh your memory here.

Going Further

Tying code to an event, such as the click of a button, is done by using the Handles statement. We can use this feature to tie together multiple events so that they are all handled by one procedure. A beginning programmer might ask why should we do this, but part of programming is organizing our code and this technique does exactly that. Instead of having related code scatters into several subroutines we can put it all together in one subroutine. Consider the following example.

This program, called Faces, uses radio buttons to control the color of the text displayed in the message to the user. In keeping with advice to always attach code to a control in order to keep the coding simpler you would expect four event procedures: one for each radio button. This arrangement means we would have four procedures which would look like this:

    Private Sub radRed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRed.CheckedChanged
        lblMessage.ForeColor = Color.Red
    End Sub

    Private Sub radGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radGreen.CheckedChanged
        lblMessage.ForeColor = Color.Green
    End Sub

    Private Sub radBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radBlue.CheckedChanged
        lblMessage.ForeColor = Color.Blue
    End Sub

    Private Sub radOrange_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOrange.CheckedChanged
        lblMessage.ForeColor = Color.Orange
    End Sub

In VB 6 this technique was called a control array and although the name has been dropped it is still possible to effect the same thing in VB 2005 and VB 2008. Here we create a single procedure to handle all these events. Now if you think about it, the immediate problem becomes: How does my program know which Radio Button has been clicked? If you will recall, there is a property of Radio Buttons, all controls actually, called TabIndex which has a unique value on the form - each control has a different value for TabIndex. So we can use this property to discern which Radio Button has been clicked. Here is what the new procedure would look like.

    Private Sub setColor(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRed.CheckedChanged, radGreen.CheckedChanged, radBlue.CheckedChanged, radOrange.CheckedChanged, radOther.CheckedChanged

        'We need a variable of type Control to find out what the TabIndex value is
        Dim theRadioButton As Control

        'We assign the sender control into our variable
        theRadioButton = CType(sender, Control)

        'We use a case statement to choose which color to use for the message
        Select Case CType(theRadioButton.TabIndex, Integer)
            Case 0
                lblOutput.ForeColor = Color.Red
            Case 1
                lblOutput.ForeColor = Color.Green
            Case 2
                lblOutput.ForeColor = Color.Blue
            Case 3
                lblOutput.ForeColor = Color.Orange
            Case 4
            If radOther.Checked Then
                dlgColor.ShowDialog()
                lblOutput.ForeColor = dlgColor.Color
            End If
        End Select
    End Sub

Lets go through this code line by line to see what is happening.

  1. Notice the Handles statement at the end of the procedure declaration. In this case we have listed all the Radio Button Click events that we want this procedure to handle. This is the way to cause several events to be handled by one procedure.
  2. Inside the procedure we declare a variable of type Control. See the comment in the code.
  3. Now we need to get the data from the control, with which the user interacted, into our variable so we can check the TabIndex value to see which Radio Button was pushed. This is done with the CType statement using the data which all VB generated procedures declare in their headers - Sender. Sender is the control which was activated when the user clicked the Radio Button. Ctype converts Sender into a 'Control' data type. Note this will not work if the data is not compatible with the conversion process!
  4. Now we can use a case statement to issue commands to color our text appropriately based on the TabIndex of the Radio Buttons. If you compare the TabIndex of each Radio Button you will notice that they correspond to the values in the respective Radio Buttons.

You can explore the whole program to see how it works here.