Event handlers using object.method statements

Once you create a user interface, you are ready to add event handlers to your solution. The computer must be told what to do when each possible event occurs. Run our first program with event handlers. As you see, it displays a fixed mesage and has four objects in its user interface:
  1. A form we will refer to as "Me"
  2. A button named "btnStop"
  3. A button named "btnMessage"
  4. A text box named "txtOut"
and it can respond to two events:
  1. The user clicks on the "Stop the program" button
  2. The user clicks on the "Display the message" button
The following are the listings of the two event handler subprograms:
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.Close()
End Sub

Private Sub btnMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessage.Click
        txtOut.AppendText("Hello, my friend!  ")
End Sub
Each of these event handlers consists of three lines. The first and third lines begin and end the subprogram. The executable instructions (also called statements) are between them.

The first event handler executes when the user clicks on the button named "btnStop." When the Me.Close() instruction executes, the program is terminated. Me refers to the currently running object. (Since this is a Windows application, the development system automatically generated it. I am being a little loose with terminology here -- more on this later).

The second event handler executes when the user clicks on the button named "btnMessage." It causes the computer to display the greeting message.

Let's take a closer look at the first lines of the event handlers. For example:

Private Sub btnStop_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
This designates a private subprogram, which means it cannot be used outside this form. The name of the subprogram is btnStop_Click() and it handles the event btnStop.Click. In other words, it is executed when the user clicks on the button called btnStop. Similarly, the event handler btnMessage_Click() is executed when the user clicks on the button named btnMessage.

To summarize, event handlers have an opening line and an end line, with one or more executable instructions between them:

Private Sub  . . .
     One or more executable instructions
End Sub

Note that the statements we have used in our event handlers have the same format: the name of an object and the name of an action, with a dot between them. Programmers call these actions methods, so we refer to these as object.method statements.

We will be learning several other statement formats, and you will be free to use any of them in writing event handlers.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.