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.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
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:
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.Private Sub btnStop_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
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.