Datasheet
8. After you close the message box by clicking the OK button, click the Exit button on your form.
The application closes and you will be returned to the Visual Basic 2005 IDE.
How It Works
The code that you added to the Click event for the OK button will take the name that was entered in the
text box and use it as part of the message that was displayed in Figure 1-19.
The first line of text entered in this procedure is actually a comment, text that is meant to be read by the
human programmer who is writing or maintaining the code, not by the computer. Comments in Visual
Basic 2005 begin with a single quote (
‘), and everything following on that line is considered a comment
and ignored by the compiler. Comments will be discussed in detail in Chapter 3.
The
MessageBox.Show method displays a message box that accepts various parameters. As used in
your code, you have passed the string text to be displayed in the message box. This is accomplished
through the concatenation of string constants defined by text enclosed in quotes. Concatenation of strings
into one long string is performed through the use of the ampersand (
&) character.
The code that follows concatenates a string constant of
“Hello,” followed by the value contained in the
Text property of the
txtName text box control followed by a string constant of “! Welcome to Visual
Basic 2005.”
The second parameter being passed to the MessageBox.Show method is the caption to be
used in the title bar of the Message Box dialog box.
Finally, the underscore (_) character used at the end of the lines in the following code enables you to split
your code onto separate lines. This tells the compiler that the rest of the code for the parameter is contin-
ued on the next line. This is really useful when building long strings, because it allows you to view the
entire code fragment in the Code Editor without having to scroll the Code Editor window to the right to
view the entire line of code.
Private Sub btnOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOK.Click
‘Display a message box greeting the user
MessageBox.Show(“Hello,” & txtName.Text & _
“! Welcome to Visual Basic 2005.”, _
“Hello User Message”)
End Sub
The next procedure that you added code for was the Exit button’s Click event. Here you simply enter the
code:
Me.Close(). As explained earlier, the Me keyword refers to the form itself. The Close method of
the form closes the form and releases all resources associated with it, thus ending the program.
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
‘End the program and close the form
Me.Close()
End Sub
21
Welcome to Visual Basic 2005
04_574019 ch01.qxd 9/16/05 9:43 PM Page 21