The first line of text you entered in this procedure (‘Display a message box greeting to the
user) 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 2008 begin with a single quote ('), and everything following on that line is considered a comment and ignored by the compiler.
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 2008.”. 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 continued 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 to the user
MessageBox.Show(“Hello,” & txtName.Text & _
“! Welcome to Visual Basic 2008.”, _
“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
No comments:
Post a Comment